Created
July 5, 2016 19:26
-
-
Save jorgenschaefer/50c0f8f42c8bfb046da1e26de51f0ada to your computer and use it in GitHub Desktop.
Log a record with and without exception information
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!