3

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?

Alain
  • 157
  • 1
  • 3
  • 14

2 Answers2

4

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
Community
  • 1
  • 1
Alain
  • 157
  • 1
  • 3
  • 14
  • Is 'VMRunAll_Python_Logger' defined in your 'loggers' section of the config or is it just the module name? – TanB Oct 03 '13 at 21:56
2

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

Community
  • 1
  • 1
sihrc
  • 2,728
  • 2
  • 22
  • 43
  • Thank you for the confirmation on import logging.config I tried my code at the end of the day last night and had an `AttributeError: 'module' object has no attribute 'config'` message. What you suggested to import happens to be the solution to the `AttributeError`. As to the example, I had seen it already. But thanks. Let me come back with fresh news in just a second. – Alain Jul 30 '13 at 15:13