0

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")
Klaus D.
  • 13,874
  • 5
  • 41
  • 48
rocker
  • 1
  • 1
  • 1
    Just a quick question, how often do you think, you will have to rotate a log file? Every Day? Every Week? Every Month? Year? – user1767754 Jan 09 '18 at 06:35
  • What is the problem at hand? I see that the `RotatingFileHandler` is already in effect with `maxBytes=2, backupCount=5`. Am I missing something? – Eduard Jan 09 '18 at 06:36
  • Btw, why wouldn't you just get the logger instance from call.py instead? `logger = logging.getLogger('MyLogger')` then simply `logger.info("Hello Info")`. You can also modify the log format based on the current usage. Say for instance, include pathname and lineno when on debug mode, and exclude them when your simply runnning the application on a normal routine? – Eduard Jan 09 '18 at 06:41
  • Q1 - I need logrotation based on the file size. Like whenever file reaches a size it should rotate.Not by day/week. Q2 - It's in effect, but not working, I integrated the RotatingFileHandler in my code, the rotatingfilehandler part in the code to be integrated with the rest of the code – rocker Jan 09 '18 at 07:35
  • Q3 - This is a good solution as well but how to implement this as I am new to python. I think 'MyLogger' is a class which I need to add, but I took it from an example available online. So not sure how it would work. I think if I define MyLogger as a class then it will work. Please correct me if i'm wrong – rocker Jan 09 '18 at 07:41

0 Answers0