TOML is becoming common in the Python ecosystem. pyproject.toml
is the packaging standard and 3.11 gets the tomllib
parser in the standard library.
Storing a logging configuration in a toml file means occasionally storing a configuration for the root logger, normally specified as logging_config["loggers"][""]
. However, this configuration cannot be written to a toml file since the specification does not allow empty or white-space-only fields. This can be worked around by using JSON or by providing a sentinel value (eg, _root_
) and handling that value in application code. It would be easier if the root logger could optionally be specified by name, such as RootLogger
or _root_
.
Interestingly, if you structure your toml file correctly, you can specify the empty field name, but it seems this is a bug, not a feature, and is not reliable through load/dump cycles:
>>> import toml
>>> from pprint import pprint
>>> config = toml.loads("""
[loggers]
'' = {level='NOTSET', handlers=[]}
'logger_1' = {level='ERROR', handlers=['handler_1']}
""")
>>> pprint(config)
{'loggers': {'': {'handlers': [], 'level': 'NOTSET'},
'logger_1': {'handlers': ['handler_1'], 'level': 'ERROR'}}}
>>> toml_config = toml.dumps(config)
>>> print(toml_config)
[loggers.""]
level = "NOTSET"
handlers = []
[loggers.logger_1]
level = "ERROR"
handlers = [ "handler_1",]
>>> config_from_dump = toml.loads(toml_config)
[...]
TomlDecodeError: Can't have a keygroup with an empty name (line 4 column 1 char 23)