如何使用日志记录模块避免重复输出

时间:2015-02-05 18:22:51

标签: python logging

我正在使用日志记录模块将我脚本中曾经是print语句的内容输出到控制台和日志文件。但是,每次运行脚本时,输出似乎都附加到控制台和日志中打印的内容,而不是覆盖那里的内容。例如,第一次运行脚本时,我在控制台和日志文件中获得此输出:

This print statement is going to both the console and the log
The year is: 2015

第二次运行脚本时,我在控制台中得到了这个:

This print statement is going to both the console and the log
This print statement is going to both the console and the log
The year is: 2015
The year is: 2015

第三次:

This print statement is going to both the console and the log
This print statement is going to both the console and the log
This print statement is going to both the console and the log
The year is: 2015
The year is: 2015
The year is: 2015

等..并且日志文件会不断添加,以便我最终得到:

This print statement is going to both the console and the log
The year is: 2015
This print statement is going to both the console and the log
This print statement is going to both the console and the log
The year is: 2015
The year is: 2015
This print statement is going to both the console and the log
This print statement is going to both the console and the log
This print statement is going to both the console and the log
The year is: 2015
The year is: 2015
The year is: 2015

我想要的是日志文件和控制台只给我以下内容,无论我重新运行脚本多少次:

This print statement is going to both the console and the log
The year is: 2015

注意:我目前能够得到我想要的唯一方法是在脚本运行之间关闭Python,但这不是一个实用的解决方案。

我已尝试使用flush()并尝试过这样的事情:

logger = logging.basicConfig(filemode='w', level=logging.DEBUG)

但是我无法使用我的代码。有人可以帮忙吗?这是我的代码:

import inspect
import logging
import datetime

def getDate():
    now = datetime.datetime.now()
    m = now.month
    d = now.day
    y = str(now.year)
    if m < 10:
        m = "0"+str(m)
    else:
        m = str(m)
    if d < 10:
        d = "0"+str(d)
    else:
        d = str(d)
    y = y[2:]
    formatted_date = m+d+y
    return formatted_date

def function_logger(file_level, console_level = None):
    function_name = inspect.stack()[1][3]
    logger = logging.getLogger(function_name)
    logger.setLevel(logging.DEBUG) #By default, logs all messages

    if console_level != None:
        ch = logging.StreamHandler() #StreamHandler logs to console
        ch.setLevel(console_level)
        ch_format = logging.Formatter('%(message)s')
        ch.setFormatter(ch_format)
        logger.addHandler(ch)

    log_name = 'Test_log' + getDate() + '.log'
    fh = logging.FileHandler(log_name.format(function_name))
    fh.setLevel(file_level)
    fh_format = logging.Formatter('%(message)s')
    fh.setFormatter(fh_format)
    logger.addHandler(fh)
    return logger

def f1():
    global logger
    year = '2015'
    logger.info("The year is: %s" % (year))

def main():

    global logger
    logger = function_logger(logging.INFO, logging.INFO)
    logger.info('This print statement is going to both the console and the log')
    f1()
    logging.shutdown()

if __name__ == '__main__':
    main()

2 个答案:

答案 0 :(得分:2)

感谢Lucas Graf,以下是我修复代码以完全符合我的要求的方法:

def function_logger(file_level, console_level = None):
    function_name = inspect.stack()[1][3]
    logger = logging.getLogger(function_name)
    logger.setLevel(logging.DEBUG) #By default, logs all messages
    log_name = 'Test_log_' + getDate() + '.log'

    if not logger.handlers:
        if console_level != None:
            ch = logging.StreamHandler() #StreamHandler logs to console
            ch.setLevel(console_level)
            ch_format = logging.Formatter('%(message)s')
            ch.setFormatter(ch_format)
            logger.addHandler(ch)

        fh = logging.FileHandler(log_name.format(function_name), mode='w')
        fh.setLevel(file_level)
        fh_format = logging.Formatter('%(message)s')
        fh.setFormatter(fh_format)
        logger.addHandler(fh)

    return logger

关于此修复,有三点需要注意:

1:我在测试条件下移动了两个addHandler:

if not logger.handlers:

2:我将模式='w'添加到了Filhandler:

fh = logging.FileHandler(log_name.format(function_name), mode='w')

3:我清除了main()底部的处理程序:

logger.handlers = []

答案 1 :(得分:0)

只需这样做:

def LOG_insere(file, format, text, level):
    infoLog = logging.FileHandler(file)
    infoLog.setFormatter(format)
    logger = logging.getLogger(file)
    logger.setLevel(level)
    
    if not logger.handlers:
        logger.addHandler(infoLog)
        if (level == logging.INFO):
            logger.info(text)
        if (level == logging.ERROR):
            logger.error(text)
        if (level == logging.WARNING):
            logger.warning(text)
    
    infoLog.close()
    logger.removeHandler(infoLog)
    
    return