Skip to content

Commit

Permalink
Choose logging handler via --log-handler CLI option (#3293)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpitclaudel authored May 30, 2024
1 parent c46063c commit 39c9644
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
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 # noqa: I001
from pelican.log import console, DEFAULT_LOG_HANDLER # noqa: I001
from pelican.log import init as init_logging
from pelican.generators import (
ArticlesGenerator,
Expand Down Expand Up @@ -455,6 +455,17 @@ def parse_arguments(argv=None):
),
)

LOG_HANDLERS = {"plain": None, "rich": DEFAULT_LOG_HANDLER}
parser.add_argument(
"--log-handler",
default="rich",
choices=LOG_HANDLERS,
help=(
"Which handler to use to format log messages. "
"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.log_handler = LOG_HANDLERS[args.log_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.log_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

0 comments on commit 39c9644

Please sign in to comment.