graylog-labs / gelf-rb

Ruby GELF library (Graylog Extended Log Format)

Home Page:https://rubygems.org/gems/gelf

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting ArgumentError

gavinheale opened this issue · comments

Hi,

I am new to Graylog2 and I have just tried to get my first rails application integrated. All I have done is add the line:

config.logger = GELF::Logger.new("192.168.20.51", 12201, "WAN", { :facility => "testapp" })

to my development.rb.

This seems to work ok, in that I am getting messages through to Graylog2 but I also get a lot of:

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

I think these are just blank lines but cannot be sure. I see that there was a pull request to fix blank lines but it was never merged. Could that be the problem?

Regards,
Gavin

Hi. Did you get anywhere with this? I am having the same issue but only when i use active support logger to extend the Rails logger and allow me to log to more than one place. I am using Rails 3.2.12 and have ported the active support logger code to my config/initilizers as per this post however i am getting the same ArgumentError: short_message is missing. The only way I have found to fix this is to modify the Gelf-rb gem and in the add method have altered the code (5th line) to use args[1] for message rather than args[0] which is always nil

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

I ran into to the same issue and I think I found out why it happens:

1.) GELF::Notifier checks if short_message (which is the log message) exists in the log hash. It does so by checking if @hash['short_message'].to_s.empty? is true. In Ruby a blank string "" returns true on empty?.

2.) Rails::Rack::Logger logs blank strings in the development environment for better readability. To save you the click:

        # Put some space between requests in development logs.
        if development?
          logger.debug ''
          logger.debug ''
        end

So, to fix this either don't use GELF in development, fix the "issue" in Rails or allow blank strings as short_message in GELF.

My recommendation would be to use a gem like lograge to convert the Rails logs to a format better suited to structured logging. From the point of the logging code, receiving empty log messages is a bit silly and not that surprising that it complains.