Setting `logging.Formatter.converter` with `logging.config.dictConfig`?

Hi,

for logging in my application I do roughly this:

import logging, tomllib, time

log = logging.getLogger("my_app")

def setup_logging():
    logging.Formatter.converter = time.gmtime   # <--- can this be done in toml below?
    config_file = pathlib.Path("logging_conf.toml")  
    # toml contents at: https://paste.rs/Jg5Ap.txt
    with open(config_file, "rb") as fh:
        config = tomllib.load(fh)
    logging.config.dictConfig(config)

if __name__ == "__main__":
    setup_logging()
    # ...

Can I specify the part that says logging.Formatter.converter = time.gmtime per logger in the dict that I pass to dictConfig somehow?

This does not seem to work:

[formatters.simple]
format = "%(asctime)s [%(levelname)-8.8s|%(module)-14.14s]: %(message)s :[%(name)s]"
datefmt = "%Y-%m-%dZ%H:%M:%S"
converter = "ext://time.gmtime"  # also not without the `ext://`

The complete logging_conf.toml is here: Source Code | Jg5Ap | Rocket Powered Pastebin

I don’t think this is possible in this way because the keys correspond to the arguments for Formatter, and converter is not one of those. You do have the option of passing class value and specifying a custom subclass of Formatter that uses the gmtime converter.

That not really much better, though, since you still need some custom code and can’t just have everything in the config file.

2 Likes