### Install ecs-logging-python Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md Install the ecs-logging-python library using pip. ```cmd $ python -m pip install ecs-logging ``` -------------------------------- ### Install ecs-logging-python Source: https://github.com/elastic/ecs-logging-python/blob/main/README.md Install the library using pip. Ensure you are using a compatible Python version. ```bash python -m pip install ecs-logging ``` -------------------------------- ### ECS Log Output Example Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md Example of the JSON output generated by the ecs-logging library for a debug log message. ```json { "@timestamp": "2020-03-20T18:11:37.895Z", "log.level": "debug", "message": "Example message!", "ecs": { "version": "1.6.0" }, "http": { "request": { "method": "get" } }, "log": { "logger": "app", "origin": { "file": { "line": 14, "name": "test.py" }, "function": "func" }, "original": "Example message!" } } ``` -------------------------------- ### Example ECS-Formatted JSON Output Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md This is an example of a log message formatted according to the Elastic Common Schema (ECS). It includes timestamp, log level, message, and detailed ECS fields for HTTP and URL information. ```json { "@timestamp": "2020-03-26T13:08:11.728Z", "log.level": "info", "message": "Hello, 世界!", "ecs": { "version": "1.6.0" }, "http": { "request": { "bytes": 1337, "method": "get" }, "version": "2" }, "url": { "domain": "example.com", "original": "https://example.com", "path": "/", "port": 443, "registered_domain": "example.com", "scheme": "https", "top_level_domain": "com" } } ``` -------------------------------- ### Configure Structlog for ECS Logging Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md Configure structlog to use the ECS formatter, preserving non-ASCII characters. This setup is useful for applications that need to log messages with international characters in an ECS-compliant format. ```python structlog.configure( processors=[ecs_logging.StructlogFormatter(ensure_ascii=False)], wrapper_class=structlog.BoundLogger, context_class=dict, logger_factory=structlog.PrintLoggerFactory(), ) logger = structlog.get_logger("app") logger.info("Hello, 世界!") # Non-ASCII characters are preserved in output ``` -------------------------------- ### Filebeat < 7.16 Log File Configuration Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md Configure Filebeat versions prior to 7.16 to parse ECS-formatted JSON logs from a file. This setup includes options for handling JSON keys and errors. ```yaml filebeat.inputs: - type: log paths: /path/to/logs.json json_keys_under_root: true json.overwrite_keys: true json.add_error_key: true json.expand_keys: true processors: - add_host_metadata: ~ - add_cloud_metadata: ~ - add_docker_metadata: ~ - add_kubernetes_metadata: ~ ``` -------------------------------- ### Basic Standard Library Logging Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md Configure Python's standard library logging to use ECS formatting. Ensure the logger is set to an appropriate level and an ECS formatter is added to the handler. ```python import logging import ecs_logging # Get the Logger logger = logging.getLogger("app") logger.setLevel(logging.DEBUG) # Add an ECS formatter to the Handler handler = logging.StreamHandler() handler.setFormatter(ecs_logging.StdlibFormatter()) logger.addHandler(handler) # Emit a log! logger.debug("Example message!", extra={"http.request.method": "get"}) ``` -------------------------------- ### Update Version in __init__.py Source: https://github.com/elastic/ecs-logging-python/blob/main/RELEASING.md Modify the version number in the __init__.py file according to the release type (major, minor, or patch). ```python ecs_logging/__init__.py ``` -------------------------------- ### Structlog Configuration for ECS Logging Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md Configure structlog to use `ecs_logging.StructlogFormatter` as the final processor for JSON conversion and ECS enrichment. Ensure it's the last processor in the list. ```python import structlog import ecs_logging # Configure Structlog structlog.configure( processors=[ecs_logging.StructlogFormatter()], wrapper_class=structlog.BoundLogger, context_class=dict, logger_factory=structlog.PrintLoggerFactory(), ) # Get the Logger logger = structlog.get_logger("app") # Add additional context logger = logger.bind(**{ "http": { "version": "2", "request": { "method": "get", "bytes": 1337, }, }, "url": { "domain": "example.com", "path": "/", "port": 443, "scheme": "https", "registered_domain": "example.com", "top_level_domain": "com", "original": "https://example.com", } }) # Emit a log! logger.debug("Example message!") ``` -------------------------------- ### Tag Release Commit Source: https://github.com/elastic/ecs-logging-python/blob/main/RELEASING.md Create a signed Git tag for the release commit, using the version number. The changelog content should be copied into the tag message. ```git git tag -s X.Y.Z ``` -------------------------------- ### Commit Changes for Release Source: https://github.com/elastic/ecs-logging-python/blob/main/RELEASING.md Commit the version update and changelog modifications with a specific message format. ```git git commit -m "update CHANGELOG and rev to vX.Y.Z" ``` -------------------------------- ### Docker Container Labels for ECS Logging Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md Apply these labels to Docker containers to ensure ECS-formatted logs are parsed correctly. This configuration controls JSON key overwriting, error key addition, and key expansion. ```yaml labels: co.elastic.logs/json.overwrite_keys: true <1> co.elastic.logs/json.add_error_key: true <2> co.elastic.logs/json.expand_keys: true <3> ``` -------------------------------- ### Push Tags Upstream Source: https://github.com/elastic/ecs-logging-python/blob/main/RELEASING.md Push the newly created Git tags to the upstream repository to make them available for the release process. ```git git push upstream --tags ``` -------------------------------- ### Kubernetes Pod Annotations for ECS Logging Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md Add these annotations to Kubernetes pods to ensure ECS-formatted logs are parsed correctly. This configuration affects how JSON keys are handled, including overwriting, error key addition, and key expansion. ```yaml annotations: co.elastic.logs/json.overwrite_keys: true <1> co.elastic.logs/json.add_error_key: true <2> co.elastic.logs/json.expand_keys: true <3> ``` -------------------------------- ### Filebeat 7.16+ Log File Configuration Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md Configure Filebeat 7.16+ to use the filestream input for parsing ECS-formatted JSON logs from a file. Ensures proper handling of JSON unmarshalling errors and key expansion. ```yaml filebeat.inputs: - type: filestream <1> paths: /path/to/logs.json parsers: - ndjson: overwrite_keys: true <2> add_error_key: true <3> expand_keys: true <4> processors: <5> - add_host_metadata: ~ - add_cloud_metadata: ~ - add_docker_metadata: ~ - add_kubernetes_metadata: ~ ``` -------------------------------- ### Exclude Fields from ECS Logging Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md Configure StdlibFormatter to exclude specific fields or entire categories of fields from the log output using the `exclude_fields` option. ```python from ecs_logging import StdlibFormatter formatter = StdlibFormatter( exclude_fields=[ # You can specify individual fields to ignore: "log.original", # or you can also use prefixes to ignore # whole categories of fields: "process", "log.origin", ] ) ``` -------------------------------- ### Limit Stack Trace Depth Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md Control the number of stack frames collected for `error.stack_trace` in StdlibFormatter using `stack_trace_limit`. Set to 0 to disable collection. ```python from ecs_logging import StdlibFormatter formatter = StdlibFormatter( # Only collects 3 stack frames stack_trace_limit=3, ) formatter = StdlibFormatter( # Collects the last 2 frames (closest to the error) stack_trace_limit=-2, ) formatter = StdlibFormatter( # Disable stack trace collection stack_trace_limit=0, ) ``` -------------------------------- ### Control ASCII Encoding in StdlibFormatter Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md Configure StdlibFormatter to either escape non-ASCII characters or preserve them using the `ensure_ascii` parameter. Default is to escape. ```python from ecs_logging import StdlibFormatter # Default behavior - non-ASCII characters are escaped formatter = StdlibFormatter() # Output: {"message":"Hello \\u4e16\\u754c"} # Preserve non-ASCII characters formatter = StdlibFormatter(ensure_ascii=False) # Output: {"message":"Hello 世界"} ``` -------------------------------- ### Control ASCII Encoding in StructlogFormatter Source: https://github.com/elastic/ecs-logging-python/blob/main/docs/reference/installation.md Similar to StdlibFormatter, StructlogFormatter supports the `ensure_ascii` parameter to control non-ASCII character escaping. ```python import structlog import ecs_logging ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.