I'm using a config file to configure my logger in a Python application. This is the file:
[loggers]
keys=root
[logger_root]
level=INFO
handlers=console
[handlers]
keys=console,file_rotating
[handler_console]
class=StreamHandler
level=WARNING
formatter=console
args=(sys.stderr,)
[handler_file_rotating]
class=TimeRotatingFileHandler
level=DEBUG
formatter=file
args=('../logs/twicker.log', 'd', 1, 5)
[formatters]
keys=console,file
[formatter_console]
format=%(levelname)s - %(message)s
[formatter_file]
format=%(asctime)s - %(levelname)s - %(module)s - %(message)s
My problem is with TimeRotatingFileHandler. Everytime I run the app I get the next error:
ImportError: No module named 'TimeRotatingFileHandler'
What I'm doing wrong? I tried also changing class line to class=handlers.TimeRotatingFileHandler
but in that case I get the next error:
ImportError: No module named 'handlers'
To extend on this a little if you decide to make a custom handler all you need to do is define that handler near the top of your code and then define it as an object in logging.handlers.
Example:
Now you can easily reference it in your config file.
I ran into the same problem when using
dictConfig
The solution for me was to fully qualify the module path like this:You might want to give that a try
The
class=
is evaluated in the namespace of thelogging
module, and by default this does not have a binding tohandlers
. So you could dobefore calling
fileConfig()
, and thenclass=handlers.TimedRotatingHandler
should work.