Skip to content

Instantly share code, notes, and snippets.

@kespindler
Created January 11, 2014 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kespindler/8373568 to your computer and use it in GitHub Desktop.
Save kespindler/8373568 to your computer and use it in GitHub Desktop.
Monkey-patching logging
import logging
def getMessage(self):
"""
Return the message for this LogRecord.
Return the message for this LogRecord after merging any user-supplied
arguments with the message.
"""
if not logging._unicode: #if no unicode support...
msg = str(self.msg)
else:
msg = self.msg
if not isinstance(msg, basestring):
try:
msg = str(self.msg)
except UnicodeError:
msg = self.msg #Defer encoding till later
if self.args:
msg = msg.format(*self.args)
return msg
logging.LogRecord.getMessage = getMessage
logging.error("hello {}", "world")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment