add some details to json logs

This commit is contained in:
Robert Kaussow 2019-04-03 23:39:27 +02:00
parent 4f33afff3e
commit fa14538f20
3 changed files with 26 additions and 18 deletions

View File

@ -12,6 +12,7 @@ import ansible
from ansiblelater import LOG
from ansiblelater import utils
from ansiblelater.logger import flag_extra
from ansiblelater.command.review import Error
from ansiblelater.exceptions import ( # noqa
LaterError, LaterAnsibleError
@ -127,19 +128,19 @@ 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=err_labels)
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=err_labels)
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=err_labels)
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:
LOG.info("Best practice '%s' met" % standard.name, extra=labels)
LOG.info("Best practice '%s' met" % standard.name, extra=flag_extra(labels))
elif LooseVersion(standard.version) > LooseVersion(self.version):
LOG.info("Future standard '%s' met" % standard.name, extra=labels)
LOG.info("Future standard '%s' met" % standard.name, extra=flag_extra(labels))
else:
LOG.info("Standard '%s' met" % standard.name)

View File

@ -5,11 +5,12 @@ import os
import sys
import colorama
from six import iteritems
from ansible.module_utils.parsing.convert_bool import boolean as to_bool
from pythonjsonlogger import jsonlogger
CONSOLE_FORMAT = "%(levelname)s: %(message)s"
JSON_FORMAT = "(levelname) (message) (asctime)"
JSON_FORMAT = "(levelname) (asctime)"
def _should_do_markup():
@ -23,18 +24,23 @@ def _should_do_markup():
colorama.init(autoreset=True, strip=not _should_do_markup())
clashing_keywords = {key for key in dir(logging.LogRecord(None, None, "", 0, "", (), None, None)) if "__" not in key}
additional_clashing_keywords = {
"message",
"asctime"
}
clashing_keywords = clashing_keywords.union(additional_clashing_keywords)
def OverwriteMakeRecord(self, name, level, fn, lno, msg, args, exc_info, func=None, extra=None):
"""
A factory method which can be overridden in subclasses to create
specialized LogRecords.
"""
rv = logging.LogRecord(name, level, fn, lno, msg, args, exc_info, func)
if extra is not None:
for key in extra:
rv.__dict__[key] = extra[key]
print("xxx", rv.__dict__)
return rv
def flag_extra(kwargs):
"""Ensure kwargs not conflict with the logging module."""
assert isinstance(kwargs, dict)
flagged = dict()
for key, value in iteritems(kwargs):
flagged["later_" + key] = value
return flagged
class LogFilter(object):
@ -66,7 +72,6 @@ def get_logger(name=None, level=logging.DEBUG, json=False):
"""
logger = logging.getLogger(name)
logger.makeRecord(OverwriteMakeRecord)
logger.setLevel(level)
logger.addHandler(_get_error_handler(json=json))
logger.addHandler(_get_warn_handler(json=json))

View File

@ -490,6 +490,8 @@ def parse_yaml_linenumbers(data, filename):
data = loader.get_single_data()
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as e:
raise LaterError("syntax error", e)
except (yaml.composer.ComposerError) as e:
raise LaterError("syntax error", e)
return data