implement custom logging formatter

supports custom log message formats for each loglevel and, by
extension, custom ANSI codes and colors for errors and warnings

(#304)
This commit is contained in:
Mike Fährmann
2019-06-21 19:56:09 +02:00
parent 70713f0f28
commit 15e4ddf46d
4 changed files with 55 additions and 16 deletions

View File

@@ -1518,7 +1518,7 @@ Logging Configuration
=========== ===== =========== =====
Type ``object`` Type ``object``
Example .. code:: Examples .. code::
{ {
"format": "{asctime} {name}: {message}", "format": "{asctime} {name}: {message}",
@@ -1527,10 +1527,21 @@ Example .. code::
"encoding": "ascii" "encoding": "ascii"
} }
{
"level": "debug",
"format": {
"debug" : "debug: {message}",
"info" : "[{name}] {message}",
"warning": "Warning: {message}",
"error" : "ERROR: {message}"
}
}
Description Extended logging output configuration. Description Extended logging output configuration.
* format * format
* Format string for logging messages * General format string for logging messages
or a dictionary with format strings for each loglevel.
In addition to the default In addition to the default
`LogRecord attributes <https://docs.python.org/3/library/logging.html#logrecord-attributes>`__, `LogRecord attributes <https://docs.python.org/3/library/logging.html#logrecord-attributes>`__,

View File

@@ -148,8 +148,13 @@
{ {
"mode": "terminal", "mode": "terminal",
"log": { "log": {
"format": "{name}: {message}", "level": "info",
"level": "info" "format": {
"debug" : "\u001b[0;37m{name}: {message}\u001b[0m",
"info" : "\u001b[1;37m{name}: {message}\u001b[0m",
"warning": "\u001b[1;33m{name}: {message}\u001b[0m",
"error" : "\u001b[1;31m{name}: {message}\u001b[0m"
}
}, },
"logfile": { "logfile": {
"path": "~/gallery-dl/log.txt", "path": "~/gallery-dl/log.txt",

View File

@@ -175,6 +175,7 @@
"mode": "auto", "mode": "auto",
"progress": true, "progress": true,
"shorten": true, "shorten": true,
"log": "[{name}][{levelname}] {message}",
"logfile": null, "logfile": null,
"unsupportedfile": null "unsupportedfile": null
}, },

View File

@@ -35,6 +35,30 @@ class Logger(logging.Logger):
return rv return rv
class Formatter(logging.Formatter):
"""Custom formatter that supports different formats per loglevel"""
def __init__(self, fmt, datefmt):
if not isinstance(fmt, dict):
fmt = {"debug": fmt, "info": fmt, "warning": fmt, "error": fmt}
self.formats = fmt
self.datefmt = datefmt
def format(self, record):
record.message = record.getMessage()
fmt = self.formats[record.levelname]
if "{asctime" in fmt:
record.asctime = self.formatTime(record, self.datefmt)
msg = fmt.format_map(record.__dict__)
if record.exc_info and not record.exc_text:
record.exc_text = self.formatException(record.exc_info)
if record.exc_text:
msg = msg + "\n" + record.exc_text
if record.stack_info:
msg = msg + "\n" + record.stack_info
return msg
def initialize_logging(loglevel): def initialize_logging(loglevel):
"""Setup basic logging functionality before configfiles have been loaded""" """Setup basic logging functionality before configfiles have been loaded"""
# convert levelnames to lowercase # convert levelnames to lowercase
@@ -46,7 +70,7 @@ def initialize_logging(loglevel):
logging.Logger.manager.setLoggerClass(Logger) logging.Logger.manager.setLoggerClass(Logger)
# setup basic logging to stderr # setup basic logging to stderr
formatter = logging.Formatter(LOG_FORMAT, LOG_FORMAT_DATE, "{") formatter = Formatter(LOG_FORMAT, LOG_FORMAT_DATE)
handler = logging.StreamHandler() handler = logging.StreamHandler()
handler.setFormatter(formatter) handler.setFormatter(formatter)
handler.setLevel(loglevel) handler.setLevel(loglevel)
@@ -80,13 +104,11 @@ def setup_logging_handler(key, fmt=LOG_FORMAT, lvl=LOG_LEVEL):
"%s: missing or invalid path (%s)", key, exc) "%s: missing or invalid path (%s)", key, exc)
return None return None
level = opts.get("level", lvl) handler.setLevel(opts.get("level", lvl))
logfmt = opts.get("format", fmt) handler.setFormatter(Formatter(
datefmt = opts.get("format-date", LOG_FORMAT_DATE) opts.get("format", fmt),
formatter = logging.Formatter(logfmt, datefmt, "{") opts.get("format-date", LOG_FORMAT_DATE),
handler.setFormatter(formatter) ))
handler.setLevel(level)
return handler return handler
@@ -100,10 +122,10 @@ def configure_logging_handler(key, handler):
if handler.level == LOG_LEVEL and "level" in opts: if handler.level == LOG_LEVEL and "level" in opts:
handler.setLevel(opts["level"]) handler.setLevel(opts["level"])
if "format" in opts or "format-date" in opts: if "format" in opts or "format-date" in opts:
logfmt = opts.get("format", LOG_FORMAT) handler.setFormatter(Formatter(
datefmt = opts.get("format-date", LOG_FORMAT_DATE) opts.get("format", LOG_FORMAT),
formatter = logging.Formatter(logfmt, datefmt, "{") opts.get("format-date", LOG_FORMAT_DATE),
handler.setFormatter(formatter) ))
# -------------------------------------------------------------------- # --------------------------------------------------------------------