Rotating Log file (logging)

#!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'''

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