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
To start logging using the Python logging module, the factory function logging. getLogger(name) is typically executed. The getLogger() function accepts a single argument - the logger's name. It returns a reference to a logger instance with the specified name if provided, or root if not.
Configuration dictionary schema. Describing a logging configuration requires listing the various objects to create and the connections between them; for example, you may create a handler named 'console' and then say that the logger named 'startup' will send its messages to the 'console' handler.
The qualname option is the name of the logger to log messages for; and the propagate option determines whether messages sent to this logger should also be sent to its parent logger.
You need to load and parse the YAML file:
import yaml
logging.config.dictConfig(yaml.load(open('logging.config', 'r')))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With