Skip to content

Commit

Permalink
configuration: add logging_levels section
Browse files Browse the repository at this point in the history
Allow to configure the logging-levels of various loggers by adding a new
section `logging_levels` to the configuration file. Each entry in that
section configures the logging-level of one logger / channel.
  • Loading branch information
olaf-mandel committed Oct 6, 2024
1 parent fb5a956 commit d2bf861
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ collector_opts:
blacklist:
- docker0
- lo
logging_levels:
root: INFO
foomodule.barcollector: WARNING
```
### Start-up Configuration
Expand Down
2 changes: 2 additions & 0 deletions p3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ collector_opts:
blacklist:
- docker0
- lo
logging_levels:
root: INFO
24 changes: 23 additions & 1 deletion p3exporter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from wsgiref.simple_server import make_server

import yaml
import logging
import logging.config
import os
import signal
import sys
Expand All @@ -13,6 +13,27 @@
from p3exporter.web import create_app


def setup_logging(cfg: dict):
"""Set up logging as configured.
The configuration may optionally contain an entry `logging_levels`,
if it does not or if that entry is not a dict then does nothing.
The expected contents of the entry is logger-names as keys and
logging-levels as values.
:param cfg: Configuration as read from config-file.
:type cfg: dict
"""
if not isinstance(cfg.get('logging_levels'), dict):
return
levels = cfg['logging_levels']
loggers = {k: {'level': v} for k, v in levels.items()}
logging.config.dictConfig({
'version': 1,
'loggers': loggers,
})


def shutdown():
"""Shutdown the app in a clean way."""
logging.info('Shutting down, see you next time!')
Expand Down Expand Up @@ -41,6 +62,7 @@ def main():
with open(args.config, 'r') as config_file:
cfg = yaml.load(config_file, Loader=yaml.SafeLoader)
collector_config = CollectorConfig(**cfg)
setup_logging(cfg)

Collector(collector_config)

Expand Down
34 changes: 34 additions & 0 deletions tests/test_cases/logging_levels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from p3exporter import setup_logging
import logging
import pytest


@pytest.mark.parametrize("cfg_levels,expected", [
(None, [logging.NOTSET, logging.NOTSET, logging.NOTSET]),
("Not a dict", [logging.NOTSET, logging.NOTSET, logging.NOTSET]),
({"": "INFO", "foo": "DEBUG"}, [logging.INFO, logging.DEBUG, logging.NOTSET]),
({"root": "WARNING", "bar": "CRITICAL"}, [logging.WARNING, logging.NOTSET, logging.CRITICAL]),
({"foo": 10, "bar": 20}, [logging.NOTSET, logging.DEBUG, logging.INFO]),
])
def test_logging_levels(cfg_levels, expected):
loggers = ["", "foo", "bar"]

# GIVEN a clean slate of uninitialized logging-levels
for lg in loggers:
logging.getLogger(lg).setLevel(logging.NOTSET)

# AND an input config-dictionary
cfg = {
"exporter_name": "Test only",
"collectors": [],
"collector_opts": {},
}
if cfg_levels is not None:
cfg["logging_levels"] = cfg_levels

# WHEN calling setup_logging()
setup_logging(cfg)

# THEN the logging-levels should get changed to the expected
for i, lg in enumerate(loggers):
assert logging.getLogger(lg).level == expected[i]

0 comments on commit d2bf861

Please sign in to comment.