Skip to content

Instantly share code, notes, and snippets.

@wolever
Created September 14, 2014 21:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolever/ecb9800afefbbc830b46 to your computer and use it in GitHub Desktop.
Save wolever/ecb9800afefbbc830b46 to your computer and use it in GitHub Desktop.
Monkey patch for logging.LogRecord.getMessage so it will never, ever raise an exception
def monkeypatch_logging_getMessage():
""" Monkey patch logging.LogRecord.getMessage so it will never, ever raise an exception. """
from unstdlib import to_str
from logging import LogRecord
oldGetMessage = LogRecord.getMessage
def getMessage(self):
try:
return oldGetMessage(self)
except Exception as e:
pass
try:
if not self.args:
return to_str(self.msg)
return to_str("%s %%%r" %(self.msg, self.args))
except Exception as e:
pass
try:
return to_str("%s (error including args: %r)" %(self.msg, e))
except Exception as e:
pass
return "(error formatting message: %r)" %(e, )
LogRecord.getMessage = getMessage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment