Note that there are some explanatory texts on larger screens.

plurals
  1. POPython logger not respecting setLevel?
    primarykey
    data
    text
    <p>I've spent a bit of time looking through the site at Python logger questions hoping my would be resolved there. I've set up a logger with two stream handlers that have both different formats and levels of logging, here's a functional snippet from my codebase:</p> <pre><code>import os import time import logging LOG_LEVELS = [logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG] TEST_RESULT_LEVELV_NUM = 51 # http://stackoverflow.com/a/11784984/196832 def status(self, message, *args, **kws): self._log(TEST_RESULT_LEVELV_NUM, message, args, **kws) logging.addLevelName(TEST_RESULT_LEVELV_NUM, "RESULT") logging.Logger.result = status def setup_logging(level=0, quiet=False, logdir=None): logger = logging.getLogger('juju-test') ffmt = logging.Formatter('%(asctime)s %(name)s %(levelname)-8s: %(message)s') cfmt = logging.Formatter('%(name)s %(levelname)s: %(message)s') #logger.setLevel(0) if level &gt;= len(LOG_LEVELS): level = len(LOG_LEVELS) - 1 if logdir: if not os.path.exists(logdir): os.makedirs(logdir) logfile = os.path.join(logdir, 'juju-test.%s.log' % int(time.time())) fh = logging.FileHandler(logfile) # Always at least log to INFO for file, unless DEBUG is requested fh.setLevel(LOG_LEVELS[level if level &gt;= 2 else 2]) fh.setFormatter(ffmt) logger.addHandler(fh) if not quiet: ch = logging.StreamHandler() ch.setLevel(LOG_LEVELS[level]) ch.setFormatter(cfmt) logger.addHandler(ch) return logger </code></pre> <p>I've been using an argparse to feed this, but for testing purposes if you feed the following to function:</p> <pre><code>logger = setup_logging(level=1, logdir="/tmp/oofrab/") logger.info('Informative!') logger.warn('Whoa buddy!') logger.error('Look what you did.') logger.result("They told me not to make a custom logging level, I'll show them!") logger.debug('Lots of bugs, man') </code></pre> <p>I'd expect to see <code>status</code>, <code>error</code>, and <code>warn</code> in the console. Then <code>status</code>, <code>error</code>, <code>warn</code> and <code>info</code> in the log. However, I only see down to <code>warn</code> in both console and log file, despite selecting <code>logging.INFO</code> (key 2 in the <code>LOG_LEVELS</code> list) for the file handler. Is this expected?</p> <p>I'm not using <code>basicConfig</code> or anything else when building the logger, why can't I have these two custom levels?</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