Where Is a Complete Example of Logging.Config.Dictconfig

Where is a complete example of logging.config.dictConfig?

How about here! The corresponding documentation reference is configuration-dictionary-schema.

LOGGING_CONFIG = { 
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
},
},
'handlers': {
'default': {
'level': 'INFO',
'formatter': 'standard',
'class': 'logging.StreamHandler',
'stream': 'ext://sys.stdout', # Default is stderr
},
},
'loggers': {
'': { # root logger
'handlers': ['default'],
'level': 'WARNING',
'propagate': False
},
'my.packg': {
'handlers': ['default'],
'level': 'INFO',
'propagate': False
},
'__main__': { # if __name__ == '__main__'
'handlers': ['default'],
'level': 'DEBUG',
'propagate': False
},
}
}

Usage:

import logging.config

# Run once at startup:
logging.config.dictConfig(LOGGING_CONFIG)

# Include in each module:
log = logging.getLogger(__name__)
log.debug("Logging is configured.")

In case you see too many logs from third-party packages, be sure to run this config using logging.config.dictConfig(LOGGING_CONFIG) before the third-party packages are imported.

To add additional custom info to each log message using a logging filter, consider this answer.

Basic logging dictConfig in Python

The following code works perfectly for me:

import logging
import logging.config

log_dict = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
},
},
'handlers': {
'default': {
'level': 'INFO',
'formatter': 'standard',
'class': 'logging.StreamHandler',
},
'file_handler': {
'level': 'INFO',
'filename': '/tmp/mylogfile.log',
'class': 'logging.FileHandler',
'formatter': 'standard'
}
},
'loggers': {
'': {
'handlers': ['file_handler'],
'level': 'INFO',
'propagate': True
},
}
}
logging.config.dictConfig(log_dict)
logging.info("test")

And indeed, it's based on the answer mentioned above

Problems about python logging.config.dictConfig

The solution is just changing the position between logfile and log_handler.
Wrong code line: def init(self, logfile, log_handler):
Right code line: def init(self, log_handler, logfile):
Silly of me. Sorry to interrupt.

Which module should contain logging.config.dictConfig(my_dictionary)? What about my_dictionary?

So, I just finally figured it out.

It all works well if one puts the dictionary in the child module Module 1 (and set Propagate: True).

MY_DICTIONARY = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)-8s %(asctime)s %(module)s %(process)d %(thread)d %(message)s',
'datefmt': '%a, %d %b %Y %H:%M:%S'

},
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s',
'datefmt': '%a, %d %b %Y %H:%M:%S'

},
'simple': {
'format': '%(asctime)s %(levelname)-8s %(message)s',
'datefmt': '%a, %d %b %Y %H:%M:%S'
}
#etc.
}

}

Followed by the following calls:

logging.config.dictConfig(MY_DICTIONARY)
vmrunalllogger = logging.getLogger('VMRunAll_Python_Logger')

Then in Module 2 (the parent module), have this:

logging.config.dictConfig(MY_DICTIONARY)
mylogger = logging.getLogger('RunAll_Logger')

I could not find specific examples that showed two different modules logging to multiple files like mine is doing, but information from multiple sources like the following helped:

  1. From the Python documentation
  2. Another question on Stackoverflow.com
  3. And the examples of in the cookbook

Get current (or basic) python logging configuration as a dictionary

I'm not sure if there's a clean way do to this through any public logging function. Looking through the package CPython source, the defaults aren't set aside and neatly defined in any structure (such as a dict). Instead, they're hardcoded into the function logging.basicConfig. Specifically, this function contains some common "default" setup keywords, but unfortunately for your purposes, these are hardcoded into the function. For example:

style = kwargs.pop("style", '%')

This uses the root logger (which itself is root = RootLogger(logging.WARNING)) and adds some config to it.

One way to work with that, which is really not ideal, would be to look at the "before and after" config tied to logging.root before and after you call logging.basicConfig()*. For example:

>>> root = logging.root
>>> root.handlers
[]
>>> logging.basicConfig()
>>> root.handlers
[<StreamHandler <stderr> (NOTSET)>]

Lastly, I would mention that the default style comes from:

logging._STYLES['%']

which works out to %(levelname)s:%(name)s:%(message)s.

*If you call this function with no arguments, there is really not a whole lot done; the main change is adding a StreamHandler (stdout) to the root logger and then adding a Formatter to that.



Related Topics



Leave a reply



Submit