Sha256: a5c6dca74c52d05702977123b1beced76c40ca94a573c73cff3e0edfa4b6d27f

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

require 'logger'
require 'socket'
require 'time'

module LogStashLogger
  HOST = ::Socket.gethostname

  class Formatter < ::Logger::Formatter
    include TaggedLogging::Formatter

    def call(severity, time, progname, message)
      event = build_event(message, severity, time)
      "#{event.to_json}\n"
    end

    protected

    def build_event(message, severity, time)
      data = message
      if data.is_a?(String) && data.start_with?('{')
        data = (JSON.parse(message) rescue nil) || message
      end

      event = case data
                when LogStash::Event
                  data.clone
                when Hash
                  event_data = data.merge("@timestamp" => time)
                  LogStash::Event.new(event_data)
                else
                  LogStash::Event.new("message" => msg2str(data), "@timestamp" => time)
              end

      event['severity'] ||= severity
      #event.type = progname

      event['host'] ||= HOST

      LogStashLogger.configuration.customize_event_block.call(event) if LogStashLogger.configuration.customize_event_block.respond_to?(:call)

      current_tags.each do |tag|
        event.tag(tag)
      end

      # In case Time#to_json has been overridden
      if event.timestamp.is_a?(Time)
        event.timestamp = event.timestamp.iso8601(3)
      end

      event
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
logstash-logger-0.10.1 lib/logstash-logger/formatter.rb
logstash-logger-0.10.0 lib/logstash-logger/formatter.rb
logstash-logger-0.9.0 lib/logstash-logger/formatter.rb