Ruby & Syslog & custom facility

I am very new to syslog.

We decided to use syslog to track some special events in our Rails application.

The problem is that I do not 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 this I need to define my own tool in /etc/syslog.conf as follows:

 myapp_events.* /var/log/myapp_events.log 

After rebooting syslogd, I see that I can play with it directly in the bash console:

 syslog -s -k Facility myapp_events Message "this is my message" 

A message will appear in /var/log/myapp_events.log as expected, but I cannot reproduce this behavior using syslog ruby ​​gem . I 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 a tool , but it must be an integer, and I have a line.

Any suggestion?

+4
source share
4 answers

Specifically, the rubi syslog implementation does not allow us to use custom tools .

The rubi implementation of syslog uses syslog [C implementation] ( http://github.com/ruby/ruby/blob/trunk/ext/syslog/syslog.c#L36 ).

The implementation of syslog the C allows us to use a very short list of the names of objects: 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, I did to use one of the LOG_LOCALX objects that already exist for personal use.

Now I can configure syslog as follows:

 # /etc/syslog.conf local5.* /var/log/myapp_events.log 

And in Ruby do the following:

 Syslog.open('myapp', Syslog::LOG_PID, Syslog::LOG_LOCAL5) { |s| s.info 'this is my message' } 

I think syslog wants you to define custom tools .

+13
source

The logger is not useful if you have multiple employees or threads, as the log messages alternate. This is why the default registrar in Rails 3 is BufferedLogger.

This is why you should use a buffering syslogger like rsyslog or it will kill your performance. (I believe syslogd uses fsync (), which is a synchronous call that is waiting to be returned.)

+3
source

Take a look at Lumberjack .

It supports Syslog as a log device, and you can specify an object as an option:

 require 'lumberjack' require 'lumberjack_syslog_device' syslog_device = Lumberjack::SyslogDevice.new(:facility => 'myapp_events') logger = Lumberjack::Logger.new(syslog_device) logger.info "Hello, Syslog!" 

Lumberjack also has many other features that make it worthy of attention.

+3
source

You might want to learn Logger , which is very similar to syslog, but it is a bit more user friendly. It has several levels of criticality, such as Syslog, and offers cropping based on size or age.

0
source

All Articles