简体   繁体   中英

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

This is all Python. I am still learning...

I have written two modules for my Python application: Module1 and Module2 . I need to send my logging results to three different files. Module1 sends to setup.log and setupdetails.log files and Module2 sends to runall.log Module2 is the application I run. Within it is an import statement that calls Module1.

Because I have configured my logging in a my_dictionary , which one of the modules should contain the dictionary? Which one of the modules should contain the logging.config.dictConfig(my_dictionary) function?

Do you know of anywhere I could find a good script with an example of how to use dictConfig ?

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

For dictConfig:

import logging.config
logging.config.dictConfig() #Your method.

In terms of what you need to do, it might be easier if you posted some of your code.

Some documentation

An Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM