Skip to content

Instantly share code, notes, and snippets.

@jorgenschaefer
Created July 5, 2016 19:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgenschaefer/50c0f8f42c8bfb046da1e26de51f0ada to your computer and use it in GitHub Desktop.
Save jorgenschaefer/50c0f8f42c8bfb046da1e26de51f0ada to your computer and use it in GitHub Desktop.
Log a record with and without exception information
import logging
class NoExceptionsFormatter(logging.Formatter):
def format(self, record):
record.message = record.getMessage()
if self.usesTime():
record.asctime = self.formatTime(record, self.datefmt)
return self._fmt % record.__dict__
with_exceptions = logging.StreamHandler()
with_exceptions.setFormatter(
logging.Formatter("With exceptions: %(message)s")
)
without_exceptions = logging.StreamHandler()
without_exceptions.setFormatter(
NoExceptionsFormatter("WITHOUT exceptions: %(message)s")
)
root = logging.getLogger()
root.addHandler(with_exceptions)
root.addHandler(without_exceptions)
log = logging.getLogger("some.logger")
try:
0/0
except:
log.exception("This is an error")
@montaro
Copy link

montaro commented Oct 10, 2016

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment