Python日志记录会复制日志消息

时间:2016-05-10 09:35:56

标签: python logging duplicates

我尝试使用自己的函数将消息记录到sys.stdout和文件中以设置相同的格式。当我登录文件或在函数外部时,一切都按预期工作。当我将消息发送到我的函数时,我得到重复:

def log(lvl, msg):
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S',
                        filename='/tmp/test.log',
                        filemode='a')

    console = logging.StreamHandler(sys.stdout)
    console.setLevel(logging.INFO)
    formatter = logging.Formatter('%(levelname)-8s %(message)s')
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)
    logging.log(lvl, "%s: %s" % (options.build_node, msg))

if __name__ == "__main__":

    print "Executing outside of the function"
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S',
                        filename='/tmp/test.log',
                        filemode='a')
    console = logging.StreamHandler(sys.stdout)
    console.setLevel(logging.INFO)
    formatter = logging.Formatter('%(levelname)-8s %(message)s')
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)
    logging.log(logging.INFO, "some message")
    logging.log(logging.ERROR, "some error message")
    logging.log(logging.WARN, "Warning message")
    logging.log(logging.INFO, "another info message")     

    print "\nNow calling the log function"
    log(logging.INFO, "some message")
    log(logging.ERROR, "some error message")
    log(logging.WARN, "Warning message")
    log(logging.INFO, "another info message")

我得到的输出是:

Executing outside of the function
INFO     some message
ERROR    some error message
WARNING  Warning message
INFO     another info message

Now calling the log function
INFO     None: some message
INFO     None: some message
ERROR    None: some error message
ERROR    None: some error message
ERROR    None: some error message
WARNING  None: Warning message
WARNING  None: Warning message
WARNING  None: Warning message
WARNING  None: Warning message
INFO     None: another info message
INFO     None: another info message
INFO     None: another info message
INFO     None: another info message
INFO     None: another info message

并且日志文件内容符合预期:

2016-05-10 09:38:15 INFO     some message
2016-05-10 09:38:15 ERROR    some error message
2016-05-10 09:38:15 WARNING  Warning message
2016-05-10 09:38:15 INFO     another info message
2016-05-10 09:38:15 INFO     None: some message
2016-05-10 09:38:15 ERROR    None: some error message
2016-05-10 09:38:15 WARNING  None: Warning message
2016-05-10 09:38:15 INFO     None: another info message

我无法找出我的日志(lvl,msg)功能有什么问题。 某些东西导致它将消息传播到标准输出,每次调用都有重复。 我错过了什么?

提前致谢

2 个答案:

答案 0 :(得分:4)

每次调用函数logging.getLogger('').addHandler(console)时,

log都会添加一个处理程序,这就是你有重复邮件的原因。

答案 1 :(得分:3)

您只应该设置一次配置,而不是每次都记录。每次调用A value is trying to be set on a copy of a slice from a DataFrame都会向根记录器添加一个额外的控制台处理程序,以便消息数量不断增加。