ecs-logging-python icon indicating copy to clipboard operation
ecs-logging-python copied to clipboard

Feature parity for structlog

Open as42sl opened this issue 3 years ago • 5 comments

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.

as42sl avatar Dec 01 '21 07:12 as42sl

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.

basepi avatar Dec 01 '21 17:12 basepi

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

HenrikOssipoff avatar Aug 04 '22 08:08 HenrikOssipoff

@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.

basepi avatar Aug 04 '22 15:08 basepi

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,
)

jasperjonker avatar Oct 04 '22 11:10 jasperjonker

@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

basepi avatar Oct 05 '22 19:10 basepi