简体   繁体   English

使用带有 basicConfig 和 RotatingFileHandler 的日志记录

[英]Using logging with basicConfig and RotatingFileHandler

I'm trying to use logging.basicConfig to log everything and logging.hanlers.RotatingFileHandler to have a maxBytes=1000000 and backupCount=5 .我正在尝试使用logging.basicConfig来记录所有内容,并使用logging.hanlers.RotatingFileHandler来获得maxBytes=1000000backupCount=5 The problem is that when I use RotatingFileHandler it'll log some duplicate outputs as well.问题是,当我使用RotatingFileHandler它也会记录一些重复的输出。 I tried removing basicConfig and only using RotatingFileHandler but it didn't log every message I wanted.我尝试删除basicConfig并且只使用RotatingFileHandler但它没有记录我想要的每条消息。

example:例子:

logging.basicConfig(format=f, datefmt=date, level=lvl, filename=file_name)
handler = logging.handlers.RotatingFileHandler(file_name, maxBytes=1000000, backupCount=5)
logging.getLogger('').addHandler(handler)

I changed that to remove basicConfig by:我改变了它以通过以下方式删除basicConfig

handler = logging.handlers.RotatingFileHandler(file_name, maxBytes=1000000, backupCount=5)
handler.setFormatter(f)
handler.setLevel(lvl)
logging.getLogger('').addHandler(handler)

The above didn't work as RotatingFileHandler didn't log every message that I wanted.以上不起作用,因为RotatingFileHandler没有记录我想要的每条消息。 Why?为什么?

The logger that you get from logging.getLogger('') has its logging level set by default to logging.WARNING .您从logging.getLogger('')获得的记录器的日志记录级别默认设置为logging.WARNING You need to set the desired level on the logger itself as well as on each handler you associate with it to ensure you log the desired messages.您需要在记录器本身以及与它关联的每个处理程序上设置所需的级别,以确保记录所需的消息。 Your second code snippet sets the level for the handler but not the logger.您的第二个代码片段设置了处理程序的级别,而不是记录器的级别。

https://docs.python.org/3/library/logging.html#logging.Logger.setLevel https://docs.python.org/3/library/logging.html#logging.Logger.setLevel

The default level of the root logger is logging.WARNING .根记录器的默认级别是logging.WARNING This causes messages which are less severe to be ignored (see Logging Levels ).这会导致不太严重的消息被忽略(请参阅日志级别)。 In order for the root logger to delegate all messages, its level needs to be set to logging.NOTSET .为了让根记录器委托所有消息,它的级别需要设置为logging.NOTSET

import logging
import logging.handlers

# Change root logger level from WARNING (default) to NOTSET in order for all messages to be delegated.
logging.getLogger('').setLevel(logging.NOTSET)

# Add file rotatin handler, with level DEBUG
rotatingHandler = logging.handlers.RotatingFileHandler(filename='rotating.log', maxBytes=1000, backupCount=5)
rotatingHandler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
rotatingHandler.setFormatter(formatter)
logging.getLogger('').addHandler(rotatingHandler)

log = logging.getLogger("myApp." + __name__)

log.debug('Hello, debug message.')
log.info('Hello, info message.')
log.warning('Hello, warning message.')
log.error('Hello, error message.')

You can combine the basicConfig and RotatingFileHandler through " logging.Formatter " , the below code snippet shows how to implement that.您可以通过“ logging.Formatter ”组合 basicConfig 和 RotatingFileHandler,下面的代码片段展示了如何实现它。 This will log all the messages and avoid duplicates.这将记录所有消息并避免重复。

import logging
import logging.handlers

#Set logger with required log level as base 
loggerInstance = logging.getLogger('name_of_logger')
loggerInstance.setLevel(logging.DEBUG)

# Configure the handler with filePath,maxBytes and backupCount
# maxBytes - theMaxSizeOfLogFileInBytes
# backupCount - numberOFBackUpFiles to be created ex: logFile1,logFile2 etc (after log rotation)
handler = logging.handlers.RotatingFileHandler(completeLogFilePathWithFileName,
                                               maxBytes=100000,
                                               backupCount=30)
#Specify the required format                                               
formatter = logging.Formatter('%(asctime)s %(filename)s %(funcName)s %(lineno)d %(levelname)s %(message)s')
#Add formatter to handler
handler.setFormatter(formatter)
#Initialize logger instance with handler
loggerInstance.addHandler(handler)

Use various options in logger to log once the loggerInstance is created.创建 loggerInstance 后,使用 logger 中的各种选项进行记录。

Ex:例如:

loggerInstance.debug("your debug message") #for debug
loggerInstance.error("your error message") #for error

for detailed documentation follow below link: https://docs.python.org/3/library/logging.handlers.html#rotatingfilehandle有关详细文档,请访问以下链接: https : //docs.python.org/3/library/logging.handlers.html#rotatingfilehandle

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM