python logging module:a quick question

Lie Ryan lie.1296 at gmail.com
Tue Dec 27 05:48:58 EST 2011


On 12/27/2011 05:26 PM, Littlefield, Tyler wrote:
> Hello all:
> I have a basic server I am working on, and wanted some input with an
> error I'm getting.
> I am initializing the logger like so:
> if __name__ == "__main__":
> observer = log.PythonLoggingObserver()
> observer.start()
> logging.basicConfig(filename='logs/server.log', level=logging.DEBUG,
> format='%(asctime)s [%(levelname)s] %(module)s:%(funcname)s:%(lineno)d
> %(message)s')
> logger = logging.getLogger()
> logger.addHandler(logging.handlers.TimedRotatingFileHandler)

You should pass an **instance** of the handler, not the class; so it 
should look like this:

logger.addHandler(logging.handlers.TimedRotatingFileHandler(...))

if you want to configure the handler, e.g. configure how often the log 
file is rotated, you can do it by passing parameters to the handler's 
constructure, e.g.:

logger.addHandler(logging.handlers.TimedRotatingFileHandler(filename='/foo/mylog.log', 
when='d', interval=3, backupCount=5))

will create a handler that will log to the file /foo/mylog.log and 
rotate the log every 3 days and keeps the last 5 logs for backup.

the logging handlers are documented over here: 
http://docs.python.org/library/logging.handlers.html#logging.handlers.TimedRotatingFileHandler

Note that if you're using a recent enough version of python, it's also 
possible to configure the logging module using a dictionary-based 
configuration. Dictionary-based configuration is generally simpler than 
code-based configuration.




More information about the Python-list mailing list