|
import logging |
|
import os |
|
import colorlog |
|
|
|
|
|
def _create_logger(): |
|
""" |
|
Create a logger with colorized output |
|
Usage: LOG_LEVEL=DEBUG python <script.py> |
|
""" |
|
|
|
handler = colorlog.StreamHandler() |
|
fmt = "%(log_color)s%(levelname)-8s%(reset)s [%(filename)s:%(lineno)d] %(message)s" |
|
handler.setFormatter( |
|
colorlog.ColoredFormatter( |
|
fmt=fmt, |
|
log_colors={ |
|
"DEBUG": "blue", |
|
"INFO": "green", |
|
"WARNING": "yellow", |
|
"ERROR": "red", |
|
"CRITICAL": "red", |
|
}, |
|
) |
|
) |
|
|
|
log_level = os.getenv("LOG_LEVEL", "WARNING").upper() |
|
logger = colorlog.getLogger(__package__) |
|
logger.setLevel(level=getattr(logging, log_level, logging.WARNING)) |
|
|
|
logger.addHandler(handler) |
|
return logger |
|
|
|
|
|
log = _create_logger() |
|
|