简体   繁体   English

Python TimedRotatingFileHandler - 日志文件名中的PID - 最佳方法

[英]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)
  1. Will this code maintain 7 Backups for each PID? 此代码是否会为每个PID维护7个备份?
  2. Is there a better way to split this so that my disk does not fill up with useless files? 有没有更好的方法来分割这个,以便我的磁盘不会填满无用的文件? Give that the PID might be unique for the processes over months. 假设PID可能在几个月内对于进程是唯一的。
  3. Is there a better approach to doing this? 有没有更好的方法来做到这一点?

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完成此操作而无需编写单独的清除/删除脚本吗?

  1. Yes, this will maintain 7 backups, or a weeks worth of logs, for each unique log path. 是的,这将为每个唯一的日志路径维护7个备份或一周的日志。
  2. Rotating file handlers are the correct way to put a limit on logs. 旋转文件处理程序是对日志进行限制的正确方法。
  3. As I said, rotating file handlers are the correct approach. 正如我所说,旋转文件处理程序是正确的方法。 I suppose you could use a 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.

相关问题 TimedRotatingFileHandler 更改文件名? - TimedRotatingFileHandler Changing File Name? 使用TimedRotatingFileHandler,如何使用与旋转相同的约定命名第一个日志文件? - Using TimedRotatingFileHandler, how to name the first log file by the same convention as the rotations? Python TimedRotatingFileHandler 不写入文件 - Python TimedRotatingFileHandler not writing to file TimedRotatingFileHandler日志的文件名约定? (蟒蛇) - Filename convention for TimedRotatingFileHandler log? (python) Python TimedRotatingFileHandler 记录到文件和标准错误 - Python TimedRotatingFileHandler logs to a file and stderr python3:timedrotatingfilehandler 日志轮换问题与多个脚本相同的日志文件 - python3: timedrotatingfilehandler log rotation issue with same log file with multiple scripts 如何使用 python 的 TimedRotatingFileHandler 强制旋转名称? - How to force a rotating name with python's TimedRotatingFileHandler? django+uwsgi 使用 TimedRotatingFileHandler 进行日志记录“覆盖旋转的日志文件” - django+uwsgi logging with TimedRotatingFileHandler "overwrites rotated log file" 如何使用logging.handlers.TimedRotatingFileHandler在当前日期之前创建日志文件 - how to create a log file by current date with logging.handlers.TimedRotatingFileHandler 如何使用 Django 和 Gunicorn 登录到文件? 使用 TimedRotatingFileHandler 会丢失日志 - How to log to file using Django and Gunicorn? Using TimedRotatingFileHandler misses logs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM