I am using the Python standard logging configuration, combined with yaml to load a config file with the dictConfig() function:
import logging
import yaml
with open('logging.yaml','r') as f:
logConfig = yaml.safe_load(f.read())
logging.config.dictConfig(logConfig)
Since an incremental logging configuration in python limits the capabilities, every logging file has to contain a minimum amount of information, something like this:
version: 1
formatters:
simple:
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
loggers:
my_module:
level: ERROR
root:
level: INFO
handlers: [console]
Which either forces one to learn this by heart, store it somewhere, or look it up each time. Since neither of these really work for me, I would like to find a way to generate it. This brings me to the question:
Is there a way to get the current (or basic) logging config as a dictionary?
This would make it easy to make an initial config file by running the following code once, and just remove/edit what you want:
import logging
import yaml
logConfig = logging.get_current_config_as_a_dictionary()
with open('logging.yaml','w') as f:
f.write(yaml.dump(logConfig))
Yaml is just my personal preference of course, the same question could be posted for something like JSON.