我在 Windows 上遇到了 python 日志记录模块的奇怪问题。下面的代码显示了它:
import multiprocessing
import logging
import sys
class ProcessLogger(multiprocessing.Process):
def __init__(self):
super().__init__()
self.create_logger()
print('state of logger in main proccess:')
print(self.logger)
print(self.logger.handlers)
def run(self):
print('state of logger in child proccess:')
print(self.logger)
print(self.logger.handlers)
def create_logger(self):
self.logger = logging.getLogger('something')
self.logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
self.logger.addHandler(handler)
if __name__ == '__main__':
logg = ProcessLogger()
logg.start()
logg.join()
print(sys.version)
它在 Windows 上打印以下输出:
state of logger in main proccess:
<Logger something (DEBUG)>
[<StreamHandler <stderr> (NOTSET)>]
state of logger in child proccess:
<Logger something (WARNING)>
[]
3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)]
由于某种原因,新进程中的记录器对象具有默认状态。
我在ubuntu上试过这个,它似乎按预期工作:
state of logger in main proccess:
<Logger something (DEBUG)>
[<StreamHandler <stderr> (NOTSET)>]
state of logger in child proccess:
<Logger something (DEBUG)>
[<StreamHandler <stderr> (NOTSET)>]
3.7.1 (default, Oct 22 2018, 11:21:55)
[GCC 8.2.0]
这是 Windows 上的预期行为吗?有人可以解释一下吗?