简体   繁体   English

Python 针对多个处理程序不工作的日志记录

[英]Python Logging Against Multiple Handlers Not Working

The intention here is to log to:这里的目的是登录到:

  1. local log file (INFO)本地日志文件 (INFO)

  2. console (DEBUG)控制台(调试)

  3. remote source (INFO)远程源(信息)

However my code only seems to write to both console/file source at the same log level so if I later on in my code call sessionLog.debug("This should be a debug line") in one function and sessionLog.info("This should be an info line") in another, both are logged to both the console and the file.但是,我的代码似乎只在同一日志级别写入控制台/文件源,所以如果我稍后在我的代码中调用 sessionLog.debug("This should be a debug line") 在一个 function 和 sessionLog.info("This应该是一个信息行") 在另一个中,两者都记录到控制台和文件中。 What I'm trying to make happen is that sessionLog.debug() would go to the console line.我想要实现的是 sessionLog.debug() 将 go 到控制台行。 What am I missing here?我在这里想念什么? I understand the remote source won't work for right now.我知道远程源现在无法使用。

import logging, logging.handlers

sessionLogFilename = datetime.now().strftime('LineGame_%H_%M_%d_%m_%Y.log')
logFormatFiles = ("ROBITLOG: " + '%(asctime)s - %(name)s - %(message)s')
logFormatConsole = ("ROBITLOG: " + '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
filelogformatter = logging.Formatter(logFormatFiles)
consolelogformatter = logging.Formatter(logFormatConsole)
fileLogger = logging.FileHandler(sessionLogFilename)
fileLogger.setLevel(logging.DEBUG)
fileLogger.setFormatter(filelogformatter)
consoleLogger = logging.StreamHandler(stdout)
consoleLogger.setLevel(logging.DEBUG)
consoleLogger.setFormatter(consolelogformatter)
remoteLoggerIP = "127.0.0.1"
remoteLoggerPort = 5124
remoteLogger = logging.handlers.SocketHandler(remoteLoggerPort,remoteLoggerPort)
remoteLogger.setLevel(logging.INFO)
logging.getLogger('').addHandler(fileLogger)
logging.getLogger('').addHandler(consoleLogger)
# logging.getLogger('').addHandler(remoteLogger)
sessionLog = logging.getLogger('')
sessionLog.setLevel(logging.INFO)

So the solution is that the parent logger (sessionLog) needs to at or a lower logging level than the other handlers.所以解决方案是父记录器(sessionLog)需要达到或低于其他处理程序的记录级别。 In this sessionLog needed to be set to a logging level of debug.在此 sessionLog 中需要设置为调试的日志记录级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 在不同的日志级别记录到多个处理程序? - Python logging to multiple handlers, at different log levels? python日志记录中同一日志文件的多个处理程序 - multiple handlers to same log file in python logging 信号处理程序和Python中的日志记录 - Signal handlers and logging in Python logging.handlers.WatchedFileHandler不起作用 - logging.handlers.WatchedFileHandler not working 没有basicConfig的日志处理程序将无法运行 - Logging handlers not working without basicConfig 将日志记录 basicConfig 与多个处理程序一起使用 - Using logging basicConfig with multiple handlers 使用python日志包,如何将其他格式插入到具有自己的格式化程序的多个记录器处理程序中? - Using python logging package, how to insert additional formatting into multiple logger handlers that have their own formatters? python日志记录:多个处理程序如何可以写入单个动态日志文件 - python logging:How multiple handlers Can write to a single dynamic log file python-日志记录模块-handlers.SysLogHandler-发送多行而不是一行 - python - logging module - handlers.SysLogHandler - sending multiple lines instead of one 如何使用带有多个处理程序的Python日志记录模块将一个日志记录发送到文件,将另一个日志记录发送到电子邮件 - How to send one log record to file and another to email using Python logging module with multiple handlers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM