我想使用python中的线程调用一个以日志文件名为参数的函数
上面的问题是output1.log正在为所有t1、t2和t3线程记录3条日志消息。我希望output1.log仅包含t1线程日志,并在此行中分别包含:我想使用python中的线程调用一个以日志文件名为参数的函数,python,logging,Python,Logging,上面的问题是output1.log正在为所有t1、t2和t3线程记录3条日志消息。我希望output1.log仅包含t1线程日志,并在此行中分别包含: import threading import logging, logging.handlers class FuncThread(threading.Thread): def __init__(self, target, *args): self._target = target self._arg
import threading
import logging, logging.handlers
class FuncThread(threading.Thread):
def __init__(self, target, *args):
self._target = target
self._args = args
threading.Thread.__init__(self)
def run(self):
self._target(*self._args)
def someOtherFunc(data, key, testlogfile):
#initialize logging system
testlogger = logging.getLogger("testlogger")
testlogger.setLevel(logging.DEBUG)
file = open(testlogfile,'w')
file.close()
# This handler writes everything to a file.
h1 = logging.FileHandler(testlogfile)
f = logging.Formatter("%(levelname)s %(asctime)s %(funcName)s %(lineno)d %(message)s")
h1.setFormatter(f)
h1.setLevel(logging.DEBUG)
testlogger.addHandler(h1)
testlogger = logging.getLogger("testlogger")
testlogger.debug("someOtherFunc was called : data=%s; key=%s" % (str(data), str(key)))
t1 = FuncThread(someOtherFunc, [1,2], 4, "output1.log")
t1.start()
t1.join()
t2 = FuncThread(someOtherFunc, [2,4], 8, "output2.log")
t2.start()
t2.join()
t3 = FuncThread(someOtherFunc, [3,6], 9, "output3.log")
t3.start()
t3.join()
每个线程都获得相同的记录器。尝试将其更改为:
testlogger = logging.getLogger("testlogger")
我试图更改它,但没有任何帮助。这次没有捕获日志。只创建了空日志文件。解决了它。我需要像此testlogger一样删除最后的处理程序。removeHandler(h1)这个问题有什么离题之处?
testlogger = logging.getLogger(testlogfile)