红宝石与系统日志和定制设施
我对系统日志很陌生。
我们决定使用 syslog 来跟踪 Rails 应用程序中的一些特殊事件。
问题是我不想使用默认的 /var/log/system.log
文件,而是使用自定义文件,例如 /var/log/myapp_events.log
。
我发现为此我必须在 /etc/syslog.conf
中定义自己的设施,如下所示:
myapp_events.* /var/log/myapp_events.log
重新启动 syslogd 后,我发现我可以直接在 bash 控制台中使用它:
syslog -s -k Facility myapp_events Message "this is my message"
该消息按预期显示在 /var/log/myapp_events.log
中,但我无法使用 syslog ruby gem 重现此行为。我尝试过:
require 'syslog'
Syslog.open('myapp_events', Syslog::LOG_PID | Syslog::LOG_CONS) { |s| s.warning 'this is my message' } # sends the message to system.log
Syslog.open('myapp_events', Syslog::LOG_PID | Syslog::LOG_CONS, 'myapp_events') { |s| s.warning 'this is my message' } # error because 'myapp_event' can't be converted to int.
我看到 Syslog.open
有第三个参数,它是 facility 但它必须是一个整数,而我拥有的是一个字符串。
有什么建议吗?
I am very new playing with syslog.
We have decided to use syslog to track some special events in our Rails application.
The problem is that I don't want to use the default /var/log/system.log
file but use a custom one like /var/log/myapp_events.log
.
I see that for that I have to define my own facility in /etc/syslog.conf
like this:
myapp_events.* /var/log/myapp_events.log
After restarting syslogd I see that I can play with it directly into the bash console:
syslog -s -k Facility myapp_events Message "this is my message"
The message appears into the /var/log/myapp_events.log
as expected, but I cannot reproduce this behavior using the syslog ruby gem. I have tried:
require 'syslog'
Syslog.open('myapp_events', Syslog::LOG_PID | Syslog::LOG_CONS) { |s| s.warning 'this is my message' } # sends the message to system.log
Syslog.open('myapp_events', Syslog::LOG_PID | Syslog::LOG_CONS, 'myapp_events') { |s| s.warning 'this is my message' } # error because 'myapp_event' can't be converted to int.
I see that Syslog.open
has a third argument which is the facility but it has to be an integer and what I have is a string.
Any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当然,
syslog
ruby 实现不允许我们使用自定义设施。syslog
ruby 实现使用syslog
[C 实现] (http://github.com/ruby/ruby/blob/trunk/ext/syslog/syslog.c#L36)。syslog
C 实现仅允许我们使用非常短的设施名称列表:LOG_USER、LOG_MAIL、LOG_DAEMON、LOG_AUTH、LOG_SYSLOG、LOG_LPR、LOG_NEWS、LOG_UUCP、UUCP、LOG_CRON、LOG_AUTHPRIV、LOG_FTP 、LOG_LOCAL0、LOG_LOCAL1、LOG_LOCAL2、LOG_LOCAL3、LOG_LOCAL4、LOG_LOCAL5、LOG_LOCAL6、LOG_LOCAL7
。所以最后我所做的是使用已经供个人使用的
LOG_LOCALX
设施之一。现在我可以像这样配置 syslog:
在 Ruby 中这样做:
我认为这是
syslog
希望您定义自定义设施的方式。Definitely the
syslog
ruby implementation doesn't allow us to use custom facilities.The
syslog
ruby implementation is using thesyslog
[C implementation] (http://github.com/ruby/ruby/blob/trunk/ext/syslog/syslog.c#L36).The
syslog
C implementation only allows us to use a very short list of facility names:LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_SYSLOG, LOG_LPR, LOG_NEWS, LOG_UUCP, UUCP , LOG_CRON, LOG_AUTHPRIV, LOG_FTP, LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5, LOG_LOCAL6, LOG_LOCAL7
.So in the end what I did was to use one of the
LOG_LOCALX
facilities that are already there for personal use.Now I can configure syslog like this:
And in Ruby do this:
I think is the way that
syslog
wants you to define custom facilities.当您有多个工作线程或线程时,记录器没有用,因为日志消息是交错的。这就是为什么 Rails 3 中的默认记录器是 BufferedLogger。
这也是为什么你必须使用像 rsyslog 这样的缓冲系统记录器,否则它会降低你的性能。 (我相信 syslogd 使用 fsync() 这是一个等待返回的同步调用。)
Logger isn't useful when you have multiple workers or threads, since the log messages get interleaved. This is why the default logger in Rails 3 is BufferedLogger.
This is also why you must use a buffering syslogger like rsyslog, or it will kill your performance. (I believe syslogd uses fsync() which is a synchronous call that waits to return.)
看看 Lumberjack。
它支持 Syslog 作为日志设备,并且您可以指定设施作为选项:
Lumberjack 还有很多其他功能值得一看。
Take a look at Lumberjack.
It has support for Syslog as a log device, and you can specify facility as an option:
Lumberjack also has quite a few other features that makes it worth a look.
您可能想考虑使用
Logger
,与syslog非常相似,只是更加人性化一些。它具有多个关键级别,如 Syslog,并提供基于大小或寿命的日志滚动。You might want to look into using
Logger
, which is very similar to syslog, only it is a bit more user-friendly. It has multiple levels of criticality, like Syslog, and offers log rolling based on size or age.