[Python] Custom log level not working with structlog

Discussão em 'Python' iniciado por Stack, Abril 19, 2025.

  1. Stack

    Stack Membro Participativo

    I am working with python custom log - TRACE is custom log level in below code.

    With default logger, its working fine

    But when I change it for structlog it is giving error.

    structlog not able to identify TRACE in below code.

    It seems that structlog doesn't support custom log levels ?

    Already tried workaround mentioned https://github.com/hynek/structlog/issues/47

    i.e

    structlog.stdlib.TRACE = TRACE = 5
    structlog.stdlib._NAME_TO_LEVEL['trace'] = TRACE


    But it is not working

    TRACE = 19


    logging.addLevelName(TRACE, "TRACE")

    logging.basicConfig(
    level=os.environ.get("LOGLEVEL", "TRACE"),
    format=os.environ.get("LOGFORMAT", '%(levelname)-8s= %(asctime)-15s = %(message)s'))



    structlog.configure(
    processors=[
    structlog.stdlib.filter_by_level,
    structlog.stdlib.add_logger_name,
    structlog.stdlib.add_log_level,
    structlog.stdlib.PositionalArgumentsFormatter(),
    structlog.processors.StackInfoRenderer(),
    structlog.processors.format_exc_info,
    structlog.processors.UnicodeDecoder(),
    structlog.stdlib.render_to_log_kwargs,
    ],
    context_class=dict,
    logger_factory=structlog.stdlib.LoggerFactory(),
    wrapper_class=structlog.stdlib.BoundLogger,
    cache_logger_on_first_use=True,
    )




    #LOG = logging.getLogger() ->> Working fine
    LOG = structlog.getLogger()



    LOG.log(TRACE, "hello") ->> error on this line
    LOG.info("testing")

    Continue reading...

Compartilhe esta Página