Skip to content

Commit 52cb699

Browse files
authored
Merge pull request #244 from MITLibraries/TIMX-459-update-logging
TIMX 459 - update logging
2 parents 419a468 + 43ca788 commit 52cb699

File tree

6 files changed

+512
-490
lines changed

6 files changed

+512
-490
lines changed

Pipfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name = "pypi"
55

66
[packages]
77
attrs = "*"
8-
beautifulsoup4 = "*"
8+
beautifulsoup4 = "==4.12.3"
99
click = "*"
1010
jsonlines = "*"
1111
lxml = "*"

Pipfile.lock

+473-475
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ WORKSPACE=### Set to `dev` for local development, this will be set to `stage` an
5353

5454
```shell
5555
ETL_VERSION=### Version number of the TIMDEX ETL infrastructure. This can be used to align application behavior with the requirements of other applications in the TIMDEX ETL pipeline.
56+
WARNING_ONLY_LOGGERS=### Comma-seperated list of logger names to set as WARNING only, e.g. 'botocore,charset_normalizer,smart_open'
5657
```
5758

5859
## CLI commands

pyproject.toml

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ fixable = ["E", "F", "I", "Q"]
5151
[tool.ruff.lint.flake8-annotations]
5252
mypy-init-return = true
5353

54+
[tool.ruff.lint.flake8-builtins]
55+
builtins-allowed-modules = [
56+
"json", # allows for transmogrifier/sources/json directory
57+
"xml", # allows for transmogrifier/sources/xml directory
58+
]
59+
5460
[tool.ruff.lint.flake8-pytest-style]
5561
fixture-parentheses = false
5662

transmogrifier/cli.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def main(
6666
) -> None:
6767
start_time = perf_counter()
6868
root_logger = logging.getLogger()
69-
logger.info(configure_logger(root_logger, verbose))
69+
logger.info(configure_logger(root_logger, verbose=verbose))
7070
logger.info(configure_sentry())
7171
logger.info("Running transform for source %s", source)
7272

transmogrifier/config.py

+30-13
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,40 @@
132132
}
133133

134134

135-
def configure_logger(logger: logging.Logger, verbose: bool) -> str: # noqa: FBT001
135+
def configure_logger(
136+
root_logger: logging.Logger,
137+
*,
138+
verbose: bool = False,
139+
warning_only_loggers: str | None = None,
140+
) -> str:
141+
"""Configure application via passed application root logger.
142+
143+
If verbose=True, 3rd party libraries can be quite chatty. For convenience, they can
144+
be set to WARNING level by either passing a comma seperated list of logger names to
145+
'warning_only_loggers' or by setting the env var WARNING_ONLY_LOGGERS.
146+
"""
136147
if verbose:
137-
logging.basicConfig(
138-
format="%(asctime)s %(levelname)s %(name)s.%(funcName)s() line %(lineno)d: "
139-
"%(message)s",
148+
root_logger.setLevel(logging.DEBUG)
149+
logging_format = (
150+
"%(asctime)s %(levelname)s %(name)s.%(funcName)s() "
151+
"line %(lineno)d: %(message)s"
140152
)
141-
logger.setLevel(logging.DEBUG)
142-
for handler in logging.root.handlers:
143-
handler.addFilter(logging.Filter("transmogrifier"))
144153
else:
145-
logging.basicConfig(
146-
format="%(asctime)s %(levelname)s %(name)s.%(funcName)s(): %(message)s"
147-
)
148-
logger.setLevel(logging.INFO)
154+
root_logger.setLevel(logging.INFO)
155+
logging_format = "%(asctime)s %(levelname)s %(name)s.%(funcName)s(): %(message)s"
156+
157+
warning_only_loggers = os.getenv("WARNING_ONLY_LOGGERS", warning_only_loggers)
158+
if warning_only_loggers:
159+
for name in warning_only_loggers.split(","):
160+
logging.getLogger(name).setLevel(logging.WARNING)
161+
162+
handler = logging.StreamHandler()
163+
handler.setFormatter(logging.Formatter(logging_format))
164+
root_logger.addHandler(handler)
165+
149166
return (
150-
f"Logger '{logger.name}' configured with level="
151-
f"{logging.getLevelName(logger.getEffectiveLevel())}"
167+
f"Logger '{root_logger.name}' configured with level="
168+
f"{logging.getLevelName(root_logger.getEffectiveLevel())}"
152169
)
153170

154171

0 commit comments

Comments
 (0)