KeyError: 'handlers.RotatingFileHandler'

Peter Otten __peter__ at web.de
Mon Sep 12 13:05:33 EDT 2016


Daiyue Weng wrote:

> Hi, I am trying to use 'RotatingFileHandler' in a logging config file,
> 
> import logging
> import logging.config
> import logging.handlers
> 
> logging.config.fileConfig('logging.conf')
> 
> 
> [loggers]
> keys=root,ingestion_log
> 
> [handlers]
> keys=consoleHandler,fileHandler
> 
> [formatters]
> keys=ingestFormatter
> 
> [logger_root]
> level=DEBUG
> handlers=consoleHandler
> 
> [logger_ingestion_log]
> level=DEBUG
> handlers=handlers.RotatingFileHandler

The handlers listed above must be taken from the keys value in the 
[handlers] section. If you want a third logger you have to add a 
corresponding key there first under a name that you invented, e. g.

[handlers]
keys=consoleHandler,fileHandler,my_rotating_filehandler

You also need a

[handler_my_rotating_handler]

section where you specify the class etc. Only then you can reference it in 
the logger section as

[logger_ingestion_log]
handlers=my_rotating_filehandler

For the details have another look at

https://docs.python.org/dev/library/logging.config.html#logging-config-fileformat

> maxBytes=51200
> qualname=ingestion_log
> propagate=0
> 
> [handler_consoleHandler]
> class=StreamHandler
> level=DEBUG
> formatter=Formatter
> args=(sys.stdout,)
> 
> [handler_fileHandler]
> class=FileHandler
> level=DEBUG
> formatter=Formatter
> args=("log/logging.log",)
> 
> [formatter_Formatter]
> format=pathname~%(pathname)s||timestamp~%(asctime)s||level~%(levelname)s||
name~%(name)s||function_name~%(funcName)s||debug_message~%(message)s
> datefmt=%m/%d/%Y %H:%M:%S
> 
> 
> but Python generated a key error,
> 
> C:\Continuum\Anaconda3\lib\logging\config.py:85: in fileConfig
>     _install_loggers(cp, handlers, disable_existing_loggers)
> C:\Continuum\Anaconda3\lib\logging\config.py:254: in _install_loggers
>     logger.addHandler(handlers[hand])
> E   KeyError: 'handlers.RotatingFileHandler'
> 
> how to fix the errors?
> 
> thanks





More information about the Python-list mailing list