Skip to content

Commit ef718c3

Browse files
committed
Tweak logging to logging is written to root logger.
1 parent e2f3323 commit ef718c3

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/pyapp/app/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def my_command(
141141
import os
142142
import sys
143143
from argparse import ArgumentParser
144+
from argparse import FileType
144145
from argparse import Namespace as CommandOptions
145146
from typing import Optional
146147
from typing import Sequence
@@ -329,6 +330,11 @@ def _init_parser(self):
329330
help="Specify the log level to be used. "
330331
f"Defaults to env variable: {_key_help(self.env_loglevel_key)}",
331332
)
333+
# arg_group.add_argument(
334+
# "--log-file",
335+
# type=FileType(mode="w", encoding="UTF-8"),
336+
# help="Optionally override log file output.",
337+
# )
332338
arg_group.add_argument(
333339
"--log-color",
334340
"--log-colour",
@@ -474,13 +480,14 @@ def configure_logging(self, opts: CommandOptions):
474480
if hasattr(self, "_init_logger"):
475481
self.default_log_handler.formatter = self.get_log_formatter(opts.log_color)
476482

483+
if conf.settings.LOGGING:
484+
logger.info("Applying logging configuration.")
485+
477486
# Replace root handler with the default handler
478487
logging.root.handlers.pop(0)
479488
logging.root.handlers.append(self.default_log_handler)
480489

481490
if conf.settings.LOGGING:
482-
logger.info("Applying logging configuration.")
483-
484491
# Set a default version if not supplied by settings
485492
dict_config = conf.settings.LOGGING.copy()
486493
dict_config.setdefault("version", 1)
@@ -490,7 +497,7 @@ def configure_logging(self, opts: CommandOptions):
490497
logging.root.setLevel(opts.log_level)
491498

492499
# Replay initial entries and remove
493-
self._init_logger.replay(self.default_log_handler)
500+
self._init_logger.replay(logger.root)
494501
del self._init_logger
495502

496503
def checks_on_startup(self, opts: CommandOptions):

0 commit comments

Comments
 (0)