Ruby logging to several backends using Active Support logger

886 Views Asked by At

I am using Rails 3.2.12/Ruby 1.9.3 and am trying to set up multiple loggers so I can log both to a file and to a graylog server which we have set up. I have got close using this soltuion but with a Gelf logger - http://railsware.com/blog/2014/08/07/rails-logging-into-several-backends/

So I have back ported the ActiveSupport::Logger to my config/initializers and set up the gelf logger as below

(development.rb)

gelf_logger = GELF::Logger.new("greylogserver", 12201, "WAN", { :host => 'host', :facility => "railslog"})
Rails.logger.extend(ActiveSupport::Logger.broadcast(gelf_logger)) 

however I am finding that I only get errors logged to the graylog server

ArgumentError: short_message is missing. Options version, short_message and host must be set.

and when I debug the code I can see the args being passed into the Gelf Logger add method (below) always has the 1st element as the level, the 2nd is nil and the 3rd contains the message. This is confusing as the 2nd arg should be the message and the 3rd the progname. The only solution I have come up with is to alter the Gelf-rb gem (as below) by changing the 6th line to use args[1] for message then it works, however this is not ideal and there must be a way to fix this in my code.

  def add(level, *args)
    raise ArgumentError.new('Wrong arguments.') unless (0..2).include?(args.count)

    # Ruby Logger's author is a maniac.
    message, progname = if args.count == 2
                          [args[1], args[1]]
                        elsif args.count == 0
                          [yield, default_options['facility']]
                        elsif block_given?
                          [yield, args[0]]
                        else
                          [args[0], default_options['facility']]
                        end
....

Just to note when i directly set my Rails logger to use the Gelf logger in development.rb then it works fine

Rails.logger = GELF::Logger.new("greylogserver", 12201, "WAN", { :host => 'host', :facility => "railslog"})

So it has to be something to do with my implementation of ActiveSupport::Logger which is from here - https://github.com/rails/rails/blob/6329d9fa8b2f86a178151be264cccdb805bfaaac/activesupport/lib/active_support/logger.rb

Any help would be much appreciated

1

There are 1 best solutions below

0
On BEST ANSWER

As @Leons mentions the same issue was reported to the project as issue #26. The poster wrote a patch with testcases and logged in issue #27 a pull request with a fix to make the interface of the add method identical with the usual definitions.

This was merged in on Dec 22nd, 2014. Since then no new release was made.

I think it is best to compile directly from the github repo with something like:

$ echo "gem 'gelf', :git => 'https://github.com/Graylog2/gelf-rb.git'" >>Gemfile
$ bundle install

or similar.

good luck.