mirror of
https://github.com/thegeeklab/ansible-later.git
synced 2024-11-22 12:50:42 +00:00
add better multiline logging and fix json format issues
This commit is contained in:
parent
554d957af3
commit
397c92a27b
@ -113,6 +113,9 @@ class Candidate(object):
|
|||||||
|
|
||||||
labels = {"tag": "review", "standard": standard.name, "file": self.path, "passed": True}
|
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
|
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))]:
|
if not err.lineno or utils.is_line_in_ranges(err.lineno, utils.lines_ranges(lines))]:
|
||||||
err_labels = copy.copy(labels)
|
err_labels = copy.copy(labels)
|
||||||
@ -122,16 +125,22 @@ class Candidate(object):
|
|||||||
|
|
||||||
if not standard.version:
|
if not standard.version:
|
||||||
LOG.warn("{id}Best practice '{name}' not met:\n{path}:{error}".format(
|
LOG.warn("{id}Best practice '{name}' not met:\n{path}:{error}".format(
|
||||||
id=standard.id, name=standard.name, path=self.path, error=err),
|
id=self._format_id(standard.id),
|
||||||
extra=flag_extra(err_labels))
|
name=standard.name,
|
||||||
|
path=self.path,
|
||||||
|
error=err), extra=flag_extra(err_labels))
|
||||||
elif LooseVersion(standard.version) > LooseVersion(self.version):
|
elif LooseVersion(standard.version) > LooseVersion(self.version):
|
||||||
LOG.warn("{id}Future standard '{name}' not met:\n{path}:{error}".format(
|
LOG.warn("{id}Future standard '{name}' not met:\n{path}:{error}".format(
|
||||||
id=standard.id, name=standard.name, path=self.path, error=err),
|
id=self._format_id(standard.id),
|
||||||
extra=flag_extra(err_labels))
|
name=standard.name,
|
||||||
|
path=self.path,
|
||||||
|
error=err), extra=flag_extra(err_labels))
|
||||||
else:
|
else:
|
||||||
LOG.error("{id}Standard '{name}' not met:\n{path}:{error}".format(
|
LOG.error("{id}Standard '{name}' not met:\n{path}:{error}".format(
|
||||||
id=standard.id, name=standard.name, path=self.path, error=err),
|
id=self._format_id(standard.id),
|
||||||
extra=flag_extra(err_labels))
|
name=standard.name,
|
||||||
|
path=self.path,
|
||||||
|
error=err), extra=flag_extra(err_labels))
|
||||||
errors = errors + 1
|
errors = errors + 1
|
||||||
if not result.errors:
|
if not result.errors:
|
||||||
if not standard.version:
|
if not standard.version:
|
||||||
@ -143,6 +152,12 @@ class Candidate(object):
|
|||||||
|
|
||||||
return errors
|
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
|
def __repr__(self): # noqa
|
||||||
return "%s (%s)" % (type(self).__name__, self.path)
|
return "%s (%s)" % (type(self).__name__, self.path)
|
||||||
|
|
||||||
|
@ -25,12 +25,12 @@ def _should_do_markup():
|
|||||||
colorama.init(autoreset=True, strip=not _should_do_markup())
|
colorama.init(autoreset=True, strip=not _should_do_markup())
|
||||||
|
|
||||||
|
|
||||||
def flag_extra(kwargs):
|
def flag_extra(extra):
|
||||||
"""Ensure kwargs not conflict with the logging module."""
|
"""Ensure extra args are prefixed."""
|
||||||
assert isinstance(kwargs, dict)
|
assert isinstance(extra, dict)
|
||||||
|
|
||||||
flagged = dict()
|
flagged = dict()
|
||||||
for key, value in iteritems(kwargs):
|
for key, value in iteritems(extra):
|
||||||
flagged["later_" + key] = value
|
flagged["later_" + key] = value
|
||||||
|
|
||||||
return flagged
|
return flagged
|
||||||
@ -54,6 +54,22 @@ class LogFilter(object):
|
|||||||
return logRecord.levelno <= self.__level
|
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):
|
def get_logger(name=None, level=logging.DEBUG, json=False):
|
||||||
"""
|
"""
|
||||||
Build a logger with the given name and returns the logger.
|
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 = logging.StreamHandler(sys.stderr)
|
||||||
handler.setLevel(logging.ERROR)
|
handler.setLevel(logging.ERROR)
|
||||||
handler.addFilter(LogFilter(logging.ERROR))
|
handler.addFilter(LogFilter(logging.ERROR))
|
||||||
handler.setFormatter(logging.Formatter(error(CONSOLE_FORMAT)))
|
handler.setFormatter(MultilineFormatter(error(CONSOLE_FORMAT)))
|
||||||
|
|
||||||
if json:
|
if json:
|
||||||
handler.setFormatter(jsonlogger.JsonFormatter(JSON_FORMAT))
|
handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT))
|
||||||
|
|
||||||
return handler
|
return handler
|
||||||
|
|
||||||
@ -103,10 +119,10 @@ def _get_warn_handler(json=False):
|
|||||||
handler = logging.StreamHandler(sys.stdout)
|
handler = logging.StreamHandler(sys.stdout)
|
||||||
handler.setLevel(logging.WARN)
|
handler.setLevel(logging.WARN)
|
||||||
handler.addFilter(LogFilter(logging.WARN))
|
handler.addFilter(LogFilter(logging.WARN))
|
||||||
handler.setFormatter(logging.Formatter(warn(CONSOLE_FORMAT)))
|
handler.setFormatter(MultilineFormatter(warn(CONSOLE_FORMAT)))
|
||||||
|
|
||||||
if json:
|
if json:
|
||||||
handler.setFormatter(jsonlogger.JsonFormatter(JSON_FORMAT))
|
handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT))
|
||||||
|
|
||||||
return handler
|
return handler
|
||||||
|
|
||||||
@ -115,10 +131,10 @@ def _get_info_handler(json=False):
|
|||||||
handler = logging.StreamHandler(sys.stderr)
|
handler = logging.StreamHandler(sys.stderr)
|
||||||
handler.setLevel(logging.INFO)
|
handler.setLevel(logging.INFO)
|
||||||
handler.addFilter(LogFilter(logging.INFO))
|
handler.addFilter(LogFilter(logging.INFO))
|
||||||
handler.setFormatter(logging.Formatter(info("%(message)s")))
|
handler.setFormatter(MultilineFormatter(info("%(message)s")))
|
||||||
|
|
||||||
if json:
|
if json:
|
||||||
handler.setFormatter(jsonlogger.JsonFormatter(JSON_FORMAT))
|
handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT))
|
||||||
|
|
||||||
return handler
|
return handler
|
||||||
|
|
||||||
@ -127,10 +143,10 @@ def _get_critical_handler(json=False):
|
|||||||
handler = logging.StreamHandler(sys.stderr)
|
handler = logging.StreamHandler(sys.stderr)
|
||||||
handler.setLevel(logging.CRITICAL)
|
handler.setLevel(logging.CRITICAL)
|
||||||
handler.addFilter(LogFilter(logging.CRITICAL))
|
handler.addFilter(LogFilter(logging.CRITICAL))
|
||||||
handler.setFormatter(logging.Formatter(critical(CONSOLE_FORMAT)))
|
handler.setFormatter(MultilineFormatter(critical(CONSOLE_FORMAT)))
|
||||||
|
|
||||||
if json:
|
if json:
|
||||||
handler.setFormatter(jsonlogger.JsonFormatter(JSON_FORMAT))
|
handler.setFormatter(MultilineJsonFormatter(JSON_FORMAT))
|
||||||
|
|
||||||
return handler
|
return handler
|
||||||
|
|
||||||
@ -164,4 +180,5 @@ def color_text(color, msg):
|
|||||||
:returns: string
|
:returns: string
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return "{}{}{}".format(color, msg, colorama.Style.RESET_ALL)
|
return "{}{}{}".format(color, msg, colorama.Style.RESET_ALL)
|
||||||
|
@ -16,11 +16,6 @@ class Standard(object):
|
|||||||
:param standard_dict: Dictionary object containing all neseccary attributes
|
: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.id = standard_dict.get("id", "")
|
||||||
self.name = standard_dict.get("name")
|
self.name = standard_dict.get("name")
|
||||||
self.version = standard_dict.get("version")
|
self.version = standard_dict.get("version")
|
||||||
|
Loading…
Reference in New Issue
Block a user