Finding source of format errors when using python logging

When I have many different modules using the standard python logging module, the following stack trace helps me a little to find out exactly where I had a poorly formed log statement:

Traceback (most recent call last): File "/usr/lib/python2.6/logging/__init__.py", line 768, in emit msg = self.format(record) File "/usr/lib/python2.6/logging/__init__.py", line 648, in format return fmt.format(record) File "/usr/lib/python2.6/logging/__init__.py", line 436, in format record.message = record.getMessage() File "/usr/lib/python2.6/logging/__init__.py", line 306, in getMessage msg = msg % self.args TypeError: not all arguments converted during string formatting 

I'm just starting to use the python registration module, so maybe I'm missing something obvious. I am not sure if the stack trace is useless because I use potions, or if this is normal for the logging module, but any help would be appreciated. I would like to change the source, anything so that the protocol library actually makes it clear where the problem is.

+7
source share
5 answers

The logging module is designed to prevent bad log messages from killing the rest of the code, so the emit method catches errors and passes them to the handleError method. The easiest way would be to temporarily edit /usr/lib/python2.6/logging/__init__.py and find handleError . It looks something like this:

 def handleError(self, record): """ Handle errors which occur during an emit() call. This method should be called from handlers when an exception is encountered during an emit() call. If raiseExceptions is false, exceptions get silently ignored. This is what is mostly wanted for a logging system - most users will not care about errors in the logging system, they are more interested in application errors. You could, however, replace this with a custom handler if you wish. The record which was being processed is passed in to this method. """ if raiseExceptions: ei = sys.exc_info() try: traceback.print_exception(ei[0], ei[1], ei[2], None, sys.stderr) sys.stderr.write('Logged from file %s, line %s\n' % ( record.filename, record.lineno)) except IOError: pass # see issue 5971 finally: del ei 

Now temporarily edit it. Inserting a simple raise at the beginning should ensure that the error is passed by your code instead of swallowing. Once you have fixed the problem, simply return the logging code to what it was.

+5
source

Instead of editing the installed Python code, you may also find errors like this:

  def handleError(record): raise RuntimeError(record) handler.handleError = handleError 

where the handler is one of the handlers that pose the problem. Now that a format error occurs, you will see the location.

+6
source

This is not an answer to the question, but I hope it will be other newcomers with the registration module, like me.

My problem was that I replaced all print occurrences with logging.info, so a valid line of type print('a',a) became logging.info('a',a) (but it should be logging.info('a %s'%a) ).

This was also outlined in How to log logging errors? but this is not reflected in the study

+3
source

Alternatively, you can create your own formatter, but then you should include it everywhere.

 class DebugFormatter(logging.Formatter): def format(self, record): try: return super(DebugFormatter, self).format(record) except: print "Unable to format record" print "record.filename ", record.filename print "record.lineno ", record.lineno print "record.msg ", record.msg print "record.args: ",record.args raise FORMAT = '%(levelname)s %(filename)s:%(lineno)d %(message)s' formatter = DebugFormatter(FORMAT) handler = logging.StreamHandler() handler.setLevel(logging.DEBUG) handler.setFormatter(formatter) logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) logger.addHandler(handler) 
+1
source

There was the same problem, such a trace occurs due to an incorrect format name. Therefore, when creating a format for a log file, check the format name once in the python documentation: " https://docs.python.org/3/library/logging.html#formatter-objects "

0
source

All Articles