Differences

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

Link to this comparison view

rotatinglogfile [2014/10/26 01:52] (current)
Line 1: Line 1:
 +==== Rotating Log file (logging) ====
 +<​code>​
 +#!python
 +
 +# get file date / time info
 +
 +import os, sys, time
 +import logging
 +import logging.config
 +
 +
 +__log_name = '​pylog.log'​
 +__MAX_LOG_FILE_SIZE = 100 * 1000
 +
 +def checkLogFileSize(logFileName):​
 + fileSize = 0
 + try:
 + fileSize = os.path.getsize(logFileName)
 + print fileSize
 + except:
 +     print(__log_name + " not found or could not delete"​);​
 +
 + tryDelete = 0
 + print fileSize
 + tryDelete = (fileSize > __MAX_LOG_FILE_SIZE)
 + print tryDelete
 + if (tryDelete):​
 + try:
 + os.remove(logFileName+"​.1"​)
 + os.rename(logFileName,​ logFileName+'​.1'​)
 + except OSError,​inst:​
 + print "​OSError:"​+str(inst)
 + os.remove(logFileName+"​.1"​)
 + finally:
 + print "​finally"​
 + pass
 +
 +if __name__ == "​__main__":​
 + #  remember to update __log_name - this should be same name as defined in 
 + logConfigFile = '​py_logging.conf'​
 + checkLogFileSize(__log_name)
 + print "​Resume..."​
 + logging.config.fileConfig(logConfigFile)
 + __logger = logging.getLogger('​mylog'​)
 +
 + for i in range(300):
 + __logger.debug(str(i))
 + '''​print i'''​
 +</​code>​
 +
 +----
 +  * [[pythoninfo|Back to Python]]
  
rotatinglogfile.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