在 Rails 应用程序中使用 syslog
我正在考虑在我的 Rails 应用程序中使用 syslog。 这篇博文中概述了该过程:
- 将
gem 'SyslogLogger'
添加到您的Gemfile
- 将
require 'syslog_logger'
添加到config/environments/Production.rb
的顶部, - 同时取消注释
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:
- Add
gem 'SyslogLogger'
to yourGemfile
- Add
require 'syslog_logger'
to the top ofconfig/environments/production.rb
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@cite 的答案涵盖了区分应用程序的一种选项。然而,系统日志消息框架实际上有 2 个字段,使其变得更加容易:
hostname
和tag
(更常见的是并用作程序名称)。主机名
由系统syslog守护进程在将消息转发到中央服务器之前设置。对于同一系统上的所有应用程序来说,它都是相同的,但当您的服务器数量超过 1 台时,它可能会很方便。更有趣的是
标签
。您的应用在实例化SyslogLogger
时定义tag
。例如:记录器类将作为
app1
发送到系统 syslogd,并且出现在本地日志文件和任何远程 syslog 目标中(无需修改日志消息本身)。默认为rails
。所有现代系统日志守护进程都可以根据标签
进行过滤;对于 syslog-ng,请参见program()
;对于 rsyslog,请参见$programname
。另外,值得注意的是,
SyslogLogger
基本上包装了 Copenlog()
和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
andtag
(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 definestag
when it instantiatesSyslogLogger
. For example: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 israils
. All modern syslog daemons can filter based ontag
; seeprogram()
for syslog-ng and$programname
for rsyslog.Also, it's worth noting that
SyslogLogger
is basically wrapping the Copenlog()
andsyslog()
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 bysyslog()
, 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. Theremote_syslog_logger
gem is on GitHub.是的,默认情况下,几乎所有 Unix
syslogds
都会将user
或local*
设施中给出的消息写入同一文件中。但是,我知道的每个 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 theuser
orlocal*
facility in the same file. However, everysyslogd
I know will allow you to specify logfiles on a per-facility basis, so you can have your first application log tolocal1.*
, second one tolocal2.*
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 haverailsapp_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.)