[英]Python TimedRotatingFileHandler - PID in Log File name - Best Approach
I wish to start multiple instances (Processes) of the Python Program and I want each one of them to write to their own log file. 我希望启动Python程序的多个实例(进程),我希望它们中的每一个都写入自己的日志文件。
The processes will be restarted atleast once daily. 这些过程将至少每天重启一次。
So i arrived at the following code. 所以我到达了以下代码。
logHandler = TimedRotatingFileHandler(os.path.join(os.path.dirname(sys.argv[0]),'logs/LogFile_'+str(os.getpid())+'.log'),when="midnight", backupCount=7)
What I would ideally like to have is that the logs pertaining to only 1 Week are maintained. 我理想的是,只保留1周的日志。 Can this be done using TimeRotatingFileHander without having to write a separate Purge/Delete script? 可以使用TimeRotatingFileHander完成此操作而无需编写单独的清除/删除脚本吗?
RotatingFileHandler
, but that rotates when the log hits a size, rather than at a particular time, so it doesn't allow you to specify a weeks worth of logs. 我想你可以使用RotatingFileHandler
,但是当日志达到一个大小时,而不是在特定时间,它会旋转,所以它不允许你指定一周的日志。 I'm a bit confused by how you're keeping the pid for a given process constant giving that the 'processes will be restarted at least once daily'. 我对你如何保持给定流程的pid不断给出'过程将至少每天重启一次'感到困惑。 A stronger guarantee that each process has a unique log path is to provide it explicitly as an argument, eg python script --log-file="$(pwd)/logs/LogFileProcX.log"
更强保证每个进程具有唯一的日志路径是明确地将其作为参数提供,例如python script --log-file="$(pwd)/logs/LogFileProcX.log"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.