Differences

This shows you the differences between two versions of the page.

Link to this comparison view

rotationlogging [2014/10/26 01:52] (current)
Line 1: Line 1:
 +===== 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:
  
 +<​code>​
 +
 +# 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=
 +</​code>​
 +
 +The code to perform logging with reading values for log.ini file is listed below.
 +
 +<​code>​
 +
 +#​!/​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))
 +</​code>​
 +----
 +* [[pythoninfo|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