Skip to content

Instantly share code, notes, and snippets.

@cliffxuan
Created October 10, 2017 13:38
Show Gist options
  • Save cliffxuan/a4979d927126f5572b3a2306c6a652f6 to your computer and use it in GitHub Desktop.
Save cliffxuan/a4979d927126f5572b3a2306c6a652f6 to your computer and use it in GitHub Desktop.
import logging
from contextlib import ContextDecorator
format = '%(asctime)s - %(levelname)s - %(message)s'
ch = logging.StreamHandler()
ch.setFormatter(logging.Formatter(format))
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.addHandler(ch)
class context_decorator(ContextDecorator):
def __enter__(self):
return self
def __exit__(self, *exc):
if exc != (None, None, None):
logging.info("***********************start")
logging.exception("Uncaught exception: %s", exc[1])
logging.info("***********************end")
return False
with context_decorator():
1 / 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment