将标准输出从多个进程重定向到 python 日志记录模块

发布于 2024-10-05 09:06:52 字数 383 浏览 5 评论 0原文

我有一个 python 脚本,它使用 subprocess.Popen 启动多个用户进程。每个进程的标准输出都被重定向到一个唯一的文件。例如,我按如下方式启动每个进程,

proc = my_proc  
for p in range(1, max_p, 1):  
    log_file = proc + "_" + str(p) + ".log"  
    log = open(log_file, "w+")  
    subprocess.Popen([my_proc, p], shell = False, stdout = log)  

我想在这些文件变得太大时轮换它们。这样做的最好方法是什么?我想使用日志记录模块,但我认为这不是它的预期用途

谢谢

I have a python script that launches a number of user processes using subprocess.Popen. Each process stdout is redirected to a unique file. For example the I launch each process as follows

proc = my_proc  
for p in range(1, max_p, 1):  
    log_file = proc + "_" + str(p) + ".log"  
    log = open(log_file, "w+")  
    subprocess.Popen([my_proc, p], shell = False, stdout = log)  

I would like to rotate these files when they become too big. What would the best way of doing this? I would like to use the logging module but I dont think this is its intended use

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情泪▽动烟 2024-10-12 09:06:53

不是一个Python式的解决方案;但在 Linux 系统上,我更喜欢使用 logrotate 自动轮转我的日志。检查它是否安装在您的系统上(在 ubuntu 上,有一个名为 /etc/logrotate.d/ 的目录,其中的文件通过 cron 自动运行)。这可能比在应用程序内运行日志轮转更可取,也可能不更可取。

它是非常可配置的,例如,允许压缩较旧的文件,通过旋转 N 命令保留 N 个文件,当 cron 超过“大小 100k”时旋转,并且查看 man logrotate,它的设置非常简单。

从手册页来看,这是一个示例文件

   # sample logrotate configuration file
   compress

   /var/log/messages {
       rotate 5
       weekly
       postrotate
           /usr/bin/killall -HUP syslogd
       endscript
   }

   "/var/log/httpd/access.log" /var/log/httpd/error.log {
       rotate 5
       mail [email protected]
       size 100k
       sharedscripts
       postrotate
           /usr/bin/killall -HUP httpd
       endscript
   }

Not a pythonic solution; but on linux systems I prefer using logrotate to automatically rotate my logs. Check to see if its installed on your system (On ubuntu say there is a directory called /etc/logrotate.d/ with files automatically run via cron). This may or may not be preferred to having log rotation run from within the application.

It's very configurable, e.g., allows compression of older files keeps N files via rotate N command, rotates when the cron over "size 100k", and looking at man logrotate, its very straightforward to setup.

From the man page here's a sample file

   # sample logrotate configuration file
   compress

   /var/log/messages {
       rotate 5
       weekly
       postrotate
           /usr/bin/killall -HUP syslogd
       endscript
   }

   "/var/log/httpd/access.log" /var/log/httpd/error.log {
       rotate 5
       mail [email protected]
       size 100k
       sharedscripts
       postrotate
           /usr/bin/killall -HUP httpd
       endscript
   }
挽梦忆笙歌 2024-10-12 09:06:53

logging.handlers.RotatingFileHandler怎么样?

How about logging.handlers.RotatingFileHandler?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文