Sha256: a37c4639da9add91125f3d2d9400db0e276c2a8013bc8fcf68b56fd6acda4257

Contents?: true

Size: 1015 Bytes

Versions: 4

Compression:

Stored size: 1015 Bytes

Contents

require "json"
require "socket"

module LogjamAgent
  class Request
    attr_reader :fields

    @@hostname = Socket.gethostname.split('.').first

    def initialize(app, env, logger, initial_fields)
      @logger = logger
      @forwarder = Forwarders.get(app, env)
      @lines = []
      @fields = initial_fields.merge(:host => @@hostname, :process_id => Process.pid, :lines => @lines)
    end

    def add_line(severity, timestamp, message)
      @lines << [severity, format_time(timestamp), message.strip]
    end

    def forward
      engine = @fields.delete(:engine)
      @forwarder.send(@fields.to_json, engine)
    rescue Exception => e
      handle_forwarding_error(e)
    end

    private

    def format_time(t)
      # iso time with microseconds
      t.strftime("%Y-%m-%dT%H:%M:%S.#{t.usec}")
    end

    def handle_forwarding_error(exception)
      @logger.error exception.to_s
      LogjamAgent.error_handler.call(exception)
    rescue Exception
      # swallow all exceptions
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
logjam_agent-0.1.3 lib/logjam_agent/request.rb
logjam_agent-0.1.2 lib/logjam_agent/request.rb
logjam_agent-0.1.1 lib/logjam_agent/request.rb
logjam_agent-0.1.0 lib/logjam_agent/request.rb