在 python 日志文件中包含当前日期
我有一个每天运行的流程。 它使用 Python 日志记录。 如何配置 python 日志记录模块以写入文件名中包含当前日期的文件?
因为我每天早上都会重新启动该过程,所以 TimedRotatingFileHandler 将无法工作。 该项目较大,因此我有兴趣保留登录日志配置文件所需的代码。
I have a process that I run every day. It uses Python logging. How would I configure the python logging module to write to a file containing the current date in the file name?
Because I restart the process every morning the TimedRotatingFileHandler won't work. The project is larger, so I would be interested in keeping the code required for logging into a logging configuration file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 TimedRotatingFileHandler。 例如:
但这可能只有在您的程序运行超过一天的情况下才有效。 如果您的脚本每天在 cron 作业中启动,您最好手动格式化传递给日志处理程序的文件名以包含时间戳。
You can use the TimedRotatingFileHandler. For example:
But this will probably only work if your program runs for more than one day. If your script starts daily in a cron job you're better off manually formatting the filename which you pass to your log handler to include a timestamp.
您可能对 TimedRotatingFileHandler 感兴趣,请参阅 http://docs.python。 org/library/logging.html#logging.handlers.TimedRotatingFileHandler
You may be interested in the TimedRotatingFileHandler see http://docs.python.org/library/logging.html#logging.handlers.TimedRotatingFileHandler