Class: RailsLogstasher::Rack::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-logstasher/rack/logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, tags = nil) ⇒ Logger

Returns a new instance of Logger.



7
8
9
# File 'lib/rails-logstasher/rack/logger.rb', line 7

def initialize(app, tags = nil)
  @app, @tags = app, tags.presence
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rails-logstasher/rack/logger.rb', line 11

def call(env)

  t1 = Time.now
  request = ActionDispatch::Request.new(env)

  event = RailsLogstasher::Event.new(Rails.logger, true)
  event.message = "#{request.request_method} #{request.filtered_path} for #{request.ip}"
  event.fields['client_ip'] = request.ip
  event.fields['method'] = request.request_method
  event.fields['path'] = request.filtered_path
  #TODO Should really move this into the base logger
  event.source = "http://#{Socket.gethostname}#{request.filtered_path}"

  event.add_tags_to_logger(request, @tags) if @tags

  RailsLogstasher.log_entries[Thread.current] = event

  status, headers, response = @app.call(env)
  [status, headers, response]

ensure
  if event
    event.fields['total_duration'] = Time.now - t1
    event.fields['status'] = status

    ['rendering','sql'].each do |type|
      if event.fields[type] && !event.fields[type].empty?
        duration = event.fields[type].inject(0) {|result, local_event| result += local_event[:duration].to_f }
        event.fields["#{type}_duration"] = duration
      end
    end

    event.write(true)
  end

  RailsLogstasher.log_entries[Thread.current] = nil
end