Initial commit
This commit is contained in:
commit
846160e961
3 changed files with 95 additions and 0 deletions
24
utils/colored_formatter.py
Normal file
24
utils/colored_formatter.py
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
"""Formatter changing logger's record levelname to have colors in the console"""
|
||||||
|
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from .console_color import ConsoleColor
|
||||||
|
|
||||||
|
|
||||||
|
class ColoredFormatter(logging.Formatter):
|
||||||
|
"""Formatter changing the record during format : adds colors to levelname"""
|
||||||
|
def format(self, record):
|
||||||
|
levelno = record.levelno
|
||||||
|
if logging.ERROR == levelno:
|
||||||
|
levelname_color = ConsoleColor.RED + record.levelname + ConsoleColor.ENDCOLOR
|
||||||
|
elif logging.WARNING == levelno:
|
||||||
|
levelname_color = ConsoleColor.ORANGE + record.levelname + ConsoleColor.ENDCOLOR
|
||||||
|
elif logging.INFO == levelno:
|
||||||
|
levelname_color = ConsoleColor.GREEN + record.levelname + ConsoleColor.ENDCOLOR
|
||||||
|
elif logging.DEBUG == levelno:
|
||||||
|
levelname_color = ConsoleColor.BLUE + record.levelname + ConsoleColor.ENDCOLOR
|
||||||
|
else:
|
||||||
|
levelname_color = record.levelname
|
||||||
|
record.levelname = levelname_color
|
||||||
|
return logging.Formatter.format(self, record)
|
||||||
13
utils/console_color.py
Normal file
13
utils/console_color.py
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
"""Colors for termial outputs"""
|
||||||
|
|
||||||
|
|
||||||
|
class ConsoleColor(object):
|
||||||
|
"""Simple shortcut to use colors in console"""
|
||||||
|
HEADER = '\033[95m'
|
||||||
|
BLUE = '\033[94m'
|
||||||
|
GREEN = '\033[92m'
|
||||||
|
ORANGE = '\033[93m'
|
||||||
|
RED = '\033[91m'
|
||||||
|
ENDCOLOR = '\033[0m'
|
||||||
|
BOLD = '\033[1m'
|
||||||
|
UNDERLINE = '\033[4m'
|
||||||
58
utils/logger.py
Normal file
58
utils/logger.py
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
from logging import handlers, StreamHandler
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from .colored_formatter import ColoredFormatter
|
||||||
|
|
||||||
|
|
||||||
|
class DummyLogger():
|
||||||
|
def debug(self, string, *args):
|
||||||
|
print(string, *args)
|
||||||
|
|
||||||
|
def info(self, string, *args):
|
||||||
|
print(string, *args)
|
||||||
|
|
||||||
|
def warn(self, string, *args):
|
||||||
|
print(string, *args)
|
||||||
|
|
||||||
|
def warning(self, string, *args):
|
||||||
|
print(string, *args)
|
||||||
|
|
||||||
|
def error(self, string, *args):
|
||||||
|
print(string, *args)
|
||||||
|
|
||||||
|
def critical(self, string, *args):
|
||||||
|
print(string, *args)
|
||||||
|
|
||||||
|
def fatal(self, string, *args):
|
||||||
|
print(string, *args)
|
||||||
|
|
||||||
|
|
||||||
|
def create_logger(name, log_dir, stdout=False):
|
||||||
|
logger = logging.getLogger(name)
|
||||||
|
os.makedirs(log_dir, exist_ok=True)
|
||||||
|
# Adding a (rotating) file handler to the logging system : outputing in capture.log
|
||||||
|
logger.setLevel(logging.DEBUG)
|
||||||
|
file_log_handler = handlers.RotatingFileHandler(
|
||||||
|
os.path.join(log_dir, name + '.log'),
|
||||||
|
maxBytes=500000,
|
||||||
|
backupCount=5)
|
||||||
|
file_log_handler.setLevel(logging.DEBUG)
|
||||||
|
log_formatter = logging.Formatter('%(asctime)s %(levelname)s : %(message)s')
|
||||||
|
file_log_handler.setFormatter(log_formatter)
|
||||||
|
logger.addHandler(file_log_handler)
|
||||||
|
|
||||||
|
if stdout:
|
||||||
|
# Adding an handler to the logging system (default has none) : outputing in stdout
|
||||||
|
terminal_log_handler = StreamHandler(sys.stdout)
|
||||||
|
terminal_log_handler.setLevel(logging.DEBUG)
|
||||||
|
if os.name != 'nt':
|
||||||
|
# Fancy color for non windows console
|
||||||
|
colored_log_formatter = ColoredFormatter('%(asctime)s %(levelname)s : %(message)s')
|
||||||
|
terminal_log_handler.setFormatter(colored_log_formatter)
|
||||||
|
else:
|
||||||
|
terminal_log_handler.setFormatter(log_formatter)
|
||||||
|
logger.addHandler(terminal_log_handler)
|
||||||
|
|
||||||
|
return logger
|
||||||
Loading…
Add table
Add a link
Reference in a new issue