in the below code, I am calling log.py from call.py script as below. I have implemented logging handler where I set the log level in log.py and call the printlog function whenever I want to print the log to file. However, here I am trying to integrate the log rotation in log.py. That is incase a specific log file size is reached I need to rotate. Please assist with this. Thanks in Adv
log.py
import logging
import sys
import glob
import logging.handlers
def printlog(logtype,module,line,msg):
#Below fields to be set for the Log Level
loglevel = logging.DEBUG
logL1 = "logging.DEBUG"
#
LOG_FILENAME = 'logging_rotatingfile_example.out'
# Set up a specific logger with our desired output level
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(
LOG_FILENAME, maxBytes=2, backupCount=5)
my_logger.addHandler(handler)
#
#Setting config for Log Level
logging.basicConfig(
level=loglevel,
filename=LOG_FILENAME, #Setting Log file name & location
format='%(asctime)s %(levelname)-8s %(msg)s',
datefmt='%Y-%m-%d %H:%M:%S')
#print ("LOGTYPE = " +logtype)
#print ("LOGLEVEL = " +logL1)
str(line)
if ("INFO" in logtype):
#if (logtype in loglevel):
logging.info(" | " + module + " | " + str(line) + " | " + msg)
if("DEBUG" in logtype):
logging.debug(" | " + module + " | " + str(line) + " | " + msg)
call.py
import log
from inspect import currentframe, getframeinfo
#To Get line num
###Should Be added in all Files to get linenum
cf = currentframe()
filename = getframeinfo(cf).filename
##########
log.printlog("DEBUG",filename,cf.f_lineno,"Hello debug")
log.printlog("INFO",filename,cf.f_lineno,"Hello Info")