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

Choose logging handler via --log-handler CLI option #3293

Merged
merged 6 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion pelican/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

# pelican.log has to be the first pelican module to be loaded
# because logging.setLoggerClass has to be called before logging.getLogger
from pelican.log import console
from pelican.log import console, DEFAULT_LOG_HANDLER
from pelican.log import init as init_logging
from pelican.generators import (
ArticlesGenerator, # noqa: I100
Expand Down Expand Up @@ -455,6 +455,17 @@ def parse_arguments(argv=None):
),
)

LOGS_HANDLERS = {"plain": None, "rich": DEFAULT_LOG_HANDLER}
parser.add_argument(
"--logs-handler",
default="rich",
choices=LOGS_HANDLERS,
help=(
"Which handler to to format log messages. "
justinmayer marked this conversation as resolved.
Show resolved Hide resolved
"The `rich` handler prints output in columns."
),
)

parser.add_argument(
"--logs-dedup-min-level",
default="WARNING",
Expand Down Expand Up @@ -509,6 +520,8 @@ def parse_arguments(argv=None):
if args.bind is not None and not args.listen:
logger.warning("--bind without --listen has no effect")

args.logs_handler = LOGS_HANDLERS[args.logs_handler]

return args


Expand Down Expand Up @@ -631,6 +644,7 @@ def main(argv=None):
level=args.verbosity,
fatal=args.fatal,
name=__name__,
handler=args.logs_handler,
logs_dedup_min_level=logs_dedup_min_level,
)

Expand Down
9 changes: 7 additions & 2 deletions pelican/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,13 @@ def error(self, *args, stacklevel=1, **kwargs):
# force root logger to be of our preferred class
logging.getLogger().__class__ = FatalLogger

DEFAULT_LOG_HANDLER = RichHandler(console=console)


def init(
level=None,
fatal="",
handler=RichHandler(console=console),
handler=DEFAULT_LOG_HANDLER,
name=None,
logs_dedup_min_level=None,
):
Expand All @@ -139,7 +141,10 @@ def init(

LOG_FORMAT = "%(message)s"
logging.basicConfig(
level=level, format=LOG_FORMAT, datefmt="[%H:%M:%S]", handlers=[handler]
level=level,
format=LOG_FORMAT,
datefmt="[%H:%M:%S]",
handlers=[handler] if handler else [],
)

logger = logging.getLogger(name)
Expand Down
Loading