Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import logging
from colorlog import ColoredFormatter
class ColorLogger:
def __init__(self):
"""Return a logger with a ColoredFormatter."""
self.formatter = ColoredFormatter(
"%(log_color)s%(levelname)s:%(name)s:%(funcName)s(%(lineno)s):%(message)s",
datefmt=None,
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
}
)
logger = logging.getLogger()
self.root_logger = logger
self.setStreamHandler()
def setStreamHandler(self):
handler = logging.StreamHandler()
handler.setFormatter(self.formatter)
self.root_logger.addHandler(handler)
def setFileHandler(self, filename="log", log_level=10):
handler = logging.FileHandler(filename=filename)
handler.setFormatter(self.formatter)
handler.setLevel(log_level)
self.root_logger.addHandler(handler)
def setLevel(self, log_level):
self.root_logger.setLevel(log_level)
return self.root_logger
def __call__(self, name):
return self.root_logger.getChild(name)
getLogger = ColorLogger()
setLevel = getLogger.setLevel
setLogFile = getLogger.setFileHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment