Python 日志记录重复日志消息

Dea*_*tar 1 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")
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

我无法找出我的 log(lvl, msg) 函数出了什么问题。某些原因导致它在每次调用时都会将消息复制到标准输出。我缺少什么?

提前致谢

Ore*_*lus 5

logging.getLogger('').addHandler(console)每次调用该函数时都会添加一个处理程序log,这就是您有重复消息的原因。