Last active
April 6, 2023 19:12
-
-
Save drewyangdev/1d1e72f3056e953514f98c7a44406cdb to your computer and use it in GitHub Desktop.
Python logger template
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 os | |
import sys | |
import io | |
import logging | |
import logging.handlers | |
from distutils.util import strtobool | |
# get logger level | |
DEBUG = strtobool(os.getenv("DEBUG", "True")) | |
# set logger config | |
logging.basicConfig( | |
format="[%(asctime)s] %(levelname)s in %(module)s: %(message)s", | |
handlers=[ | |
## log in console | |
logging.StreamHandler(sys.stdout), | |
## log in rotating file | |
## only file log: python app.py > /dev/null 2>&1 & | |
logging.handlers.RotatingFileHandler( | |
"debug.log", maxBytes=1024 * 1024 * 10, backupCount=5, encoding="utf-8" | |
), | |
], | |
) | |
# set logger level | |
logger = logging.getLogger(__name__) | |
if DEBUG: | |
logger.setLevel(logging.DEBUG) | |
else: | |
logger.setLevel(logging.INFO) | |
# handle unexpected execption | |
def excepthook(exc_type, exc_value, exc_traceback): | |
if issubclass(exc_type, KeyboardInterrupt): | |
sys.__excepthook__(exc_type, exc_value, exc_traceback) | |
return | |
if logger.getEffectiveLevel() == logging.DEBUG: | |
logger.error( | |
"Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback) | |
) | |
else: | |
logger.error(f"Uncaught exception: {exc_value}") | |
sys.excepthook = excepthook | |
# handle tqdm progress bar | |
# https://github.com/tqdm/tqdm/issues/313#issuecomment-267959111 | |
# with tqdm.tqdm( | |
# total=summary.size, | |
# unit="B", | |
# unit_scale=True, | |
# desc=summary.key, | |
# file=TqdmToLogger(logger, level=logging.INFO) | |
# ) as pbar: | |
class TqdmToLogger(io.StringIO): | |
""" | |
Output stream for TQDM which will output to logger module instead of | |
the StdOut. | |
""" | |
logger = None | |
level = None | |
buf = "" | |
def __init__(self, logger, level=None): | |
super(TqdmToLogger, self).__init__() | |
self.logger = logger | |
self.level = level or logging.INFO | |
def write(self, buf): | |
self.buf = buf.strip("\r\n\t ") | |
def flush(self): | |
self.logger.log(self.level, self.buf) | |
logger.debug("This is DEBUG") | |
logger.info("This is INFO") | |
logger.warning("This is WARNING") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment