简体   繁体   English

使用日志记录的 TimedRotatingFileHandler 创建.log 文件

[英]Creating .log files with logging's TimedRotatingFileHandler

I would expect the TimedRotatingFileHandler to create.log files, but instead it creates files without extension.我希望TimedRotatingFileHandler可以创建.log 文件,但它会创建没有扩展名的文件。 Is there any way I can set up the handler to generate log files?有什么办法可以设置处理程序来生成日志文件?

What I want in a nutshell: file_name.2019-10-09.log , instead of file_name.log.2019-10-09 .简而言之,我想要的是: file_name.2019-10-09.log ,而不是file_name.log.2019-10-09

From the class' documentation (emphasis mine):来自班级的文档(强调我的):

The system will save old log files by appending extensions to the filename .系统将通过将扩展名附加到文件名来保存旧的日志文件。 The extensions are date-and-time based, using the strftime format %Y-%m-%d_%H-%M-%S or a leading portion thereof, depending on the rollover interval.扩展是基于日期和时间的,使用 strftime 格式 %Y-%m-%d_%H-%M-%S 或其前导部分,具体取决于翻转间隔。

This, plus the implementation of the doRollover function show that the class will simply append the time to whatever filename the old log file has.这一点,加上doRollover function的实现表明 class 将简单地将 append 时间转换为旧日志文件具有的任何文件名。 There seems to be no way for you to force it to keep the same file extension.您似乎无法强制它保持相同的文件扩展名。

Here my code example, can ignore extra arguments.这里我的代码示例,可以忽略额外的 arguments。 I have in a Singleton class我有一个 Singleton class

from logging.handlers import RotatingFileHandler


rhandler = RotatingFileHandler("your_desired_FILENAME_logger.out" ,
                                       mode="a+",
                                       maxBytes=10*1024*1024,
                                       backupCount=5)

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

相关问题 logging.handler.TimedRotatingFileHandler 从不旋转日志 - logging.handler.TimedRotatingFileHandler never rotates the log 如何使用logging.handlers.TimedRotatingFileHandler在当前日期之前创建日志文件 - how to create a log file by current date with logging.handlers.TimedRotatingFileHandler django+uwsgi 使用 TimedRotatingFileHandler 进行日志记录“覆盖旋转的日志文件” - django+uwsgi logging with TimedRotatingFileHandler "overwrites rotated log file" Python日志记录创建额外的日志文件 - Python logging creating extra log files Python 日志记录未创建日志文件的多个实例 - Python logging is not creating multiple instance of log files 日志记录模块中的TimedRotatingFileHandler - TimedRotatingFileHandler in logging module 为什么logging.handlers.TimedRotatingFileHandler日志文件的创建时间在“漂移”,导致ETL头疼 - Why logging.handlers.TimedRotatingFileHandler log file create time is “drifting” causing headache for ETL TimedRotatingFileHandler未从帮助程序模块记录日志 - TimedRotatingFileHandler not logging from helper modules logging.handlers.TimedRotatingFileHandler 的错误? - Bug with with logging.handlers.TimedRotatingFileHandler? 使用带有logging.config的TimedRotatingFileHandler日志记录 - Using TimedRotatingFileHandler logging with a logging.config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM