如何整理不同进程的日志文件?

发布于 2024-12-23 23:35:35 字数 141 浏览 1 评论 0原文

我有一个用 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 技术交流群。

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

发布评论

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

评论(2

ˉ厌 2024-12-30 23:35:35

您没有说明需要在什么操作系统上运行,但对于 Linux 和 Mac OS,Ruby 的 syslog 是一个不错的选择。我不知道它是否在 Windows 上实现,但我认为没有。

内置文档不是很好,但是如果您查看源代码或“Ruby Syslog README”您将很好地了解如何使用它。

过去我使用了以下代码。出于您的目的,您需要将输出从 STDOUT 重新路由到此 syslog 方法。

require 'syslog'

def syslog(msg, level=:info)

  # if msg is an array, we assume it is a cmd, message pair.
  if (msg.is_a?(Array))
    msg = msg.join(' => ')
  end

  # escape all '%' using '%%'
  msg.gsub!('%', '%%')

  Syslog.open($0) { |s|
    case level
    when :crit, :critical
      s.notice(msg)
    when :emerg, :emergency
      s.emerg(msg)
    when :alert
      s.alert(msg)
    when :err, :error
      s.err(msg)
    when :warn, :warning
      s.warning(msg)
    when :notice
      s.notice(msg)
    when :info, :information
      s.info(msg)
    when :debug, :debugging
      s.debug(msg)
    end
  }
end

检查 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.

require 'syslog'

def syslog(msg, level=:info)

  # if msg is an array, we assume it is a cmd, message pair.
  if (msg.is_a?(Array))
    msg = msg.join(' => ')
  end

  # escape all '%' using '%%'
  msg.gsub!('%', '%%')

  Syslog.open($0) { |s|
    case level
    when :crit, :critical
      s.notice(msg)
    when :emerg, :emergency
      s.emerg(msg)
    when :alert
      s.alert(msg)
    when :err, :error
      s.err(msg)
    when :warn, :warning
      s.warning(msg)
    when :notice
      s.notice(msg)
    when :info, :information
      s.info(msg)
    when :debug, :debugging
      s.debug(msg)
    end
  }
end

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 using Syslog.open(File.basename($0)).

雪花飘飘的天空 2024-12-30 23:35:35

我最终决定使用log4r。它可以整理来自不同进程的日志并将它们添加到同一个日志文件中。

I finally decided to use log4r. It can collate logs from different processes and add them to the same log file.

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