diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:19:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-03-09 13:20:02 +0000 |
commit | 58daab21cd043e1dc37024a7f99b396788372918 (patch) | |
tree | 96771e43bb69f7c1c2b0b4f7374cb74d7866d0cb /collectors/python.d.plugin/python_modules/bases/loggers.py | |
parent | Releasing debian version 1.43.2-1. (diff) | |
download | netdata-58daab21cd043e1dc37024a7f99b396788372918.tar.xz netdata-58daab21cd043e1dc37024a7f99b396788372918.zip |
Merging upstream version 1.44.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'collectors/python.d.plugin/python_modules/bases/loggers.py')
-rw-r--r-- | collectors/python.d.plugin/python_modules/bases/loggers.py | 182 |
1 files changed, 87 insertions, 95 deletions
diff --git a/collectors/python.d.plugin/python_modules/bases/loggers.py b/collectors/python.d.plugin/python_modules/bases/loggers.py index 47f196a6d..7ae8ab0c1 100644 --- a/collectors/python.d.plugin/python_modules/bases/loggers.py +++ b/collectors/python.d.plugin/python_modules/bases/loggers.py @@ -4,6 +4,8 @@ # SPDX-License-Identifier: GPL-3.0-or-later import logging +import os +import stat import traceback from sys import exc_info @@ -15,39 +17,46 @@ except ImportError: from bases.collection import on_try_except_finally, unicode_str +LOGGING_LEVELS = { + 'CRITICAL': 50, + 'ERROR': 40, + 'WARNING': 30, + 'INFO': 20, + 'DEBUG': 10, + 'NOTSET': 0, +} -LOGGING_LEVELS = {'CRITICAL': 50, - 'ERROR': 40, - 'WARNING': 30, - 'INFO': 20, - 'DEBUG': 10, - 'NOTSET': 0} -DEFAULT_LOG_LINE_FORMAT = '%(asctime)s: %(name)s %(levelname)s : %(message)s' -DEFAULT_LOG_TIME_FORMAT = '%Y-%m-%d %H:%M:%S' +def is_stderr_connected_to_journal(): + journal_stream = os.environ.get("JOURNAL_STREAM") + if not journal_stream: + return False -PYTHON_D_LOG_LINE_FORMAT = '%(asctime)s: %(name)s %(levelname)s: %(module_name)s[%(job_name)s] : %(message)s' -PYTHON_D_LOG_NAME = 'python.d' + colon_index = journal_stream.find(":") + if colon_index <= 0: + return False + device, inode = journal_stream[:colon_index], journal_stream[colon_index + 1:] -def limiter(log_max_count=30, allowed_in_seconds=60): - def on_decorator(func): + try: + device_number, inode_number = os.fstat(2)[stat.ST_DEV], os.fstat(2)[stat.ST_INO] + except OSError: + return False - def on_call(*args): - current_time = args[0]._runtime_counters.start_mono - lc = args[0]._logger_counters + return str(device_number) == device and str(inode_number) == inode - if lc.logged and lc.logged % log_max_count == 0: - if current_time - lc.time_to_compare <= allowed_in_seconds: - lc.dropped += 1 - return - lc.time_to_compare = current_time - lc.logged += 1 - func(*args) +is_journal = is_stderr_connected_to_journal() + +DEFAULT_LOG_LINE_FORMAT = '%(asctime)s: %(name)s %(levelname)s : %(message)s' +PYTHON_D_LOG_LINE_FORMAT = '%(asctime)s: %(name)s %(levelname)s: %(module_name)s[%(job_name)s] : %(message)s' + +if is_journal: + DEFAULT_LOG_LINE_FORMAT = '%(name)s %(levelname)s : %(message)s' + PYTHON_D_LOG_LINE_FORMAT = '%(name)s %(levelname)s: %(module_name)s[%(job_name)s] : %(message)s ' - return on_call - return on_decorator +DEFAULT_LOG_TIME_FORMAT = '%Y-%m-%d %H:%M:%S' +PYTHON_D_LOG_NAME = 'python.d' def add_traceback(func): @@ -66,27 +75,16 @@ def add_traceback(func): return on_call -class LoggerCounters: - def __init__(self): - self.logged = 0 - self.dropped = 0 - self.time_to_compare = time() - - def __repr__(self): - return 'LoggerCounter(logged: {logged}, dropped: {dropped})'.format(logged=self.logged, - dropped=self.dropped) - - class BaseLogger(object): - def __init__(self, logger_name, log_fmt=DEFAULT_LOG_LINE_FORMAT, date_fmt=DEFAULT_LOG_TIME_FORMAT, - handler=logging.StreamHandler): - """ - :param logger_name: <str> - :param log_fmt: <str> - :param date_fmt: <str> - :param handler: <logging handler> - """ + def __init__( + self, + logger_name, + log_fmt=DEFAULT_LOG_LINE_FORMAT, + date_fmt=DEFAULT_LOG_TIME_FORMAT, + handler=logging.StreamHandler, + ): self.logger = logging.getLogger(logger_name) + self._muted = False if not self.has_handlers(): self.severity = 'INFO' self.logger.addHandler(handler()) @@ -96,11 +94,6 @@ class BaseLogger(object): return '<Logger: {name})>'.format(name=self.logger.name) def set_formatter(self, fmt, date_fmt=DEFAULT_LOG_TIME_FORMAT): - """ - :param fmt: <str> - :param date_fmt: <str> - :return: - """ if self.has_handlers(): self.logger.handlers[0].setFormatter(logging.Formatter(fmt=fmt, datefmt=date_fmt)) @@ -113,43 +106,48 @@ class BaseLogger(object): @severity.setter def severity(self, level): - """ - :param level: <str> or <int> - :return: - """ if level in LOGGING_LEVELS: self.logger.setLevel(LOGGING_LEVELS[level]) + def _log(self, level, *msg, **kwargs): + if not self._muted: + self.logger.log(level, ' '.join(map(unicode_str, msg)), **kwargs) + def debug(self, *msg, **kwargs): - self.logger.debug(' '.join(map(unicode_str, msg)), **kwargs) + self._log(logging.DEBUG, *msg, **kwargs) def info(self, *msg, **kwargs): - self.logger.info(' '.join(map(unicode_str, msg)), **kwargs) + self._log(logging.INFO, *msg, **kwargs) def warning(self, *msg, **kwargs): - self.logger.warning(' '.join(map(unicode_str, msg)), **kwargs) + self._log(logging.WARN, *msg, **kwargs) def error(self, *msg, **kwargs): - self.logger.error(' '.join(map(unicode_str, msg)), **kwargs) + self._log(logging.ERROR, *msg, **kwargs) - def alert(self, *msg, **kwargs): - self.logger.critical(' '.join(map(unicode_str, msg)), **kwargs) + def alert(self, *msg, **kwargs): + self._log(logging.CRITICAL, *msg, **kwargs) @on_try_except_finally(on_finally=(exit, 1)) def fatal(self, *msg, **kwargs): - self.logger.critical(' '.join(map(unicode_str, msg)), **kwargs) + self._log(logging.CRITICAL, *msg, **kwargs) + + def mute(self): + self._muted = True + + def unmute(self): + self._muted = False class PythonDLogger(object): - def __init__(self, logger_name=PYTHON_D_LOG_NAME, log_fmt=PYTHON_D_LOG_LINE_FORMAT): - """ - :param logger_name: <str> - :param log_fmt: <str> - """ + def __init__( + self, + logger_name=PYTHON_D_LOG_NAME, + log_fmt=PYTHON_D_LOG_LINE_FORMAT, + ): self.logger = BaseLogger(logger_name, log_fmt=log_fmt) self.module_name = 'plugin' self.job_name = 'main' - self._logger_counters = LoggerCounters() _LOG_TRACEBACK = False @@ -162,45 +160,39 @@ class PythonDLogger(object): PythonDLogger._LOG_TRACEBACK = value def debug(self, *msg): - self.logger.debug(*msg, extra={'module_name': self.module_name, - 'job_name': self.job_name or self.module_name}) + self.logger.debug(*msg, extra={ + 'module_name': self.module_name, + 'job_name': self.job_name or self.module_name, + }) def info(self, *msg): - self.logger.info(*msg, extra={'module_name': self.module_name, - 'job_name': self.job_name or self.module_name}) + self.logger.info(*msg, extra={ + 'module_name': self.module_name, + 'job_name': self.job_name or self.module_name, + }) def warning(self, *msg): - self.logger.warning(*msg, extra={'module_name': self.module_name, - 'job_name': self.job_name or self.module_name}) + self.logger.warning(*msg, extra={ + 'module_name': self.module_name, + 'job_name': self.job_name or self.module_name, + }) @add_traceback def error(self, *msg): - self.logger.error(*msg, extra={'module_name': self.module_name, - 'job_name': self.job_name or self.module_name}) + self.logger.error(*msg, extra={ + 'module_name': self.module_name, + 'job_name': self.job_name or self.module_name, + }) @add_traceback def alert(self, *msg): - self.logger.alert(*msg, extra={'module_name': self.module_name, - 'job_name': self.job_name or self.module_name}) + self.logger.alert(*msg, extra={ + 'module_name': self.module_name, + 'job_name': self.job_name or self.module_name, + }) def fatal(self, *msg): - self.logger.fatal(*msg, extra={'module_name': self.module_name, - 'job_name': self.job_name or self.module_name}) - - -class PythonDLimitedLogger(PythonDLogger): - @limiter() - def info(self, *msg): - PythonDLogger.info(self, *msg) - - @limiter() - def warning(self, *msg): - PythonDLogger.warning(self, *msg) - - @limiter() - def error(self, *msg): - PythonDLogger.error(self, *msg) - - @limiter() - def alert(self, *msg): - PythonDLogger.alert(self, *msg) + self.logger.fatal(*msg, extra={ + 'module_name': self.module_name, + 'job_name': self.job_name or self.module_name, + }) |