elastic / ecs-logging-python

https://pypi.org/project/ecs-logging
Apache License 2.0
69 stars 26 forks source link

Feature parity for structlog #73

Open as42sl opened 2 years ago

as42sl commented 2 years ago

The structlog logger only sets the log level, timestamp and log message while the stdlib logger is setting a lot of more stuff like exceptions, stack infos, thread/process infos. It would be great to have a feature parity here.

basepi commented 2 years ago

Thanks for the request! structlog is awesome and we should definitely make sure our support there is every bit as good as the stdlib version.

HenrikOssipoff commented 2 years ago

Just came by here. Maybe I'm mistaken, but it also appears that the structlog processor doesn't add correlation data from Elastic APM, even though the docs sounds like this is automatic for both stdlib logger and structlog: https://www.elastic.co/guide/en/ecs-logging/python/current/installation.html#correlation

basepi commented 2 years ago

@HenrikOssipoff the structlog processor just takes the whole event dict. So if you're using the structlog handler then it should have all that data.

jasperjonker commented 2 years ago

I had some issues using structlog and ecs_logging together with their processors. Apparently the order of the structlog.configure(processors=[...]) list matters. The ecs_logging.StructlogFormatter() needs to be last in the list.

Example:

structlog.configure(
    processors=[
        structlog.contextvars.merge_contextvars,
        structlog.processors.CallsiteParameterAdder(
            {
                structlog.processors.CallsiteParameter.FILENAME,
                structlog.processors.CallsiteParameter.FUNC_NAME,
                structlog.processors.CallsiteParameter.LINENO,
            }
        ),
        ecs_logging.StructlogFormatter(),
    ],
    wrapper_class=structlog.stdlib.BoundLogger,
    logger_factory=structlog.PrintLoggerFactory(),
    cache_logger_on_first_use=True,
)
basepi commented 2 years ago

@jasperjonker correct -- typically you wouldn't want a processor that both enriches and converts to JSON -- you would want those to be separate processors. But in this case we have a custom json_dumps() function which enforces some ordered fields in the resulting json string to match the ECS spec. Apologies for the lack of clarity! I have clarified in https://github.com/elastic/ecs-logging-python/pull/83