From 861aed7dcb74d10fd9e8279da5aad80362d78eb4 Mon Sep 17 00:00:00 2001 From: BJ Dierkes Date: Tue, 26 Dec 2023 17:46:03 -0600 Subject: [PATCH] Deprecate FATAL Logging Facility in Favor of CRITICAL - Resolves Issue #533 --- CHANGELOG.md | 3 ++- cement/core/deprecations.py | 1 + cement/ext/ext_logging.py | 41 +++++++++++++++++++++++++++++---- tests/core/test_deprecations.py | 6 +++++ tests/ext/test_ext_logging.py | 3 +++ 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9adec92b..820b77cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,7 +29,8 @@ Misc: Deprecations: -- None +- `[ext.logging]` Deprecate FATAL facility in favor of CRITICAL. + - [Issue #533](https://github.com/datafolklabs/cement/issues/533) ## 3.0.8 - Aug 18, 2022 diff --git a/cement/core/deprecations.py b/cement/core/deprecations.py index 3a809c7f..edfbbc18 100644 --- a/cement/core/deprecations.py +++ b/cement/core/deprecations.py @@ -4,6 +4,7 @@ from warnings import warn DEPRECATIONS = { '3.0.8-1': "Environment variable CEMENT_FRAMEWORK_LOGGING is deprecated in favor of CEMENT_LOG, and will be removed in Cement v3.2.0", # noqa: E501 '3.0.8-2': "App.Meta.framework_logging will be changed or removed in Cement v3.2.0", # noqa: E501 + '3.0.10-1': "The FATAL logging facility is deprecated in favor of CRITICAL, and will be removed in future versions of Cement.", # noqa: E501 } diff --git a/cement/ext/ext_logging.py b/cement/ext/ext_logging.py index 224bbefc..37e6b034 100644 --- a/cement/ext/ext_logging.py +++ b/cement/ext/ext_logging.py @@ -5,6 +5,7 @@ Cement logging extension module. import os import logging from ..core import log +from ..core.deprecations import deprecate from ..utils.misc import is_true, minimal_logger from ..utils import fs @@ -92,7 +93,7 @@ class LoggingLogHandler(log.LogHandler): #: The help description for the log level argument log_level_argument_help = 'logging level' - levels = ['INFO', 'WARNING', 'ERROR', 'DEBUG', 'FATAL'] + levels = ['INFO', 'WARNING', 'ERROR', 'DEBUG', 'FATAL', 'CRITICAL'] def __init__(self, *args, **kw): super(LoggingLogHandler, self).__init__(*args, **kw) @@ -120,7 +121,10 @@ class LoggingLogHandler(log.LogHandler): """ Set the log level. Must be one of the log levels configured in self.levels which are - ``['INFO', 'WARNING', 'ERROR', 'DEBUG', 'FATAL']``. + ``['INFO', 'WARNING', 'ERROR', 'DEBUG', 'FATAL', 'CRITICAL]``. + + As of Cement 3.0.10, the FATAL facility is deprecated and will be + removed in future versions of Cement. Please us `CRITICAL` instead. :param level: The log level to set. @@ -130,8 +134,13 @@ class LoggingLogHandler(log.LogHandler): self.clear_loggers(namespace) level = level.upper() + if level not in self.levels: level = 'INFO' + + if level == 'FATAL': + deprecate('3.0.10-1') + level = getattr(logging, level.upper()) self.backend.setLevel(level) @@ -311,9 +320,9 @@ class LoggingLogHandler(log.LogHandler): kwargs = self._get_logging_kwargs(namespace, **kw) self.backend.error(msg, **kwargs) - def fatal(self, msg, namespace=None, **kw): + def critical(self, msg, namespace=None, **kw): """ - Log to the FATAL (aka CRITICAL) facility. + Log to the CRITICAL facility. Args: msg (str): The message to log. @@ -329,6 +338,30 @@ class LoggingLogHandler(log.LogHandler): """ kwargs = self._get_logging_kwargs(namespace, **kw) + self.backend.critical(msg, **kwargs) + + def fatal(self, msg, namespace=None, **kw): + """ + Log to the FATAL (aka CRITICAL) facility. + + As of Cement 3.0.10, this method is deprecated and will be removed in + future versions of Cement. Please us `critical()` instead. + + Args: + msg (str): The message to log. + + Keyword Args: + namespace (str): A log prefix, generally the module ``__name__`` + that the log is coming from. Will default to + ``self._meta.namespace`` if none is passed. + + Other Parameters: + kwargs: Keyword arguments are passed on to the backend logging + system. + + """ + deprecate('3.0.10-1') + kwargs = self._get_logging_kwargs(namespace, **kw) self.backend.fatal(msg, **kwargs) def debug(self, msg, namespace=None, **kw): diff --git a/tests/core/test_deprecations.py b/tests/core/test_deprecations.py index 1a2d6043..2bc03420 100644 --- a/tests/core/test_deprecations.py +++ b/tests/core/test_deprecations.py @@ -23,3 +23,9 @@ class TestDeprecations(object): app.run() assert "3.0.8-2" in str(w[-1].message) sys.argv = orig_argv + + def test_3_0_10__1(self): + with TestApp() as app: + with warnings.catch_warnings(record=True) as w: + app.log.set_level('FATAL') + assert "3.0.10-1" in str(w[-1].message) diff --git a/tests/ext/test_ext_logging.py b/tests/ext/test_ext_logging.py index 69047f93..4ce466bb 100644 --- a/tests/ext/test_ext_logging.py +++ b/tests/ext/test_ext_logging.py @@ -28,18 +28,21 @@ def test_alternate_namespaces(tmp): app.log.info('TEST', extra=dict(namespace=__name__)) app.log.warning('TEST', extra=dict(namespace=__name__)) app.log.error('TEST', extra=dict(namespace=__name__)) + app.log.critical('TEST', extra=dict(namespace=__name__)) app.log.fatal('TEST', extra=dict(namespace=__name__)) app.log.debug('TEST', extra=dict(namespace=__name__)) app.log.info('TEST', __name__, extra=dict(foo='bar')) app.log.warning('TEST', __name__, extra=dict(foo='bar')) app.log.error('TEST', __name__, extra=dict(foo='bar')) + app.log.critical('TEST', __name__, extra=dict(foo='bar')) app.log.fatal('TEST', __name__, extra=dict(foo='bar')) app.log.debug('TEST', __name__, extra=dict(foo='bar')) app.log.info('TEST', __name__) app.log.warning('TEST', __name__) app.log.error('TEST', __name__) + app.log.critical('TEST', __name__) app.log.fatal('TEST', __name__) app.log.debug('TEST', __name__)