Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
boxydog committed May 31, 2024
1 parent d8e4563 commit 1e57089
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pelican/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ def main(argv=None):
logs_dedup_min_level = getattr(logging, args.logs_dedup_min_level)
init_logging(
level=args.verbosity,
fatal=args.fatal if args.fatal != "ignore" else "",
fatal=args.fatal,
name=__name__,
handler=args.log_handler,
logs_dedup_min_level=logs_dedup_min_level,
Expand Down
17 changes: 14 additions & 3 deletions pelican/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,18 @@ def init(
name=None,
logs_dedup_min_level=None,
):
"""Initialize the logger.
:param level: the log level
:param fatal: how to set up the FatalLogger. If "warning", then warnings are fatal.
If fatal is set to anything other than "" or "ignore",
then errors are fatal.
:param handler: the logging handler
:param name: the name of the logger to use
:param logs_dedup_min_level: the LimitFilter.LOGS_DEDUP_MIN_LEVEL to use
"""
FatalLogger.warnings_fatal = fatal.startswith("warning")
FatalLogger.errors_fatal = bool(fatal)
FatalLogger.errors_fatal = bool(fatal) and fatal != "ignore"

LOG_FORMAT = "%(message)s"
logging.basicConfig(
Expand All @@ -155,12 +165,13 @@ def init(
LimitFilter.LOGS_DEDUP_MIN_LEVEL = logs_dedup_min_level


def log_warnings():
def log_warnings(fatal: str) -> None:
"""Redirect warnings module to use logging instead."""
import warnings

logging.captureWarnings(True)
warnings.simplefilter("default", DeprecationWarning)
init(logging.DEBUG, name="py.warnings")
init(logging.DEBUG, name="py.warnings", fatal=fatal)


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion pelican/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from pelican.log import log_warnings

# redirect warnings module to use logging instead
log_warnings()
# "ignore" means "don't raise on logging an error"
log_warnings("ignore")

# setup warnings to log DeprecationWarning's and error on
# warnings in pelican's codebase
Expand Down

0 comments on commit 1e57089

Please sign in to comment.