From fa14538f205f8df6c31c209e996d289015f6369e Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Wed, 3 Apr 2019 23:39:27 +0200 Subject: [PATCH] add some details to json logs --- ansiblelater/command/candidates.py | 11 ++++++----- ansiblelater/logger.py | 31 +++++++++++++++++------------- ansiblelater/utils/yamlhelper.py | 2 ++ 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/ansiblelater/command/candidates.py b/ansiblelater/command/candidates.py index b6d81c9..21bbf30 100644 --- a/ansiblelater/command/candidates.py +++ b/ansiblelater/command/candidates.py @@ -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) diff --git a/ansiblelater/logger.py b/ansiblelater/logger.py index 538c41b..e197fc5 100644 --- a/ansiblelater/logger.py +++ b/ansiblelater/logger.py @@ -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)) diff --git a/ansiblelater/utils/yamlhelper.py b/ansiblelater/utils/yamlhelper.py index 2f60d88..32b5aff 100644 --- a/ansiblelater/utils/yamlhelper.py +++ b/ansiblelater/utils/yamlhelper.py @@ -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