Class: Ramolog::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/ramolog/main.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, uri, collection) ⇒ Logger

Returns a new instance of Logger.



6
7
8
9
10
# File 'lib/ramolog/main.rb', line 6

def initialize(app, uri, collection)
  @app = app
  @db = Mongo::Client.new(uri)
  @collection = @db[collection]
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/ramolog/main.rb', line 12

def call(env)
  begin_at = Time.now
  status, header, body = @app.call(env)
  end_at = Time.now

  # Retrieve the original remote host if running behind a front end.
  remote_addr = env['REMOTE_ADDR'] or '-'
  if forwarded = env['HTTP_X_FORWARDED_FOR'] then
    remote_addr = forwarded.split(',')[-1]
  end

  @collection.insert_one({
    format: 1,
    date: begin_at.iso8601,
    referrer: (env['HTTP_REFFERER'] or '-'),
    request: {
      method: env['REQUEST_METHOD'],
      host: (env['HTTP_HOST'] or '-'),
      url: env['REQUEST_URI'],
      protocol: env['HTTP_VERSION'],
      acceptLanguage: (env['HTTP_ACCEPT_LANGUAGE'] or '-')
    },
    response: {
      status: status,
      contentLength: (header['Content-Length'] or '-1').to_i,
      responseTime: (end_at - begin_at) * 1000  # msec
    },
    remote: {
      addr: remote_addr,
      user: (env['REMOTE_USER'] or '-'),
      userAgent: (env['HTTP_USER_AGENT'] or '-')
    }
  })
  [status, header, body]
end