add better multiline logging and fix json format issues

This commit is contained in:
Robert Kaussow 2019-04-05 12:23:18 +02:00
parent 554d957af3
commit 397c92a27b
3 changed files with 50 additions and 23 deletions

View File

@ -113,6 +113,9 @@ class Candidate(object):
labels = {"tag": "review", "standard": standard.name, "file": self.path, "passed": True}
if standard.id and standard.id.strip():
labels["id"] = standard.id
for err in [err for err in result.errors
if not err.lineno or utils.is_line_in_ranges(err.lineno, utils.lines_ranges(lines))]:
err_labels = copy.copy(labels)
@ -122,16 +125,22 @@ class Candidate(object):
if not standard.version:
LOG.warn("{id}Best practice '{name}' not met:\n{path}:{error}".format(
id=standard.id, name=standard.name, path=self.path, error=err),
extra=flag_extra(err_labels))
id=self._format_id(standard.id),
name=standard.name,
path=self.path,
error=err), extra=flag_extra(err_labels))
elif LooseVersion(standard.version) > LooseVersion(self.version):
LOG.warn("{id}Future standard '{name}' not met:\n{path}:{error}".format(
id=standard.id, name=standard.name, path=self.path, error=err),
extra=flag_extra(err_labels))
id=self._format_id(standard.id),
name=standard.name,
path=self.path,
error=err), extra=flag_extra(err_labels))
else:
LOG.error("{id}Standard '{name}' not met:\n{path}:{error}".format(
id=standard.id, name=standard.name, path=self.path, error=err),
extra=flag_extra(err_labels))
id=self._format_id(standard.id),
name=standard.name,
path=self.path,
error=err), extra=flag_extra(err_labels))
errors = errors + 1
if not result.errors:
if not standard.version:
@ -143,6 +152,12 @@ class Candidate(object):
return errors
def _format_id(self, standard_id):
if standard_id and standard_id.strip():
standard_id = "[{id}] ".format(id=standard_id.strip())
return standard_id
def __repr__(self): # noqa
return "%s (%s)" % (type(self).__name__, self.path)

View File

@ -25,12 +25,12 @@ def _should_do_markup():
colorama.init(autoreset=True, strip=not _should_do_markup())
def flag_extra(kwargs):
"""Ensure kwargs not conflict with the logging module."""
assert isinstance(kwargs, dict)
def flag_extra(extra):
"""Ensure extra args are prefixed."""
assert isinstance(extra, dict)
flagged = dict()
for key, value in iteritems(kwargs):
for key, value in iteritems(extra):
flagged["later_" + key] = value
return flagged
@ -54,6 +54,22 @@ class LogFilter(object):
return logRecord.levelno <= self.__level
class MultilineFormatter(logging.Formatter):
"""Logging Formatter to reset color after newline characters."""
def format(self, record):
record.msg = record.msg.replace("\n", "\n{}... ".format(colorama.Style.RESET_ALL))
return logging.Formatter.format(self, record)
class MultilineJsonFormatter(jsonlogger.JsonFormatter):
"""Logging Formatter to remove newline characters."""
def format(self, record):
record.msg = record.msg.replace("\n", " ")
return jsonlogger.JsonFormatter.format(self, record)
def get_logger(name=None, level=logging.DEBUG, json=False):
"""
Build a logger with the given name and returns the logger.
@ -91,10 +107,10 @@ def _get_error_handler(json=False):
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.ERROR)
handler.addFilter(LogFilter(logging.ERROR))
handler.setFormatter(logging.Formatter(error(CONSOLE_FORMAT)))
handler.setFormatter(MultilineFormatter(error(CONSOLE_FORMAT)))
if json:
handler.setFormatter(jsonlogger.JsonFormatter(JSON_FORMAT))
handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT))
return handler
@ -103,10 +119,10 @@ def _get_warn_handler(json=False):
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.WARN)
handler.addFilter(LogFilter(logging.WARN))
handler.setFormatter(logging.Formatter(warn(CONSOLE_FORMAT)))
handler.setFormatter(MultilineFormatter(warn(CONSOLE_FORMAT)))
if json:
handler.setFormatter(jsonlogger.JsonFormatter(JSON_FORMAT))
handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT))
return handler
@ -115,10 +131,10 @@ def _get_info_handler(json=False):
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.INFO)
handler.addFilter(LogFilter(logging.INFO))
handler.setFormatter(logging.Formatter(info("%(message)s")))
handler.setFormatter(MultilineFormatter(info("%(message)s")))
if json:
handler.setFormatter(jsonlogger.JsonFormatter(JSON_FORMAT))
handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT))
return handler
@ -127,10 +143,10 @@ def _get_critical_handler(json=False):
handler = logging.StreamHandler(sys.stderr)
handler.setLevel(logging.CRITICAL)
handler.addFilter(LogFilter(logging.CRITICAL))
handler.setFormatter(logging.Formatter(critical(CONSOLE_FORMAT)))
handler.setFormatter(MultilineFormatter(critical(CONSOLE_FORMAT)))
if json:
handler.setFormatter(jsonlogger.JsonFormatter(JSON_FORMAT))
handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT))
return handler
@ -164,4 +180,5 @@ def color_text(color, msg):
:returns: string
"""
return "{}{}{}".format(color, msg, colorama.Style.RESET_ALL)

View File

@ -16,11 +16,6 @@ class Standard(object):
:param standard_dict: Dictionary object containing all neseccary attributes
"""
# if "id" not in standard_dict:
# standard_dict.update(id="")
# else:
# standard_dict.update(id="[{}] ".format(standard_dict.get("id")))
self.id = standard_dict.get("id", "")
self.name = standard_dict.get("name")
self.version = standard_dict.get("version")