python - LogRecord does not have expected fields -
in python using "logging" module, documentation promises logrecord instances have number of attributes, explicitly listed in documentation.
however, appears not true. when not use logging module's 'basicconfig()' method, program below shows attributes 'asctime' , 'message' not present in logrecords passed loghandler's 'emit' method.
import logging class logginghandler(logging.handler): def __init__(self): logging.handler.__init__(self) def emit(self, record): assert isinstance(record, logging.logrecord) print("logginghandler received logrecord: {}".format(record)) # list of logrecord attributes expected when reading # documentation of logging module: expected_attributes = \ "args,asctime,created,exc_info,filename,funcname,levelname," \ "levelno,lineno,module,msecs,message,msg,name,pathname," \ "process,processname,relativecreated,stack_info,thread,threadname" ea in expected_attributes.split(","): if not hasattr(record, ea): print("unexpected: logrecord not have '{}' field!".format(ea)) logginghandler = logginghandler() rootlogger = logging.getlogger() rootlogger.addhandler(logginghandler) # emit warning message logging.warning("warning message")
running on python 3 gives:
$python3 test_logging.py logginghandler received logrecord: <logrecord: root, 30, test_logging.py, 28, "warning message"> unexpected: logrecord not have 'asctime' field! unexpected: logrecord not have 'message' field!
what going on here? did misunderstand documentation? needs done make sure logrecord instances have 'asctime' , 'message' attributes promised?
it responsibility of formatter
set asctime
, message
prior calling self.format(record)
, attributes undefined. doc of format
method:
the record’s attribute dictionary used operand string formatting operation. returns resulting string. before formatting dictionary, couple of preparatory steps carried out. message attribute of record computed using msg % args. if formatting string contains '(asctime)', formattime() called format event time.
since example code not call self.format(record)
therefore expected behaviour attributes undefined.
to have message
, asctime
set, must first call self.format(record)
inside emit
method. please try
import logging class logginghandler(logging.handler): def emit(self, record): assert isinstance(record, logging.logrecord) print("logginghandler received logrecord: {}".format(record)) self.format(record) # list of logrecord attributes expected when reading # documentation of logging module: expected_attributes = \ "args,asctime,created,exc_info,filename,funcname,levelname," \ "levelno,lineno,module,msecs,message,msg,name,pathname," \ "process,processname,relativecreated,stack_info,thread,threadname" ea in expected_attributes.split(","): if not hasattr(record, ea): print("unexpected: logrecord not have '{}' field!".format(ea)) formatter = logging.formatter("%(asctime)s") logginghandler = logginghandler() logginghandler.setformatter(formatter) rootlogger = logging.getlogger() rootlogger.addhandler(logginghandler) # emit warning message logging.warning("warning message")
Comments
Post a Comment