Skip to content

Commit 7566cc2

Browse files
committed
Update logging, no filtering, explicit warning
Why these changes are being introduced: The previous approach filtered all logging to only loggers from the namespace 'tim' thereby excluding the new 'timdex_dataset_api' (TDA) logging that we would like exposed. This pattern works well most of the time when 3rd party library logging is not needed, but inflexible when it is. How this addresses that need: Reworks the application logging setup as part of configure_logger() to: * not use logging.basicConfig() * avoid filtering loggers, but instead explicitly setting some logger names to WARNING level (e.g. chatty ones like botocore) via arguments or env vars Side effects of this change: In production, where log level is INFO, it is possible that some libraries may produce more logging statements, but anticipated to be low. But desirable logs, like the timdex_dataset_api library, will now show and will be driven by this application's --verbose flag. Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/TIMX-459
1 parent e41bf92 commit 7566cc2

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

tim/config.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,40 @@ def configure_index_settings() -> tuple:
3131
return all_settings["mappings"], all_settings["settings"]
3232

3333

34-
def configure_logger(logger: logging.Logger, *, verbose: bool) -> str:
34+
def configure_logger(
35+
root_logger: logging.Logger,
36+
*,
37+
verbose: bool = False,
38+
warning_only_loggers: str | None = None,
39+
) -> str:
40+
"""Configure application via passed application root logger.
41+
42+
If verbose=True, 3rd party libraries can be quite chatty. For convenience, they can
43+
be set to WARNING level by either passing a comma seperated list of logger names to
44+
'warning_only_loggers' or by setting the env var WARNING_ONLY_LOGGERS.
45+
"""
3546
if verbose:
36-
logging.basicConfig(
37-
format="%(asctime)s %(levelname)s %(name)s.%(funcName)s() line %(lineno)d: "
38-
"%(message)s"
47+
root_logger.setLevel(logging.DEBUG)
48+
logging_format = (
49+
"%(asctime)s %(levelname)s %(name)s.%(funcName)s() "
50+
"line %(lineno)d: %(message)s"
3951
)
40-
logger.setLevel(logging.DEBUG)
4152
else:
42-
logging.basicConfig(
43-
format="%(asctime)s %(levelname)s %(name)s.%(funcName)s(): %(message)s"
44-
)
45-
logger.setLevel(logging.INFO)
46-
for handler in logging.root.handlers:
47-
handler.addFilter(logging.Filter("tim"))
53+
root_logger.setLevel(logging.INFO)
54+
logging_format = "%(asctime)s %(levelname)s %(name)s.%(funcName)s(): %(message)s"
55+
56+
warning_only_loggers = os.getenv("WARNING_ONLY_LOGGERS", warning_only_loggers)
57+
if warning_only_loggers:
58+
for name in warning_only_loggers.split(","):
59+
logging.getLogger(name).setLevel(logging.WARNING)
60+
61+
handler = logging.StreamHandler()
62+
handler.setFormatter(logging.Formatter(logging_format))
63+
root_logger.addHandler(handler)
64+
4865
return (
49-
f"Logger '{logger.name}' configured with level="
50-
f"{logging.getLevelName(logger.getEffectiveLevel())}"
66+
f"Logger '{root_logger.name}' configured with level="
67+
f"{logging.getLevelName(root_logger.getEffectiveLevel())}"
5168
)
5269

5370

0 commit comments

Comments
 (0)