python日志模块的日志记录问题
我正在使用python日志模块。我正在初始化具有以下数据的文件python日志模块的日志记录问题,python,logging,Python,Logging,我正在使用python日志模块。我正在初始化具有以下数据的文件 def initialize_logger(output_dir): ''' This initialise the logger :param output_dir: :return: ''' root = logging.getLogger() root.setLevel(logging.INFO) format = '%(asctime)s - %(levelna
def initialize_logger(output_dir):
'''
This initialise the logger
:param output_dir:
:return:
'''
root = logging.getLogger()
root.setLevel(logging.INFO)
format = '%(asctime)s - %(levelname)-8s - %(message)s'
date_format = '%Y-%m-%d %H:%M:%S'
if 'colorlog' in sys.modules and os.isatty(2):
cformat = '%(log_color)s' + format
f = colorlog.ColoredFormatter(cformat, date_format,
log_colors={'DEBUG': 'green', 'INFO': 'green',
'WARNING': 'bold_yellow', 'ERROR': 'bold_red',
'CRITICAL': 'bold_red'})
else:
f = logging.Formatter(format, date_format)
#ch = logging.FileHandler(output_dir, "w")
ch = logging.StreamHandler()
ch.setFormatter(f)
root.addHandler(ch)
因为只有一个streamHandler,但是我在控制台上得到了两个指纹
INFO:root:clearmessage:%ss1=00
2017-12-21 17:07:20 - INFO - clearmessage:%ss1=00
INFO:root:clearmessage:%ss2=00
2017-12-21 17:07:20 - INFO - clearmessage:%ss2=00
每条消息都打印为
Root
和Info
。你知道我为什么要拍两张照片吗。在上面的代码中,您可以忽略颜色代码。您有两个处理程序。在添加新的处理程序之前,请清除这些处理程序:
root.handlers = [] # clears the list
root.addHandler(ch) # adds a new handler
可能您调用initialize_logger()twice,但我看到这两条消息都是以不同的消息格式打印的。我调用另一个语句,如addHandler(logging.NullHandler())。这会不会影响它的工作。在代码的某个地方,我可能会调用它两次。然而,它起了作用。你知道python中的html记录器吗?我认为
getLogger()
调用将隐式创建一个处理程序。