Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change default of --fatal command line arg to terminate on error #3317

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: major

Change default of `--fatal` command line switch to terminate on error.
8 changes: 4 additions & 4 deletions pelican/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,12 @@ def parse_arguments(argv=None):

parser.add_argument(
"--fatal",
metavar="errors|warnings",
choices=("errors", "warnings"),
default="",
metavar="errors|warnings|ignore",
choices=("errors", "warnings", "ignore"),
default="errors",
help=(
"Exit the program with non-zero status if any "
"errors/warnings encountered."
"errors/warnings encountered, or ignore any errors."
),
)

Expand Down
19 changes: 15 additions & 4 deletions pelican/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,23 @@ def error(self, *args, stacklevel=1, **kwargs):

def init(
level=None,
fatal="",
fatal="errors",
handler=DEFAULT_LOG_HANDLER,
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
20 changes: 20 additions & 0 deletions pelican/tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest
from unittest.mock import MagicMock, patch

from pelican import DEFAULT_LOG_HANDLER, main


class TestLog(unittest.TestCase):
@patch("pelican.get_instance")
@patch("pelican.init_logging")
def test_main_fatal_default(self, init_logging_mock, get_instance):
get_instance.side_effect = lambda *args, **kwargs: (MagicMock(), MagicMock())
main()
init_logging_mock.assert_called_once_with(
level=None,
# default is "errors"
fatal="errors",
name="pelican",
handler=DEFAULT_LOG_HANDLER,
logs_dedup_min_level=30,
)
Loading