Nupur
Nupur

Reputation: 1081

Python logging dictionary config

I am trying to configure some logging for Python. From http://docs.python.org/howto/logging.html It is recommended that we use a YAML configuration file -

version: 1
formatters:
  simple:
    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
  console:
    class: logging.StreamHandler
    level: DEBUG
    formatter: simple
    stream: ext://sys.stdout
loggers:
  simpleExample:
    level: DEBUG
    handlers: [console]
    propagate: no
root:
  level: DEBUG
  handlers: [console]

In my code, I use this as -

import logging.config

logging.config.dictConfig('logging.config') 

However, on running it, I get an error

Traceback (most recent call last):
  File "TestLogger.py", line 62, in <module>
    Test5()  
  File "TestLogger.py", line 55, in Test5
    logging.config.dictConfig('logging.dict.2.config') 
  File "/usr/lib/python2.7/logging/config.py", line 776, in dictConfig
    dictConfigClass(config).configure()
  File "/usr/lib/python2.7/logging/config.py", line 380, in __init__
    self.config = ConvertingDict(config)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

So it seems that I am invoking the config file in an incorrect way. Just can't find the right way to invoke it. Help please? Thanks so much

Upvotes: 5

Views: 2743

Answers (1)

ecatmur
ecatmur

Reputation: 157374

You need to load and parse the YAML file:

import yaml
logging.config.dictConfig(yaml.load(open('logging.config', 'r')))

Upvotes: 11

Related Questions