2019-04-01 13:07:22 +00:00
|
|
|
"""Global logging helpers."""
|
|
|
|
|
2019-03-28 15:54:45 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import colorama
|
2019-04-03 21:39:27 +00:00
|
|
|
from six import iteritems
|
2019-03-28 15:54:45 +00:00
|
|
|
from ansible.module_utils.parsing.convert_bool import boolean as to_bool
|
2019-04-01 13:07:22 +00:00
|
|
|
from pythonjsonlogger import jsonlogger
|
|
|
|
|
2019-04-03 15:42:46 +00:00
|
|
|
CONSOLE_FORMAT = "%(levelname)s: %(message)s"
|
2019-04-03 21:39:27 +00:00
|
|
|
JSON_FORMAT = "(levelname) (asctime)"
|
2019-04-03 15:42:46 +00:00
|
|
|
|
2019-03-28 15:54:45 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
def _should_do_markup():
|
2019-03-28 15:54:45 +00:00
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
py_colors = os.environ.get("PY_COLORS", None)
|
2019-03-28 15:54:45 +00:00
|
|
|
if py_colors is not None:
|
|
|
|
return to_bool(py_colors, strict=False)
|
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
return sys.stdout.isatty() and os.environ.get("TERM") != "dumb"
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
colorama.init(autoreset=True, strip=not _should_do_markup())
|
2019-03-28 15:54:45 +00:00
|
|
|
|
2019-04-03 21:39:27 +00:00
|
|
|
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)
|
2019-03-28 15:54:45 +00:00
|
|
|
|
2019-04-03 21:39:27 +00:00
|
|
|
|
|
|
|
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
|
2019-04-03 15:42:46 +00:00
|
|
|
|
|
|
|
|
2019-03-28 15:54:45 +00:00
|
|
|
class LogFilter(object):
|
2019-04-01 13:07:22 +00:00
|
|
|
"""A custom log filter which excludes log messages above the logged level."""
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
def __init__(self, level):
|
2019-04-01 13:07:22 +00:00
|
|
|
"""
|
|
|
|
Initialize a new custom log filter.
|
|
|
|
|
|
|
|
:param level: Log level limit
|
|
|
|
:returns: None
|
|
|
|
|
|
|
|
"""
|
2019-03-28 15:54:45 +00:00
|
|
|
self.__level = level
|
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
def filter(self, logRecord): # noqa
|
2019-03-28 15:54:45 +00:00
|
|
|
# https://docs.python.org/3/library/logging.html#logrecord-attributes
|
|
|
|
return logRecord.levelno <= self.__level
|
|
|
|
|
|
|
|
|
|
|
|
def get_logger(name=None, level=logging.DEBUG, json=False):
|
|
|
|
"""
|
|
|
|
Build a logger with the given name and returns the logger.
|
2019-04-01 13:07:22 +00:00
|
|
|
|
|
|
|
:param name: The name for the logger. This is usually the module name, `__name__`.
|
|
|
|
:param level: Initialize the new logger with given log level.
|
|
|
|
:param json: Boolean flag to enable json formatted log output.
|
2019-03-28 15:54:45 +00:00
|
|
|
:return: logger object
|
|
|
|
|
2019-04-01 13:07:22 +00:00
|
|
|
"""
|
2019-03-28 15:54:45 +00:00
|
|
|
logger = logging.getLogger(name)
|
|
|
|
logger.setLevel(level)
|
|
|
|
logger.addHandler(_get_error_handler(json=json))
|
|
|
|
logger.addHandler(_get_warn_handler(json=json))
|
|
|
|
logger.addHandler(_get_info_handler(json=json))
|
2019-04-02 14:34:03 +00:00
|
|
|
logger.addHandler(_get_critical_handler(json=json))
|
2019-03-28 15:54:45 +00:00
|
|
|
logger.propagate = False
|
|
|
|
|
|
|
|
return logger
|
|
|
|
|
|
|
|
|
2019-04-03 15:42:46 +00:00
|
|
|
def update_logger(logger, level=None, json=None):
|
|
|
|
for handler in logger.handlers[:]:
|
|
|
|
logger.removeHandler(handler)
|
|
|
|
|
|
|
|
logger.setLevel(level)
|
|
|
|
logger.addHandler(_get_error_handler(json=json))
|
|
|
|
logger.addHandler(_get_warn_handler(json=json))
|
|
|
|
logger.addHandler(_get_info_handler(json=json))
|
|
|
|
logger.addHandler(_get_critical_handler(json=json))
|
|
|
|
|
|
|
|
|
2019-03-28 15:54:45 +00:00
|
|
|
def _get_error_handler(json=False):
|
|
|
|
handler = logging.StreamHandler(sys.stderr)
|
|
|
|
handler.setLevel(logging.ERROR)
|
|
|
|
handler.addFilter(LogFilter(logging.ERROR))
|
2019-04-03 15:42:46 +00:00
|
|
|
handler.setFormatter(logging.Formatter(error(CONSOLE_FORMAT)))
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
if json:
|
2019-04-03 15:42:46 +00:00
|
|
|
handler.setFormatter(jsonlogger.JsonFormatter(JSON_FORMAT))
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
return handler
|
|
|
|
|
|
|
|
|
|
|
|
def _get_warn_handler(json=False):
|
|
|
|
handler = logging.StreamHandler(sys.stdout)
|
|
|
|
handler.setLevel(logging.WARN)
|
|
|
|
handler.addFilter(LogFilter(logging.WARN))
|
2019-04-03 15:42:46 +00:00
|
|
|
handler.setFormatter(logging.Formatter(warn(CONSOLE_FORMAT)))
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
if json:
|
2019-04-03 15:42:46 +00:00
|
|
|
handler.setFormatter(jsonlogger.JsonFormatter(JSON_FORMAT))
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
return handler
|
|
|
|
|
|
|
|
|
|
|
|
def _get_info_handler(json=False):
|
|
|
|
handler = logging.StreamHandler(sys.stderr)
|
|
|
|
handler.setLevel(logging.INFO)
|
|
|
|
handler.addFilter(LogFilter(logging.INFO))
|
2019-04-01 13:07:22 +00:00
|
|
|
handler.setFormatter(logging.Formatter(info("%(message)s")))
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
if json:
|
2019-04-03 15:42:46 +00:00
|
|
|
handler.setFormatter(jsonlogger.JsonFormatter(JSON_FORMAT))
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
return handler
|
|
|
|
|
|
|
|
|
2019-04-02 14:34:03 +00:00
|
|
|
def _get_critical_handler(json=False):
|
|
|
|
handler = logging.StreamHandler(sys.stderr)
|
|
|
|
handler.setLevel(logging.CRITICAL)
|
|
|
|
handler.addFilter(LogFilter(logging.CRITICAL))
|
2019-04-03 15:42:46 +00:00
|
|
|
handler.setFormatter(logging.Formatter(critical(CONSOLE_FORMAT)))
|
2019-04-02 14:34:03 +00:00
|
|
|
|
|
|
|
if json:
|
2019-04-03 15:42:46 +00:00
|
|
|
handler.setFormatter(jsonlogger.JsonFormatter(JSON_FORMAT))
|
2019-04-02 14:34:03 +00:00
|
|
|
|
|
|
|
return handler
|
|
|
|
|
|
|
|
|
|
|
|
def critical(message):
|
|
|
|
"""Format critical messages and return string."""
|
2019-04-03 15:42:46 +00:00
|
|
|
return color_text(colorama.Fore.RED, "{}".format(message))
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def error(message):
|
2019-04-01 13:07:22 +00:00
|
|
|
"""Format error messages and return string."""
|
2019-04-03 15:42:46 +00:00
|
|
|
return color_text(colorama.Fore.RED, "{}".format(message))
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def warn(message):
|
2019-04-01 13:07:22 +00:00
|
|
|
"""Format warn messages and return string."""
|
2019-04-03 15:42:46 +00:00
|
|
|
return color_text(colorama.Fore.YELLOW, "{}".format(message))
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def info(message):
|
2019-04-01 13:07:22 +00:00
|
|
|
"""Format info messages and return string."""
|
2019-04-03 15:42:46 +00:00
|
|
|
return color_text(colorama.Fore.BLUE, "{}".format(message))
|
2019-03-28 15:54:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def color_text(color, msg):
|
2019-04-01 13:07:22 +00:00
|
|
|
"""
|
|
|
|
Colorize strings.
|
|
|
|
|
|
|
|
:param color: colorama color settings
|
|
|
|
:param msg: string to colorize
|
|
|
|
:returns: string
|
|
|
|
|
|
|
|
"""
|
|
|
|
return "{}{}{}".format(color, msg, colorama.Style.RESET_ALL)
|