Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 27 additions & 40 deletions pelican/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
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
Expand Down Expand Up @@ -83,44 +87,27 @@ def enable_filter(self):


class FatalLogger(LimitLogger):
warnings_fatal = False
errors_fatal = False

def warning(self, *args, stacklevel=1, **kwargs):
"""
Displays a logging warning.

Wrapping it here allows Pelican to filter warnings, and conditionally
make warnings 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
warning, rather than this wrapper location.
"""
stacklevel += 1
super().warning(*args, stacklevel=stacklevel, **kwargs)
if FatalLogger.warnings_fatal:
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:
raise RuntimeError("Error encountered")
fatal_lvl = logging.CRITICAL + 1 # i.e. No levels by default

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

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:
# Avoid raising RuntimeError below if no log was emitted.
return

# __init__.py:main() catches this exception then does it's own critical log.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I'd just remove the exception handler from main() and let Python report it normally...

# We need to avoid throwing the exception a second time here.
if level >= FatalLogger.fatal_lvl and level != logging.CRITICAL:
raise RuntimeError("Warning or error encountered")


logging.setLoggerClass(FatalLogger)
Expand All @@ -137,8 +124,8 @@ def init(
name=None,
logs_dedup_min_level=None,
):
FatalLogger.warnings_fatal = fatal.startswith("warning")
FatalLogger.errors_fatal = bool(fatal)
if fatal:
FatalLogger.fatal_lvl = logging.WARNING if fatal.startswith("warning") else logging.ERROR

LOG_FORMAT = "%(message)s"
logging.basicConfig(
Expand Down