使用Python记录日志。需要整合

时间:2018-01-09 06:22:30

标签: python logging log-rotation

在下面的代码中,我从call.py脚本调用log.py,如下所示。     我已经实现了日志记录处理程序,我在log.py中设置了日志级别,并且每当我想将日志打印到文件时都调用printlog函数。     但是,这里我试图在log.py中集成日志循环。这是因为我需要旋转特定的日志文件大小。请协助。感谢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")

0 个答案:

没有答案