Thursday, 15 May 2014

python 3.x - Modifying logging message format based on message logging level in Python3 -



python 3.x - Modifying logging message format based on message logging level in Python3 -

i asked question python 2 here, bumped issue 1 time again when the reply no longer worked python 3.2.3.

here's code works on python 2.7.3:

import logging # effort set python3 logger print custom messages # based on each message's logging level. # technique recommended python2 not appear work # python3 class customconsoleformatter(logging.formatter): """ modify way debug messages displayed. """ def __init__(self, fmt="%(levelno)d: %(msg)s"): logging.formatter.__init__(self, fmt=fmt) def format(self, record): # remember original format format_orig = self._fmt if record.levelno == logging.debug: self._fmt = "debug: %(msg)s" # phone call original formatter grunt work result = logging.formatter.format(self, record) # restore original format self._fmt = format_orig homecoming result # set logger my_logger = logging.getlogger("my_custom_logger") my_logger.setlevel(logging.debug) my_formatter = customconsoleformatter() console_handler = logging.streamhandler() console_handler.setformatter(my_formatter) my_logger.addhandler(console_handler) my_logger.debug("this debug-level message") my_logger.info("this info-level message")

a run using python 2.7.3:

tcsh-16: python demo_python_2.7.3.py debug: debug-level message 20: info-level message

as far can tell, conversion python3 requires slight mod customconsoleformatter.init():

def __init__(self): super().__init__(fmt="%(levelno)d: %(msg)s", datefmt=none, style='%')

on python 3.2.3:

tcsh-26: python3 demo_python_3.2.3.py 10: debug-level message 20: info-level message

can see, want replace '10' 'debug' beingness thwarted.

i've tried digging around in python3 source , looks percentstyle instantiation clobbering self._fmt after i, well, clobber myself.

my logging chops stop short of beingness able work around wrinkle.

can recommend way or perhaps point out i'm overlooking?

with bit of digging, able modify python 2 solution work python 3. in python2, necessary temporarily overwrite formatter._fmt. in python3, back upwards multiple format string types requires temporarily overwrite formatter._style._fmt instead.

# custom formatter class myformatter(logging.formatter): err_fmt = "error: %(msg)s" dbg_fmt = "dbg: %(module)s: %(lineno)d: %(msg)s" info_fmt = "%(msg)s" def __init__(self): super().__init__(fmt="%(levelno)d: %(msg)s", datefmt=none, style='%') def format(self, record): # save original format configured user # when logger formatter instantiated format_orig = self._style._fmt # replace original format 1 customized logging level if record.levelno == logging.debug: self._style._fmt = myformatter.dbg_fmt elif record.levelno == logging.info: self._style._fmt = myformatter.info_fmt elif record.levelno == logging.error: self._style._fmt = myformatter.err_fmt # phone call original formatter class grunt work result = logging.formatter.format(self, record) # restore original format configured user self._style._fmt = format_orig homecoming result

and here halloleo's illustration of how utilize above in script (from python2 version of question):

fmt = myformatter() hdlr = logging.streamhandler(sys.stdout) hdlr.setformatter(fmt) logging.root.addhandler(hdlr) logging.root.setlevel(debug)

logging python-3.x

No comments:

Post a Comment