在 Rails 应用程序中使用 syslog

发布于 2024-08-04 16:57:07 字数 548 浏览 4 评论 0原文

我正在考虑在我的 Rails 应用程序中使用 syslog。 这篇博文中概述了该过程:

  1. gem 'SyslogLogger' 添加到您的Gemfile
  2. require 'syslog_logger' 添加到 config/environments/Production.rb 的顶部,
  3. 同时取消注释 config.logger = 行。

在生产框中,我有 4 个使用乘客运行的轨道应用程序。如果我切换到对所有 4 个应用程序使用 syslogger,那么我担心来自所有 4 个应用程序的日志消息将转到单个文件,并且日志消息将交错。当然,我可以使用 splunk,但首先我想检查是否可以为每个 Rails 应用程序获取一个日志文件。这对于我的情况来说是理想的。

这可能吗?

I am thinking of using syslog in my rails applications. The process is outlined in this blog post:

  1. Add gem 'SyslogLogger' to your Gemfile
  2. Add require 'syslog_logger' to the top of config/environments/production.rb
  3. Also uncomment the config.logger = line in the same file.

In production box I have 4 rails applications running using passenger. If I switch to use syslogger for all 4 of my applications then I am afraid that the log messages from all 4 applications will go to a single file and the log messages will be interleaving. Of course, I can use splunk but first I wanted to check if it was possible for me to get one log file for each of my rails application. That would be desirable for my situation.

Is that possible?

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

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

发布评论

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

评论(2

青衫负雪 2024-08-11 16:57:07

@cite 的答案涵盖了区分应用程序的一种选项。然而,系统日志消息框架实际上有 2 个字段,使其变得更加容易:hostnametag(更常见的是并用作程序名称)。

主机名由系统syslog守护进程在将消息转发到中央服务器之前设置。对于同一系统上的所有应用程序来说,它都是相同的,但当您的服务器数量超过 1 台时,它可能会很方便。

更有趣的是标签。您的应用在实例化 SyslogLogger 时定义 tag。例如:

SyslogLogger.new('app1')

记录器类将作为 app1 发送到系统 syslogd,并且出现在本地日志文件和任何远程 syslog 目标中(无需修改日志消息本身)。默认为rails。所有现代系统日志守护进程都可以根据标签进行过滤;对于 syslog-ng,请参见 program();对于 rsyslog,请参见 $programname

另外,值得注意的是,SyslogLogger 基本上包装了 C openlog()syslog() 函数,因此基本上所有日志后配置都会发生在系统守护进程上。一般来说,这是可取的,但有时您可能希望 Rails 应用程序直接登录到特定目标(例如简化自动化部署、更改 syslog() 不允许的属性,或者在无法访问系统守护进程)。

我们遇到了一些这样的情况,并进行了一个直接的 Logger 替换,它可以自行生成 UDP 数据包。 remote_syslog_logger gem 在 GitHub 上

@cite's answer covers one option for distinguishing the apps. However, the syslog message framing actually has 2 fields that make it even easier: hostname and tag (more commonly known and used as program name).

hostname is set by the system syslog daemon before it forwards the message to a centralized server. It will be the same for all apps on the same system but may be handy as you grow past 1 server.

The more interesting one is tag. Your app defines tag when it instantiates SyslogLogger. For example:

SyslogLogger.new('app1')

The logger class will send to the system syslogd as app1, and that appear in both the local log file and any remote syslog destinations (without needing to modify the log message itself). The default is rails. All modern syslog daemons can filter based on tag; see program() for syslog-ng and $programname for rsyslog.

Also, it's worth noting that SyslogLogger is basically wrapping the C openlog() and syslog() functions, so basically all post-log configuration happens on the system daemon. Generally that's desirable, but sometimes you may want your Rails app to log directly to a specific destination (like to simplify automated deployment, change attributes not allowed by syslog(), or run in environments without access to the system daemon).

We ran into a couple of those cases and made a drop-in Logger replacement that generates the UDP packets itself. The remote_syslog_logger gem is on GitHub.

暗藏城府 2024-08-11 16:57:07

是的,默认情况下,几乎所有 Unix syslogds 都会将 userlocal* 设施中给出的消息写入同一文件中。但是,我知道的每个 syslogd 都允许您在每个设施的基础上指定日志文件,因此您可以将第一个应用程序日志记录到 local1.*,将第二个应用程序日志记录到 <代码>local2.*等等。

此外,像 syslog-ng 这样的较新的 syslog 守护进程允许通过根据正则表达式评估消息来将消息拆分到不同的文件(将其中包含 railsapp_1 的日志字符串写入 /var/log/railsapp_1.log 等等)。

因此,适当配置您的 syslogd 即可完成(更改该配置的详细细节应在 serverfault.com 如果你的系统手册页不能帮助你做到这一点。)

Yes, by default, almost all Unix syslogds will write messages given in the user or local* facility in the same file. However, every syslogd I know will allow you to specify logfiles on a per-facility basis, so you can have your first application log to local1.*, second one to local2.* and so on.

Furthermore, newer syslog daemons like syslog-ng allow for splitting messages to different files by evaluating the message against a regular expression (write log strings which have railsapp_1 in them to /var/log/railsapp_1.log and so on).

So, configure your syslogd appropriately and you are done (the gory details of changing that configuration should be asked on serverfault.com if your system's man pages don't help you doing it.)

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