Python Logging example of RotationFileHandler

Python logging is easily configured with a config file. The following file configure it for File Rotation based on size:

# log.ini

[loggers]
keys=root

[handlers]
keys=default

[formatters]
keys=default

[logger_root]
level=INFO
handlers=default
qualname=(root) # note - this is used in non-root loggers
propagate=1 # note - this is used in non-root loggers
channel=
parent=

[handler_default]
class=handlers.RotatingFileHandler
level=INFO
formatter=default
args=('l.log', 'a', 500000, 10)
filename=l.log

[formatter_default]
format=F2 %(asctime)s %(pathname)s(%(lineno)d): %(levelname)s %(message)s
datefmt=

The code to perform logging with reading values for log.ini file is listed below.

#!/usr/bin/python

import os
import sys
import logging
import logging.config

# main
if __name__ == "__main__":	# if main
	print "main"
	logging.config.fileConfig('log.ini')
	moduleName = 'X'
	logger = logging.getLogger(moduleName)
	for i in range(500000):
		logger.info('value = '+str(i))

* Back to Python

rotationlogging.txt · Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0