Skip to content

Instantly share code, notes, and snippets.

@cmarinbe
Created March 16, 2016 15:55
Show Gist options
  • Save cmarinbe/e7005d66bc4e7b1b39bc to your computer and use it in GitHub Desktop.
Save cmarinbe/e7005d66bc4e7b1b39bc to your computer and use it in GitHub Desktop.
Configuring python logging with colorlog
#!/usr/bin/env python
# =============================================================================
# @file logging_color.py
# @author C. Marin Benito ([email protected])
# @date 16.03.2016
# =============================================================================
"""
This functions configures a logger using logging and colorlog
and returns the logger for further usage
By default, time, name of the logger and message with the default
colorlog color scheme are printed.
The threshold level for displaying messages and the format of the
logger can be configured with the lvl and format arguments
"""
import logging
import colorlog
def conf_log(name, lvl=logging.DEBUG, format=None):
if not format:
format = " %(asctime)s - %(name)s | %(log_color)s%(levelname)-8s%(reset)s | %(log_color)s%(message)s%(reset)s"
logging.root.setLevel(lvl)
formatter = colorlog.ColoredFormatter(format)
stream = logging.StreamHandler()
stream.setLevel(lvl)
stream.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(lvl)
logger.addHandler(stream)
return logger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment