summaryrefslogtreecommitdiff
path: root/config/initializers/lograge.rb
blob: 49fdd23064ce98327c075dfb529cf76ae51f2df9 (plain)
1
2
3
4
5
6
7
8
9
10
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
48
49
50
51
52
# Monkey patch lograge until https://github.com/roidrage/lograge/pull/241 is released
module Lograge
  class RequestLogSubscriber < ActiveSupport::LogSubscriber
    def strip_query_string(path)
      index = path.index('?')
      index ? path[0, index] : path
    end

    def extract_location
      location = Thread.current[:lograge_location]
      return {} unless location

      Thread.current[:lograge_location] = nil
      { location: strip_query_string(location) }
    end
  end
end

# Only use Lograge for Rails
unless Sidekiq.server?
  filename = File.join(Rails.root, 'log', "#{Rails.env}_json.log")

  Rails.application.configure do
    config.lograge.enabled = true
    # Store the lograge JSON files in a separate file
    config.lograge.keep_original_rails_log = true
    # Don't use the Logstash formatter since this requires logstash-event, an
    # unmaintained gem that monkey patches `Time`
    config.lograge.formatter = Lograge::Formatters::Json.new
    config.lograge.logger = ActiveSupport::Logger.new(filename)
    # Add request parameters to log output
    config.lograge.custom_options = lambda do |event|
      params = event.payload[:params]
        .except(*%w(controller action format))
        .each_pair
        .map { |k, v| { key: k, value: v } }

      payload = {
        time: event.time.utc.iso8601(3),
        params: params,
        remote_ip: event.payload[:remote_ip],
        user_id: event.payload[:user_id],
        username: event.payload[:username]
      }

      gitaly_calls = Gitlab::GitalyClient.get_request_count
      payload[:gitaly_calls] = gitaly_calls if gitaly_calls > 0

      payload
    end
  end
end