[英]Python logging repeated output
I've been studying python logging since a few days and I'm testing it today at work. 几天以来,我一直在研究python日志记录,今天我正在工作中对其进行测试。 I'm testing the following code: 我正在测试以下代码:
import inspect
import logging
def customLogger(FileHandlerlogLevel, ConsoleHandlerLogLevel, file_path_and_name):
'''
- logLevel input values: DEBUG, INFO, WARNING, ERROR, CRITICAL
'''
# Gets the name of the class / method from where this method is called
loggerName = inspect.stack()[1][3]
logger = logging.getLogger(loggerName)
logger.setLevel(logging.DEBUG)
fileHandler = logging.FileHandler(file_path_and_name, mode="w")
fileHandler.setLevel(FileHandlerlogLevel)
formatter = logging.Formatter('%(name)s - %(lineno)d - %(asctime)s - %(levelname)s - %(message)s',
datefmt="%m/%d/%Y %I:%M:%S %p")
fileHandler.setFormatter(formatter)
logger.addHandler(fileHandler)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(ConsoleHandlerLogLevel)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
return logger
test_logger = customLogger(logging.DEBUG, logging.DEBUG, "test_loggerv01.log")
test_logger.info("test 01")
So basically I have two handlers for a given logger. 因此,基本上,对于给定的记录器,我有两个处理程序。 When I run the script the first time, this is what the console (Spyder) shows: 第一次运行脚本时,这是控制台(Spyder)显示的内容:
In [1]: runfile('C:/Users/aardem/Python_AA/AB/Corproates_Prj/custom_logger_andrea_v02.py', wdir='C:/Users/aardem/Python_AA/AB/Corproates_Prj')
<module> - 36 - 07/19/2018 06:32:22 PM - INFO - test 01
So far so good. 到现在为止还挺好。 Then, I change the info message to "test 02" (last line of my code), save and re-run the script. 然后,将信息消息更改为“ test 02”(代码的最后一行),保存并重新运行脚本。 Now, the console shows the following: 现在,控制台显示以下内容:
In [2]: runfile('C:/Users/aardem/Python_AA/AB/Corproates_Prj/custom_logger_andrea_v02.py', wdir='C:/Users/aardem/Python_AA/AB/Corproates_Prj')
<module> - 36 - 07/19/2018 06:32:34 PM - INFO - test 02
<module> - 36 - 07/19/2018 06:32:34 PM - INFO - test 02
Again, if I change the last line of my script to: 同样,如果我将脚本的最后一行更改为:
test_logger.info("test 03")
then save and re-run the script, this is the new console output: 然后保存并重新运行脚本,这是新的控制台输出:
In [3]: runfile('C:/Users/aardem/Python_AA/AB/Corproates_Prj/custom_logger_andrea_v02.py', wdir='C:/Users/aardem/Python_AA/AB/Corproates_Prj')
<module> - 37 - 07/19/2018 06:38:20 PM - INFO - test 03
<module> - 37 - 07/19/2018 06:38:20 PM - INFO - test 03
<module> - 37 - 07/19/2018 06:38:20 PM - INFO - test 03
Can somebody help me with this unexpected problem? 有人可以帮我解决这个意外的问题吗? The same problem happens with the data saved inside the file "test_loggerv01.log". 保存在文件“ test_loggerv01.log”中的数据也会发生相同的问题。 Thank you 谢谢
Every time you run that file with runfile
in the same IPython session, you're adding more and more handlers to the same logger. 每次在同一个IPython会话中使用runfile
运行该文件时,都会向同一个记录器添加越来越多的处理程序。 The old handlers don't go away. 旧的处理程序不会消失。
All those duplicate handlers will handle that logger's log records, resulting in duplicate log handling. 所有那些重复的处理程序将处理该记录器的日志记录,从而导致重复的日志处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.