在 rails 应用程序中使用 syslog [英] Using syslog in rails application

查看:33
本文介绍了在 rails 应用程序中使用 syslog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  1. gem 'SyslogLogger' 添加到您的 Gemfile
  2. config/environments/production.rb
  3. 顶部添加require 'syslog_logger'
  4. 同时取消注释同一文件中的 config.logger = 行.
  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.

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

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.

这可能吗?

推荐答案

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

@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 由系统 syslog 守护进程在将消息转发到中央服务器之前设置.对于同一系统上的所有应用,它都是一样的,但随着您超过 1 个服务器,它可能会很方便.

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.

更有趣的是tag.您的应用在实例化 SyslogLogger 时定义了 tag.例如:

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

SyslogLogger.new('app1')

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

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.

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

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).

我们遇到了几个这样的情况,并做了一个直接生成 UDP 数据包的 Logger 替换.remote_syslog_logger gem 位于 在 GitHub 上.

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.

这篇关于在 rails 应用程序中使用 syslog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆