如何整理不同进程的日志文件?
我有一个用 Ruby 编写的应用程序,它在 STDOUT
上生成日志。该应用程序将在多个进程中运行,但我需要将从每个进程生成的日志整理到一个文件中。是否可以?
我在某处读到 Syslog 可以用于此目的,但我不确定如何使用它。
I have a application written in Ruby which generates a log on STDOUT
. The application would be running in multiple processes but I need to collate logs generated from each of the process into a single file. Is it possible?
I read somewhere that Syslog can be used for this but I am not sure how it can be used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有说明需要在什么操作系统上运行,但对于 Linux 和 Mac OS,Ruby 的
syslog
是一个不错的选择。我不知道它是否在 Windows 上实现,但我认为没有。内置文档不是很好,但是如果您查看源代码或“Ruby Syslog README”您将很好地了解如何使用它。
过去我使用了以下代码。出于您的目的,您需要将输出从 STDOUT 重新路由到此
syslog
方法。检查 syslog 手册页以获取有关各种日志记录级别的信息。
Syslog.open($0)
告诉 syslog 在插入记录时使用应用程序的完整路径。您可能希望使用Syslog.open(File.basename($0))
将$0
缩小为仅应用程序名称。You don't say what OS you need this to work on, but for Linux and Mac OS, Ruby's
syslog
is a good candidate. I don't know if it's implemented on Windows, but I don't think it was.The built-in documentation is not very good, but if you look at the source or "Ruby Syslog README" you'll get a good idea how to use it.
In the past I used the following code. For your purposes you'd want to reroute your output from STDOUT to this
syslog
method.Check the syslog man pages for information about the various logging levels.
Syslog.open($0)
tells syslog to use the entire path to your application when inserting records. You might want to shrink$0
to just the application name by usingSyslog.open(File.basename($0))
.我最终决定使用log4r。它可以整理来自不同进程的日志并将它们添加到同一个日志文件中。
I finally decided to use log4r. It can collate logs from different processes and add them to the same log file.