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

Ruby logger device

joeyfironsrc opened this issue · comments

When using this gem I was surprised for example that formatter does nothing.
Wouldn't it be better if I could do something like this:
gelf_logger = GELF::Logger.new(graylog_host, graylog_port, 'LAN', defaults)
logger = ::Logger.new(gelf_logger)

Was there any reason not to do it this way?

+1 on that. I'm going through the same issue. That's bad because you can't compose gelf logger other formatters such as ActiveSupport::TaggedLogging::Formatter.

I'll try to monkey patch here and see how it goes.

I was able to integrate with the following patch:

  class GELF::Logger
    alias_method :original_add, :add
    def add(level, message = nil, progname = nil)
      if message.nil?
        if block_given?
          message = yield
        else
          message = progname
          progname = default_options['facility']
        end
      end

      original_add(level, formatter.call(level, nil, progname, message), progname)
    end
  end

That's obviously ugly, but it works. I'll open a PR with an slightly better solution.