What is the point of setLevel in a python logging handler? -
let's have following code:
import logging import logging.handlers = logging.getlogger('myapp') h = logging.handlers.rotatingfilehandler('foo.log') h.setlevel(logging.debug) a.addhandler(h) # effective log level still logging.warn print a.geteffectivelevel() a.debug('foo message') a.warn('warning message')
i expect setting logging.debug
on handler cause debug-level messages written log file. however, prints 30 effective level (equal logging.warning
, default), , logs warn
message log file, not debug message.
it appears handler's log level being dropped on floor, e.g. it's silently ignored. makes me wonder, why have setlevel
on handler @ all?
it allows finer control. default root logger has warning
level set, means wont print messages lower level(no matter how handlers' levels set!). but, if set root logger's level debug
, indeed message sent log file:
import logging import logging.handlers = logging.getlogger('myapp') a.setlevel(logging.debug) # set root's level h = logging.handlers.rotatingfilehandler('foo.log') h.setlevel(logging.debug) a.addhandler(h) print a.geteffectivelevel() a.debug('foo message') a.warn('warning message')
now, image want add new handler doesn't record debug information. can setting handler logging level:
import logging import logging.handlers = logging.getlogger('myapp') a.setlevel(logging.debug) # set root's level h = logging.handlers.rotatingfilehandler('foo.log') h.setlevel(logging.debug) a.addhandler(h) h2 = logging.handlers.rotatingfilehandler('foo2.log') h2.setlevel(logging.warning) a.addhandler(h2) print a.geteffectivelevel() a.debug('foo message') a.warn('warning message')
now, log file foo.log
contain both messages, while file foo2.log
contain warning message. interested in having log file of error-level messages, add handler
, set level logging.error
, using same logger
.
you may think of logger
logging level global restriction on messages "interesting" given logger and handlers. messages considered logger afterwards sent handlers, perform own filtering , logging process.
Comments
Post a Comment