Skip to content

Instantly share code, notes, and snippets.

@mbrengel
Last active January 17, 2024 11:30
Show Gist options
  • Save mbrengel/c823baa45ed21dce86e8b8321c804bcf to your computer and use it in GitHub Desktop.
Save mbrengel/c823baa45ed21dce86e8b8321c804bcf to your computer and use it in GitHub Desktop.
Python Logging Setup
#!/usr/bin/env python3
import logging
from logging.handlers import RotatingFileHandler
import sys
import colorama
def configure_logging():
# enable cross-platform colored output
colorama.init()
# get the root logger and make it verbose
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# this allows us to set an upper threshold for the log levels since the
# setLevel method only sets a lower one
class UpperThresholdFilter(logging.Filter):
def __init__(self, threshold, *args, **kwargs):
self._threshold = threshold
super(UpperThresholdFilter, self).__init__(*args, **kwargs)
def filter(self, rec):
return rec.levelno <= self._threshold
# use colored output and use different colors for different levels
class ColorFormatter(logging.Formatter):
def __init__(self, colorfmt, *args, **kwargs):
self._colorfmt = colorfmt
super(ColorFormatter, self).__init__(*args, **kwargs)
def format(self, record):
if record.levelno == logging.INFO:
color = colorama.Fore.GREEN
elif record.levelno == logging.WARNING:
color = colorama.Fore.YELLOW
elif record.levelno == logging.ERROR:
color = colorama.Fore.RED
elif record.levelno == logging.DEBUG:
color = colorama.Fore.CYAN
else:
color = ""
self._style._fmt = self._colorfmt.format(color, colorama.Style.RESET_ALL)
return logging.Formatter.format(self, record)
# configure formatter
logfmt = "{}[%(asctime)s|%(levelname).3s]{} %(message)s"
formatter = ColorFormatter(logfmt)
# configure stdout handler
stdouthandler = logging.StreamHandler(sys.stdout)
stdouthandler.setLevel(logging.DEBUG)
stdouthandler.addFilter(UpperThresholdFilter(logging.INFO))
stdouthandler.setFormatter(formatter)
logger.addHandler(stdouthandler)
# configure stderr handler
stderrhandler = logging.StreamHandler(sys.stderr)
stderrhandler.setLevel(logging.WARNING)
stderrhandler.setFormatter(formatter)
logger.addHandler(stderrhandler)
# configure file handler (no colored messages here)
filehandler = RotatingFileHandler("test.log", maxBytes=1024 * 1024 * 100, backupCount=5)
filehandler.setLevel(logging.DEBUG)
filehandler.setFormatter(logging.Formatter(logfmt.format("", "")))
logger.addHandler(filehandler)
def main(argv):
configure_logging()
logging.debug("This is a debug message")
logging.info("This is an info message")
logging.warning("This is a warning message")
logging.error("This is an error message")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment