From 2379f6963fb8f5202928145ca0d61aa8ec599c03 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Wed, 5 Dec 2018 02:07:34 -0400 Subject: [PATCH] Common: Add colorized log formatter New log formatter that color the output depending on the log level of the message. The default palette handles: INFO, SUCCESS, ERROR, DEBUG and CRITICAL. Signed-off-by: Roberto Rosario --- mayan/apps/common/apps.py | 3 ++- mayan/apps/common/log.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 mayan/apps/common/log.py diff --git a/mayan/apps/common/apps.py b/mayan/apps/common/apps.py index 71e86aaf6a..79d0fcb7ab 100644 --- a/mayan/apps/common/apps.py +++ b/mayan/apps/common/apps.py @@ -191,7 +191,8 @@ class CommonApp(MayanAppConfig): 'disable_existing_loggers': False, 'formatters': { 'intermediate': { - 'format': '%(name)s <%(process)d> [%(levelname)s] "%(funcName)s() line %(lineno)d %(message)s"' + '()': 'common.log.ColorFormatter', + 'format': '%(name)s <%(process)d> [%(levelname)s] "%(funcName)s() line %(lineno)d %(message)s"', }, 'logfile': { 'format': '%(asctime)s %(name)s <%(process)d> [%(levelname)s] "%(funcName)s() line %(lineno)d %(message)s"' diff --git a/mayan/apps/common/log.py b/mayan/apps/common/log.py new file mode 100644 index 0000000000..6aaaa44a1b --- /dev/null +++ b/mayan/apps/common/log.py @@ -0,0 +1,22 @@ +from __future__ import unicode_literals + +import logging + +from django.utils.termcolors import colorize + +PALETTE = { + 'CRITICAL': {'fg': 'red', 'opts': ('bold', 'blink', 'reverse')}, + 'DEBUG': {'fg': 'cyan'}, + 'ERROR': {'fg': 'red', 'opts': ('bold',)}, + 'INFO': {'fg': 'white'}, + 'SUCCESS': {'fg': 'green'}, + 'WARNING': {'fg': 'yellow', 'opts': ('bold', 'underscore')}, +} + + +class ColorFormatter(logging.Formatter): + def format(self, record): + record.msg = colorize( + text=record.msg, **PALETTE.get(record.levelname, {}) + ) + return super(ColorFormatter, self).format(record)