[Python] Lazy logger message string evaluation

Discussão em 'Python' iniciado por Stack, Maio 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using standard python logging module in my python application:


    import logging
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger("log")
    while True:
    logger.debug('Stupid log message " + ' '.join([str(i) for i in range(20)]) )
    # Do something


    The issue is that although debug level is not enabled, that stupid log message is evaluated on each loop iteration, which harms performance badly.

    Is there any solution for this?

    In C++ we have log4cxx package that provides macros like this:
    LOG4CXX_DEBUG(logger, messasage)
    That effectively evaluates to


    if (log4cxx::debugEnabled(logger)) {
    log4cxx.log(logger,log4cxx::LOG4CXX_DEBUG, message)
    }


    But since there are no macros in Python (AFAIK), if there a efficient way to do logging?

    Continue reading...

Compartilhe esta Página