[英]Python Logging Against Multiple Handlers Not Working
The intention here is to log to:这里的目的是登录到:
local log file (INFO)本地日志文件 (INFO)
console (DEBUG)控制台(调试)
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.