From 9411c067d778bf49fc72ca479fb148a819f5895c Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Fri, 26 Dec 2025 14:02:32 +0000 Subject: [PATCH] Fix raising exceptions on filtered logs --- pelican/log.py | 49 ++++++++++++++++++------------------------------- 1 file changed, 18 insertions(+), 31 deletions(-) diff --git a/pelican/log.py b/pelican/log.py index 27478d14..cf05a830 100644 --- a/pelican/log.py +++ b/pelican/log.py @@ -10,6 +10,10 @@ __all__ = ["init"] console = Console() +class FilteredMessage(Exception): + """An exception to signal whether a message was filtered or not.""" + + class LimitFilter(logging.Filter): """ Remove duplicates records, and limit the number of records in the same @@ -86,40 +90,23 @@ class FatalLogger(LimitLogger): warnings_fatal = False errors_fatal = False - def warning(self, *args, stacklevel=1, **kwargs): - """ - Displays a logging warning. + def filter(self, record): + """A hack to let _log() know whether a message was logged or not.""" + result = super().filter(record) + if not result: + raise FilteredMessage() + return result - Wrapping it here allows Pelican to filter warnings, and conditionally - make warnings fatal. + def _log(self, level, msg, args, exc_info=None, extra=None, stack_info=False, + stacklevel=1): + try: + super()._log(level, msg, args, exc_info, extra, stack_info, stacklevel + 1) + except FilteredMessage: + return - Args: - stacklevel (int): the stacklevel that would be used to display the - calling location, except for this function. Adjusting the - stacklevel allows you to see the "true" calling location of the - warning, rather than this wrapper location. - """ - stacklevel += 1 - super().warning(*args, stacklevel=stacklevel, **kwargs) - if FatalLogger.warnings_fatal: + if FatalLogger.warnings_fatal and level >= logging.WARNING: raise RuntimeError("Warning encountered") - - def error(self, *args, stacklevel=1, **kwargs): - """ - Displays a logging error. - - Wrapping it here allows Pelican to filter errors, and conditionally - make errors non-fatal. - - Args: - stacklevel (int): the stacklevel that would be used to display the - calling location, except for this function. Adjusting the - stacklevel allows you to see the "true" calling location of the - error, rather than this wrapper location. - """ - stacklevel += 1 - super().error(*args, stacklevel=stacklevel, **kwargs) - if FatalLogger.errors_fatal: + elif FatalLogger.errors_fatal and level >= logging.ERROR: raise RuntimeError("Error encountered")