Note that there are some explanatory texts on larger screens.

plurals
  1. POPython logging: propagate messages of level below current logger level
    primarykey
    data
    text
    <p>I want to log messages of a specific logger name, of a certain level and higher (say <code>INFO</code> and up) to a specific log handler, say a file handler, while still getting all log messages to the console. Python is version 2.7.</p> <p>What I tried until now was to create two loggers:</p> <ul> <li>A root logger</li> <li>A named logger</li> </ul> <p>For the root logger, I attached a <code>logging.StreamHandler</code>, and set the log level to <code>logging.DEBUG</code>.</p> <p>Then I attached a handler to the named logger and set level to <code>logging.INFO</code> for that logger.</p> <p>When I now call my module, which uses the named logger, I do not get <code>DEBUG</code> logs propagated to the root logger any more.</p> <p>Note: the extraLogger has a <em>StreamHandler</em> here to demonstrate the issue. In my production code I'd use a <em>FileHandler</em></p> <pre><code>import logging def do_logging(turn): logger = logging.getLogger('extra') logger.info('some info turn %d' % turn) logger.debug('this is debug fudge turn %d' % turn) rootLogger = logging.getLogger() handler = logging.StreamHandler() rootFormatter = logging.Formatter('root - %(levelname)s: %(msg)s') handler.setFormatter(rootFormatter) rootLogger.addHandler(handler) rootLogger.setLevel(logging.DEBUG) do_logging(1) extraLogger = logging.getLogger('extra') extraHandler = logging.StreamHandler() extraFormatter = logging.Formatter('extra - %(levelname)s: %(msg)s') extraHandler.setFormatter(extraFormatter) extraLogger.addHandler(extraHandler) extraLogger.setLevel(logging.INFO) do_logging(2) </code></pre> <p>Actual Output:</p> <pre><code>root - INFO: some info turn 1 root - DEBUG: this is debug fudge turn 1 extra - INFO: some info turn 2 root - INFO: some info turn 2 </code></pre> <p>Output that I would like to have:</p> <pre><code>root - INFO: some info turn 1 root - DEBUG: this is debug fudge turn 1 extra - INFO: some info turn 2 root - INFO: some info turn 2 root - DEBUG: this is debug fudge turn 2 </code></pre> <p>I suspect that a custom <code>Filter</code> would be helpful in this case, but I do not know how...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload