logging - Python logger not respecting setLevel? -
i've spent bit of time looking through site @ python logger questions hoping resolved there. i've set logger 2 stream handlers have both different formats , levels of logging, here's functional snippet codebase:
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 >= 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) # @ least log info file, unless debug requested fh.setlevel(log_levels[level if level >= 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
i've been using argparse feed this, testing purposes if feed following function:
logger = setup_logging(level=1, logdir="/tmp/oofrab/") logger.info('informative!') logger.warn('whoa buddy!') logger.error('look did.') logger.result("they told me not make custom logging level, i'll show them!") logger.debug('lots of bugs, man')
i'd expect see status
, error
, , warn
in console. status
, error
, warn
, info
in log. however, see down warn
in both console , log file, despite selecting logging.info
(key 2 in log_levels
list) file handler. expected?
i'm not using basicconfig
or else when building logger, why can't have these 2 custom levels?
apparently, logging.notset
not mean "all levels", rather defaults. setting parent logger level 0 reverts it's default accepted levels. being said, if set logger.setlevel logging.debug
sets logging accept levels passes filtering on various handlers filter further.
to around (and potential custom log levels) i've set initial logger level 1
Comments
Post a Comment