### Setup logging environment Source: https://github.com/nhairs/python-json-logger/blob/main/docs/cookbook.md Common initialization for logging examples. ```python ## Common Setup ## ----------------------------------------------------------------------------- import logging import uuid from pythonjsonlogger.json import JsonFormatter logger = logging.getLogger("test") logger.setLevel(logging.INFO) handler = logging.StreamHandler() logger.addHandler(handler) ``` -------------------------------- ### Basic JsonFormatter Setup Source: https://context7.com/nhairs/python-json-logger/llms.txt Demonstrates basic setup of JsonFormatter with Python's logging module. Logs are output as JSON to the console. ```python import logging from pythonjsonlogger.json import JsonFormatter # Basic setup logger = logging.getLogger("my_app") logger.setLevel(logging.INFO) handler = logging.StreamHandler() formatter = JsonFormatter() handler.setFormatter(formatter) logger.addHandler(handler) # Basic logging logger.info("Application started") # Output: {"message": "Application started"} # Logging with extra fields logger.info("User logged in", extra={"user_id": 12345, "ip_address": "192.168.1.1"}) # Output: {"message": "User logged in", "user_id": 12345, "ip_address": "192.168.1.1"} # Logging a dictionary message logger.info({ "event": "purchase_completed", "order_id": "ORD-789", "amount": 99.99, "currency": "USD" }) # Output: {"event": "purchase_completed", "order_id": "ORD-789", "amount": 99.99, "currency": "USD", "message": ""} ``` -------------------------------- ### Install Python JSON Logger Source: https://context7.com/nhairs/python-json-logger/llms.txt Install the library using pip. For optional high-performance encoders like orjson and msgspec, include them in the installation command. ```bash pip install python-json-logger # With optional high-performance encoders pip install python-json-logger orjson msgspec ``` -------------------------------- ### Install python-json-logger from GitHub Source: https://github.com/nhairs/python-json-logger/blob/main/docs/quickstart.md Install a specific version of the library directly from GitHub releases using a wheel URL. ```shell # e.g. 3.0.0 wheel pip install 'python-json-logger@https://github.com/nhairs/python-json-logger/releases/download/v3.0.0/python_json_logger-3.0.0-py3-none-any.whl' ``` -------------------------------- ### Install python-json-logger via pip Source: https://github.com/nhairs/python-json-logger/blob/main/docs/quickstart.md Use this command to install the library using pip. ```shell pip install python-json-logger ``` -------------------------------- ### Preview README Locally with Grip Source: https://github.com/nhairs/python-json-logger/blob/main/docs/contributing.md Previews the README.md file locally using the 'grip' tool after it has been installed. ```shell grip ``` -------------------------------- ### Basic JSON Logging Setup Source: https://github.com/nhairs/python-json-logger/blob/main/docs/index.md Demonstrates how to set up a basic logger that outputs messages in JSON format. Ensure the JsonFormatter is applied to the handler. ```python import logging from pythonjsonlogger.json import JsonFormatter logger = logging.getLogger() logger.setLevel(logging.INFO) handler = logging.StreamHandler() handler.setFormatter(JsonFormatter()) logger.addHandler(handler) logger.info("Logging using python-json-logger!", extra={"more_data": True}) # {"message": "Logging using python-json-logger!", "more_data": true} ``` -------------------------------- ### Install Development Dependencies Source: https://github.com/nhairs/python-json-logger/blob/main/docs/contributing.md Installs the project's development dependencies, typically within a virtual environment, using pip. ```shell pip install -e '.[dev]' ``` -------------------------------- ### Use MsgspecFormatter for Fast Encoding Source: https://context7.com/nhairs/python-json-logger/llms.txt Demonstrates how to use MsgspecFormatter for efficient JSON encoding with optional schema validation. Ensure msgspec is installed. ```python import logging from pythonjsonlogger.msgspec import MsgspecFormatter logger = logging.getLogger("msgspec") handler = logging.StreamHandler() formatter = MsgspecFormatter( "message,levelname,asctime", style="," ) handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.INFO) logger.info("Using msgspec encoder", extra={"count": 100, "active": True}) ``` -------------------------------- ### Define a function with Google-style docstrings Source: https://github.com/nhairs/python-json-logger/blob/main/docs/style-guide.md Follows the Google Python Style Guide for docstrings, including version markers for changes. ```python def my_function(param1: str, param2: int) -> bool: """Does something interesting. Args: param1: The first parameter, a string. param2: The second parameter, an integer. Returns: True if successful, False otherwise. Raises: ValueError: If param2 is negative. *New in 3.1* """ # ... function logic ... return True # See 'Return Statements' ``` -------------------------------- ### Serve Documentation Locally with MkDocs Source: https://github.com/nhairs/python-json-logger/blob/main/docs/contributing.md Builds and serves the project's documentation locally using MkDocs, allowing for previewing changes. ```shell mkdocs serve ``` -------------------------------- ### Log Exceptions with Stack Traces Source: https://context7.com/nhairs/python-json-logger/llms.txt Shows how to log exceptions with full stack traces, including options to format them as a single string or an array of lines for easier parsing. This requires the 'exc_info' field to be included in the formatter. ```python import logging from pythonjsonlogger.json import JsonFormatter logger = logging.getLogger("errors") handler = logging.StreamHandler() # Stack traces as single string (default) formatter = JsonFormatter("message,levelname,exc_info", style=",") # Stack traces as array of lines for easier parsing formatter = JsonFormatter( "message,levelname,exc_info", style=",", exc_info_as_array=True, stack_info_as_array=True ) handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.INFO) try: result = 1 / 0 except ZeroDivisionError: logger.exception("Division failed") ``` -------------------------------- ### Version-Specific Logic Pattern Source: https://github.com/nhairs/python-json-logger/blob/main/docs/style-guide.md Use sys.version_info to handle compatibility across different Python versions. ```python if sys.version_info >= (3, 10): # Python 3.10+ specific code else: # Code for older versions ``` -------------------------------- ### Create Custom Formatter by Subclassing Source: https://context7.com/nhairs/python-json-logger/llms.txt Shows how to create a custom JSON formatter by subclassing JsonFormatter and overriding the process_log_record method for advanced log data transformations, such as masking sensitive fields or adding computed fields. ```python import logging from pythonjsonlogger.json import JsonFormatter class SensitiveDataFormatter(JsonFormatter): SENSITIVE_FIELDS = {"password", "token", "secret", "api_key", "credit_card"} def process_log_record(self, log_data): # Mask sensitive fields for key in log_data: if any(sensitive in key.lower() for sensitive in self.SENSITIVE_FIELDS): log_data[key] = "***REDACTED***" # Add computed fields log_data["log_source"] = "python-app" return log_data logger = logging.getLogger("secure") handler = logging.StreamHandler() formatter = SensitiveDataFormatter() handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.INFO) logger.info("User authenticated", extra={ "user_id": 123, "api_key": "sk-1234567890abcdef", "session_token": "eyJhbGciOiJIUzI1NiIs..." }) ``` -------------------------------- ### Include all standard library fields Source: https://github.com/nhairs/python-json-logger/blob/main/docs/cookbook.md Include all standard library LogRecord attributes by setting reserved_attrs to an empty list. ```python all_fields_formatter = JsonFormatter(reserved_attrs=[]) ``` -------------------------------- ### Configure JSON logging via YAML Source: https://context7.com/nhairs/python-json-logger/llms.txt Externalize logging configuration into a YAML file for better management in production environments. ```yaml # logging_config.yaml version: 1 disable_existing_loggers: False formatters: json: (): pythonjsonlogger.json.JsonFormatter format: "%(asctime)s %(levelname)s %(name)s %(module)s %(funcName)s %(lineno)s %(message)s" rename_fields: asctime: timestamp levelname: level static_fields: service: api-service version: "1.0.0" handlers: default: formatter: json class: logging.StreamHandler stream: ext://sys.stderr loggers: myapp: level: INFO handlers: - default propagate: no root: level: WARNING handlers: - default ``` ```python import logging import logging.config import yaml with open("logging_config.yaml", "r") as f: config = yaml.safe_load(f) logging.config.dictConfig(config) logger = logging.getLogger("myapp") logger.info("Loaded from YAML config") ``` -------------------------------- ### Add Static and Default Fields Source: https://context7.com/nhairs/python-json-logger/llms.txt Incorporate constant fields into every log record using `static_fields`. Use `defaults` to provide default values that can be overridden by `extra` data in individual log calls. ```python import logging from pythonjsonlogger.json import JsonFormatter logger = logging.getLogger("service") handler = logging.StreamHandler() # Static fields are always included formatter = JsonFormatter( static_fields={ "service": "user-api", "version": "2.1.0", "environment": "production" }, defaults={ "region": "us-east-1" # Can be overridden per-log } ) handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.INFO) logger.info("Service started") # Output: {"message": "Service started", "region": "us-east-1", "service": "user-api", "version": "2.1.0", "environment": "production"} logger.info("Request processed", extra={"region": "eu-west-1", "request_id": "abc123"}) # Output: {"message": "Request processed", "region": "eu-west-1", "service": "user-api", "version": "2.1.0", "environment": "production", "request_id": "abc123"} ``` -------------------------------- ### Configure JsonFormatter with sequence of strings format Source: https://github.com/nhairs/python-json-logger/blob/main/docs/quickstart.md Provide a list or tuple of strings to the JsonFormatter to specify the fields to be logged. ```python # Sequence of strings format formatter = JsonFormatter(["message", "asctime", "exc_info"]) ``` -------------------------------- ### Implement custom formatter styles Source: https://github.com/nhairs/python-json-logger/blob/main/docs/cookbook.md Support custom styles by setting validate=False and overriding the parse method. ```python class CommaSupport(JsonFormatter): def parse(self) -> list[str]: if isinstance(self._style, str) and self._style == ",": return self._fmt.split(",") return super().parse() formatter = CommaSupport("message,asctime", style=",", validate=False) ``` -------------------------------- ### Run Tests with Pytest Source: https://github.com/nhairs/python-json-logger/blob/main/docs/contributing.md Executes the project's test suite using Pytest. ```shell pytest ``` -------------------------------- ### Lint Code with Pylint Source: https://github.com/nhairs/python-json-logger/blob/main/docs/contributing.md Runs Pylint on the 'src' directory to check for code quality and potential errors, outputting results in color. ```shell pylint --output-format=colorized src ``` -------------------------------- ### Configure JSON logging with dictConfig Source: https://context7.com/nhairs/python-json-logger/llms.txt Use Python's dictConfig to define JSON formatters, rename fields, and add static metadata to logs. ```python import logging import logging.config config = { "version": 1, "disable_existing_loggers": False, "formatters": { "json": { "()": "pythonjsonlogger.json.JsonFormatter", "format": "%(asctime)s %(levelname)s %(name)s %(message)s", "rename_fields": { "asctime": "timestamp", "levelname": "level", "name": "logger" }, "static_fields": { "service": "my-service", "environment": "production" } } }, "handlers": { "console": { "class": "logging.StreamHandler", "formatter": "json", "stream": "ext://sys.stdout" }, "file": { "class": "logging.FileHandler", "formatter": "json", "filename": "app.log" } }, "root": { "level": "INFO", "handlers": ["console", "file"] }, "loggers": { "uvicorn": { "level": "INFO", "handlers": ["console"], "propagate": False } } } logging.config.dictConfig(config) logger = logging.getLogger(__name__) logger.info("Application configured", extra={"config_version": 1}) ``` -------------------------------- ### Format Code with Black Source: https://github.com/nhairs/python-json-logger/blob/main/docs/contributing.md Formats Python code in the 'src' and 'tests' directories using the Black code formatter. ```shell black src tests ``` -------------------------------- ### Python Configuration for JSON Logging Source: https://github.com/nhairs/python-json-logger/blob/main/docs/cookbook.md This Python script defines helper functions and variables for the YAML configuration, including a custom JSON default encoder and dynamic retrieval of project version and environment. ```python import importlib.metadata import os class Dummy: pass def my_json_default(obj: Any) -> Any: if isinstance(obj, Dummy): return "DUMMY" return obj def get_version_metadata(): # https://stackoverflow.com/a/78082532 version = importlib.metadata.version(PROJECT_NAME) return version PROJECT_NAME = 'test-api' PROJECT_VERSION = get_version_metadata() ENVIRONMENT = os.environ.get('ENVIRONMENT', 'dev') ``` -------------------------------- ### Configure JsonFormatter with standard library format Source: https://github.com/nhairs/python-json-logger/blob/main/docs/quickstart.md Use standard Python logging format strings with the JsonFormatter by specifying the style. This allows for flexible field ordering and inclusion. ```python # Standard library format formatter = JsonFormatter("{message}{asctime}{exc_info}", style="{") ``` -------------------------------- ### Logging Expensive Data with LazyString Source: https://github.com/nhairs/python-json-logger/blob/main/docs/cookbook.md This Python snippet shows how to use the `LazyString` from the `lazy-string` library to defer the computation of log message data until it's actually needed. This is an alternative to using `isEnabledFor`. ```python ## Log Using lazy-string ## ------------------------------------- from lazy_string import LazyString as L start = time.time() logger.info( { "data": L("hello {}".format, L(expensive_to_compute)) } ) print(f"Logging INFO using LazyString took: {int(time.time() - start)}s") start = time.time() logger.debug( { "data": L("hello {}".format, L(expensive_to_compute)) } ) print(f"Logging DEBUG using LazyString took: {int(time.time() - start)}s") ``` -------------------------------- ### Add Automatic Timestamps Source: https://context7.com/nhairs/python-json-logger/llms.txt Enable automatic timestamp generation for log records by setting the 'timestamp' parameter to True or a custom key name. The default key is 'timestamp'. ```python import logging from pythonjsonlogger.json import JsonFormatter logger = logging.getLogger("timestamped") handler = logging.StreamHandler() # Add timestamp with default key "timestamp" formatter = JsonFormatter(timestamp=True) handler.setFormatter(formatter) # Or use a custom key name formatter = JsonFormatter(timestamp="@timestamp") handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.INFO) logger.info("Event occurred") # Output: {"message": "Event occurred", "@timestamp": "2024-01-15T10:30:45.123456+00:00"} ``` -------------------------------- ### Integrate JsonFormatter with Python logging Source: https://github.com/nhairs/python-json-logger/blob/main/docs/quickstart.md Attach the JsonFormatter to a logging handler to produce JSON output. Ensure necessary imports are included. ```python import logging from pythonjsonlogger.json import JsonFormatter logger = logging.getLogger() logHandler = logging.StreamHandler() formatter = JsonFormatter() logHandler.setFormatter(formatter) logger.addHandler(logHandler) ``` -------------------------------- ### Add additional fields using 'extra' Source: https://github.com/nhairs/python-json-logger/blob/main/docs/quickstart.md Include extra fields in log messages by passing a dictionary to the 'extra' argument of the logging method. ```python logger.info( "this logs the same additional fields as above", extra={ "my_data": 1, "other_stuff": False, }, ) ``` -------------------------------- ### YAML Configuration for JsonFormatter Source: https://github.com/nhairs/python-json-logger/blob/main/docs/cookbook.md This YAML configuration sets up the `JsonFormatter` for logging, including custom field renaming and static fields. It's designed to be used with Python's `logging.config.fileConfig`. ```yaml version: 1 disable_existing_loggers: False formatters: default: "()": pythonjsonlogger.json.JsonFormatter format: "% (asctime)s %(levelname)s %(name)s %(module)s %(funcName)s %(lineno)s %(message)s" json_default: ext://logging_config.my_json_default rename_fields: "asctime": "timestamp" "levelname": "status" static_fields: "service": ext://logging_config.PROJECT_NAME "env": ext://logging_config.ENVIRONMENT "version": ext://logging_config.PROJECT_VERSION "app_log": "true" handlers: default: formatter: default class: logging.StreamHandler stream: ext://sys.stderr access: formatter: default class: logging.StreamHandler stream: ext://sys.stdout loggers: uvicorn.error: level: INFO handlers: - default propagate: no uvicorn.access: level: INFO handlers: - access propagate: no ``` -------------------------------- ### Set static fields for JsonFormatter Source: https://github.com/nhairs/python-json-logger/blob/main/docs/quickstart.md Add static fields that are included in every log record using the 'static_fields' argument. These fields are constant across all logs. ```python formatter = JsonFormatter( static_fields={"True gets logged on every record?": True} ) ``` -------------------------------- ### Log message as a dictionary Source: https://github.com/nhairs/python-json-logger/blob/main/docs/quickstart.md Log messages as dictionaries to include structured data. Ensure a 'message' key is present if you want a string message. ```python logger.info({ "my_data": 1, "message": "if you don't include this it will be an empty string", "other_stuff": False, }) ``` -------------------------------- ### Type Check Code with Mypy Source: https://github.com/nhairs/python-json-logger/blob/main/docs/contributing.md Performs static type checking on the 'src' and 'tests' directories using Mypy. ```shell mypy src tests ``` -------------------------------- ### Control Log Fields with Format Strings Source: https://context7.com/nhairs/python-json-logger/llms.txt Configure JsonFormatter to include specific log record attributes using standard Python logging format strings or comma-separated lists. Supports percent, brace, and comma styles. ```python import logging from pythonjsonlogger.json import JsonFormatter logger = logging.getLogger("formatted") handler = logging.StreamHandler() # Using percent style (default) formatter = JsonFormatter("%(message)s %(levelname)s %(name)s %(asctime)s") handler.setFormatter(formatter) # Using brace style formatter = JsonFormatter("{message}{levelname}{name}{asctime}", style="{") handler.setFormatter(formatter) # Using comma style (python-json-logger specific) formatter = JsonFormatter("message,levelname,name,asctime,funcName,lineno", style=",") handler.setFormatter(formatter) # Using sequence of fields formatter = JsonFormatter(["message", "levelname", "name", "asctime", "funcName", "lineno"]) handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.INFO) logger.info("Processing request") # Output: {"message": "Processing request", "levelname": "INFO", "name": "formatted", "asctime": "2024-01-15 10:30:45,123", "funcName": "", "lineno": 20} ``` -------------------------------- ### Inject request IDs via extra argument Source: https://github.com/nhairs/python-json-logger/blob/main/docs/cookbook.md Pass request IDs directly into log calls using the extra parameter. ```python ## Solution 1 ## ----------------------------------------------------------------------------- formatter = JsonFormatter() handler.setFormatter(formatter) def main_1(): print("========== MAIN 1 ==========") for i in range(3): request_id = uuid.uuid4() logger.info("loop start", extra={"request_id": request_id}) logger.info(f"loop {i}", extra={"request_id": request_id}) logger.info("loop end", extra={"request_id": request_id}) return main_1() ``` -------------------------------- ### Configure JsonFormatter with comma format Source: https://github.com/nhairs/python-json-logger/blob/main/docs/quickstart.md Use a comma-separated string for the format argument with JsonFormatter for simpler format definitions. ```python # Comma format formatter = JsonFormatter("message,asctime,exc_info", style=",") ``` -------------------------------- ### Logging Expensive Data with isEnabledFor Source: https://github.com/nhairs/python-json-logger/blob/main/docs/cookbook.md This Python snippet demonstrates how to avoid the performance cost of computing data for log messages when the logger's level is not enabled. It uses `logger.isEnabledFor` to conditionally execute the expensive computation. ```python import logging import time from pythonjsonlogger.json import JsonFormatter def expensive_to_compute(): time.sleep(5) return "world" ## Setup ## ------------------------------------- logger = logging.getLogger() handler = logging.StreamHandler() formatter = JsonFormatter() handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.INFO) ## Log Using isEnabledFor ## ------------------------------------- start = time.time() if logger.isEnabledFor(logging.INFO): logger.info( { "data": "hello {}".format(expensive_to_compute()) } ) print(f"Logging INFO using isEnabledFor took: {int(time.time() - start)}s") start = time.time() if logger.isEnabledFor(logging.DEBUG): logger.debug( { "data": "hello {}".format(expensive_to_compute()) } ) print(f"Logging DEBUG using isEnabledFor took: {int(time.time() - start)}s") ``` -------------------------------- ### Set default fields for JsonFormatter Source: https://github.com/nhairs/python-json-logger/blob/main/docs/quickstart.md Configure default fields that are added to every log record using the 'defaults' argument. These can be overridden by specific log messages or 'extra' data. ```python formatter = JsonFormatter( defaults={"environment": "dev"} ) # ... logger.info("this message will have environment=dev by default") logger.info("this overwrites the environment field", extra={"environment": "prod"}) ``` -------------------------------- ### High Performance Encoding with OrjsonFormatter Source: https://context7.com/nhairs/python-json-logger/llms.txt Utilize OrjsonFormatter for significantly faster JSON encoding, ideal for high-throughput logging environments. It supports basic formatting and indentation. ```python import logging from pythonjsonlogger.orjson import OrjsonFormatter logger = logging.getLogger("fast") handler = logging.StreamHandler() # Basic orjson formatter formatter = OrjsonFormatter() # With indentation for readability formatter = OrjsonFormatter(json_indent=True) handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.INFO) import uuid from datetime import datetime logger.info("High performance logging", extra={ "request_id": uuid.uuid4(), "timestamp": datetime.now(), "metrics": {"latency_ms": 45, "status": 200} }) # Output: {"message": "High performance logging", "request_id": "550e8400-e29b-41d4-a716-446655440000", "timestamp": "2024-01-15T10:30:45.123456", "metrics": {"latency_ms": 45, "status": 200}} ``` -------------------------------- ### Inject request IDs via custom formatter Source: https://github.com/nhairs/python-json-logger/blob/main/docs/cookbook.md Override process_log_record in a custom formatter to inject fields without modifying the original LogRecord. ```python ## Solution 3 ## ----------------------------------------------------------------------------- # Reuse REQUEST_ID stuff from solution 2 class MyFormatter(JsonFormatter): def process_log_record(self, log_data): log_data["request_id"] = get_request_id() return log_data handler.setFormatter(MyFormatter()) def main_3(): print("========== MAIN 3 ==========") for i in range(3): generate_request_id() logger.info("loop start") logger.info(f"loop {i}") logger.info("loop end") return main_3() ``` -------------------------------- ### Inject request IDs via logging filter Source: https://github.com/nhairs/python-json-logger/blob/main/docs/cookbook.md Use a custom logging filter to attach request IDs to the LogRecord attributes. ```python ## Solution 2 ## ----------------------------------------------------------------------------- REQUEST_ID: str | None = None def get_request_id() -> str: return REQUEST_ID def generate_request_id(): global REQUEST_ID REQUEST_ID = str(uuid.uuid4()) class RequestIdFilter(logging.Filter): def filter(self, record): record.request_id = get_request_id() # Add request_id to the LogRecord return True request_id_filter = RequestIdFilter() logger.addFilter(request_id_filter) def main_2(): print("========== MAIN 2 ==========") for i in range(3): generate_request_id() logger.info("loop start") logger.info(f"loop {i}") logger.info("loop end") return main_2() logger.removeFilter(request_id_filter) ``` -------------------------------- ### Explicit Return Statement Convention Source: https://github.com/nhairs/python-json-logger/blob/main/docs/style-guide.md Functions must include an explicit return statement, using return or return None when no value is returned. ```python def process_data(data: dict) -> None: """Processes the given data.""" # ... processing logic ... print(data) return # or return None ``` -------------------------------- ### Rename Log Fields Source: https://context7.com/nhairs/python-json-logger/llms.txt Customize output field names to match specific log schema requirements without altering the original logging code. This is achieved by providing a mapping to the JsonFormatter. ```python import logging from pythonjsonlogger.json import JsonFormatter logger = logging.getLogger("renamed") handler = logging.StreamHandler() ``` -------------------------------- ### Handle complex types in JSON logs Source: https://context7.com/nhairs/python-json-logger/llms.txt The library automatically serializes common types like UUIDs, datetimes, Enums, and dataclasses into JSON-compatible formats. ```python import logging from pythonjsonlogger.json import JsonFormatter from datetime import datetime, date, time from enum import Enum import uuid from dataclasses import dataclass class Status(Enum): PENDING = "pending" ACTIVE = "active" COMPLETED = "completed" @dataclass class Order: id: str amount: float logger = logging.getLogger("types") handler = logging.StreamHandler() formatter = JsonFormatter() handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.INFO) # All these types are automatically handled logger.info("Complex types", extra={ "uuid": uuid.uuid4(), # -> "550e8400-e29b-41d4-a716-446655440000" "datetime": datetime.now(), # -> "2024-01-15T10:30:45.123456" "date": date.today(), # -> "2024-01-15" "time": time(10, 30, 45), # -> "10:30:45" "status": Status.ACTIVE, # -> "active" (enum value) "all_statuses": Status, # -> ["pending", "active", "completed"] (enum class) "order": Order(id="ORD-123", amount=99.99), # -> {"id": "ORD-123", "amount": 99.99} "binary_data": b"hello world" # -> "aGVsbG8gd29ybGQ=" (base64) }) ``` -------------------------------- ### Custom object serialization with JsonFormatter Source: https://github.com/nhairs/python-json-logger/blob/main/docs/quickstart.md Define a custom serialization function for objects not handled by the default JSON encoder using the 'json_default' argument. Remember to call the original json_default for broader compatibility. ```python def my_default(obj): if isinstance(obj, MyClass): return {"special": obj.special} formatter = JsonFormatter(json_default=my_default) ``` -------------------------------- ### Rename fields in JsonFormatter output Source: https://github.com/nhairs/python-json-logger/blob/main/docs/quickstart.md Customize the names of output fields using the 'rename_fields' argument. This is useful for standardizing field names across different loggers. ```python formatter = JsonFormatter( "{message}{levelname}", style="{", rename_fields={"levelname": "LEVEL"}, ) ``` -------------------------------- ### Modify log data before serialization Source: https://github.com/nhairs/python-json-logger/blob/main/docs/cookbook.md Override process_log_record to transform the log dictionary before it is converted to JSON. ```python class SillyFormatter(JsonFormatter): def process_log_record(self, log_data): new_record = {k[::-1]: v for k, v in log_data.items()} return new_record ``` -------------------------------- ### Custom Object Serialization with JsonFormatter Source: https://context7.com/nhairs/python-json-logger/llms.txt Provide a custom 'json_default' function or 'json_encoder' class to handle serialization of non-standard Python types like Decimals, custom classes, or UUIDs. ```python import logging from pythonjsonlogger.json import JsonFormatter, JsonEncoder from dataclasses import dataclass from decimal import Decimal import uuid @dataclass class User: id: int name: str email: str class CustomEncoder(JsonEncoder): def default(self, o): if isinstance(o, Decimal): return float(o) if isinstance(o, User): return {"user_id": o.id, "username": o.name} return super().default(o) # Or use a function def custom_default(obj): if isinstance(obj, Decimal): return float(obj) if isinstance(obj, User): return {"user_id": obj.id, "username": obj.name} raise TypeError(f"Object of type {type(obj)} is not JSON serializable") logger = logging.getLogger("custom") handler = logging.StreamHandler() # Using custom encoder class formatter = JsonFormatter(json_encoder=CustomEncoder) # Or using custom default function formatter = JsonFormatter(json_default=custom_default) handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.INFO) user = User(id=42, name="alice", email="alice@example.com") logger.info("User action", extra={"user": user, "amount": Decimal("99.99")}) # Output: {"message": "User action", "user": {"user_id": 42, "username": "alice"}, "amount": 99.99} ``` -------------------------------- ### Rename Fields with JsonFormatter Source: https://context7.com/nhairs/python-json-logger/llms.txt Use the 'rename_fields' parameter to map log record attributes to different keys in the JSON output. This is useful for conforming to specific log schemas like Elastic Common Schema. ```python from pythonjsonlogger import jsonlogger import logging logger = logging.getLogger("renamed-fields") logger.setLevel(logging.INFO) handler = logging.StreamHandler() formatter = jsonlogger.JsonFormatter( "{message}{levelname}{asctime}{name}", style="{", rename_fields={ "message": "@message", "levelname": "log.level", "asctime": "@timestamp", "name": "log.logger" } ) handler.setFormatter(formatter) logger.addHandler(handler) logger.info("Database connection established") ``` -------------------------------- ### Exclude fields from JsonFormatter output Source: https://github.com/nhairs/python-json-logger/blob/main/docs/quickstart.md Prevent specific fields from being added to the JSON output by including them in the 'reserved_attrs' list. By default, many LogRecord attributes are already reserved. ```python from pythonjsonlogger.core import RESERVED_ATTRS formatter = JsonFormatter( reserved_attrs=RESERVED_ATTRS+["request_id", "my_other_field"] ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.