dictConfig: logging.StreamHandler object is not iterable.
Tim Williams
tjandacw at cox.net
Wed May 24 15:42:34 EDT 2017
On Wednesday, May 24, 2017 at 2:46:54 PM UTC-4, Tim Williams wrote:
> (Apologies for using Google Groups to post)
>
> I'm trying to use dictConfig to configure logging. I keep running into the error that the logging.StreamHandler object is not iterable.
>
> I'm using Python 3.4.3 on a Windows 7 box.
>
> C:\Python34\python.exe 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)]
>
>
> I want to use the configobj module to create the dictionary from an INI file, but that's not the problem I'm having.
>
> Here is my test code:
> #############
> import logging, logging.config, logging.handlers
> import configobj
>
> # config = configobj.ConfigObj('loggingtest.ini')
> # config['version']=eval(config['version'])
> config = {
> 'version': 1,
> 'level': 'INFO',
> 'formatters': {'fmt1': {'format': '%(asctime)s: (%(levelname)s) %(message)s',
> 'datefmt': ''}
> },
> 'loggers': {'root': {'level': 'INFO',
> 'handlers': 'cfg://handlers.console'},
> 'file': {'level': 'WARN',
> 'handlers': 'cfg://handlers.file'}
> },
> 'handlers': {'console': {'class': 'logging.StreamHandler',
> 'level': 'INFO',
> 'stream': 'ext://sys.stdout'},
> 'file': {'class': 'logging.FileHandler',
> 'level': 'WARN',
> 'filename': 'test.log'}
> },
> }
>
>
> logging.config.dictConfig(config)
> ################
>
> When I run it, I get this traceback:
>
> Traceback (most recent call last):
> File "C:\Python34\lib\logging\config.py", line 611, in configure
> self.configure_logger(name, loggers[name])
> File "C:\Python34\lib\logging\config.py", line 775, in configure_logger
> self.common_logger_config(logger, config, incremental)
> File "C:\Python34\lib\logging\config.py", line 767, in common_logger_config
> self.add_handlers(logger, handlers)
> File "C:\Python34\lib\logging\config.py", line 748, in add_handlers
> for h in handlers:
> TypeError: 'StreamHandler' object is not iterable
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
> File "L:\workspace\MyPython\src\testlog.py", line 27, in <module>
> logging.config.dictConfig(config)
> File "C:\Python34\lib\logging\config.py", line 789, in dictConfig
> dictConfigClass(config).configure()
> File "C:\Python34\lib\logging\config.py", line 614, in configure
> '%r: %s' % (name, e))
> ValueError: Unable to configure logger 'root': 'StreamHandler' object is not iterable
>
>
> I even tried creating a JSON file to create the dictionary, and got the same error.
>
> Thanks for any help
> --
> Tim
Peter,
Thanks. That did it for my dictionary.
What I'm really trying to do is use configobj to do this from an INI file. The one problem is that all the values are strings, hence the
config['version']=eval(config['version'])
It looks like if I do something similar with the handlers in the loggers, like:
config['loggers']['root']['handlers']=eval(config['loggers']['root']['handlers'])
config['loggers']['file']['handlers']=eval(config['loggers']['file']['handlers'])
logging.config.dictConfig(dict(config))
seems to work.
I'm using configobj instead of configparser because configobj can nest sections. The INI file I'm trying to build has more sections for other things.
version = 1
level = INFO
[formatters]
[[fmt1]]
format = "%(asctime)s: (%(levelname)s) %(message)s"
datefmt =
[handlers]
[[console]]
class = logging.StreamHandler
level = INFO
stream = ext://sys.stdout
[[file]]
class = logging.FileHandler
level = WARN
filename = test.log
[loggers]
[[root]]
level = INFO
handlers = [console]
[[file]]
level = WARN
handlers = ['file']
propagate = 1
More information about the Python-list
mailing list