gpt4 book ai didi

Python 记录多个文件

转载 作者:太空狗 更新时间:2023-10-30 02:49:09 25 4
gpt4 key购买 nike

我的 Python 日志记录模块有问题,我不确定我是否在做一些愚蠢的事情,但我有两个 Python 脚本,一个 (master.py) 调用另一个 (slave.py)。它们都记录到单独的日志文件,但被调用的第二个脚本 (slave.py) 似乎递归记录!

谁能看出我在这里做错了什么?

这是我的代码:

# master.py

import sys
import logging
import slave

masterLog = logging.getLogger('master')
masterLog.setLevel(logging.DEBUG)
masterHandler = logging.FileHandler('master.log')
formatter = logging.Formatter('%(levelname)s: %(asctime)s %(funcName)s(%(lineno)d) -- %(message)s', datefmt = '%Y-%m-%d %H:%M:%S')
masterHandler.setFormatter(formatter)
masterLog.addHandler(masterHandler)
masterLog.info('This is masterLog running on master.py')
print 'master.py has: ', len(masterLog.handlers), 'handlers'

for i in range(1,6):
masterLog.info('Running slave %s' % i)
slave.runMain()

# slave.py

import sys
import logging

def runMain():
slaveLog = logging.getLogger('slave')
slaveLog.setLevel(logging.DEBUG)
slaveHandler = logging.FileHandler('slave.log')
formatter = logging.Formatter('%(levelname)s: %(asctime)s %(funcName)s(%(lineno)d) -- %(message)s', datefmt = '%Y-%m-%d %H:%M:%S')
slaveHandler.setFormatter(formatter)
slaveLog.addHandler(slaveHandler)
slaveLog.info('This is slaveLog running on slave.py')
print 'slave.py has: ', len(slaveLog.handlers), 'handlers'

if __name__ == '__main__':
runMain()

这是输出:

master.py has:  1 handlers
slave.py has: 1 handlers
slave.py has: 2 handlers
slave.py has: 3 handlers
slave.py has: 4 handlers
slave.py has: 5 handlers

和 master.log:

INFO: 2012-02-29 13:26:49 <module>(13) -- This is masterLog running on master.py
INFO: 2012-02-29 13:26:49 <module>(17) -- Running slave 1
INFO: 2012-02-29 13:26:49 <module>(17) -- Running slave 2
INFO: 2012-02-29 13:26:49 <module>(17) -- Running slave 3
INFO: 2012-02-29 13:26:49 <module>(17) -- Running slave 4
INFO: 2012-02-29 13:26:49 <module>(17) -- Running slave 5

还有slave.log:

INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py
INFO: 2012-02-29 13:26:49 runMain(13) -- This is slaveLog running on slave.py

从属脚本每次调用时似乎都会添加一个新的文件处理程序。 slave.log 文件中应该只有 5 个条目,但每次调用 slave.py 时记录器输出到每个文件处理程序!

谢谢, jack

最佳答案

logging.getLogger([name]) 总是返回同一个对象(一个全局对象),并且每次调用 runMain 时都会对其调用 addHandler。

关于Python 记录多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9500155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com