Class: LogToolbox::LogrageConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/log_toolbox/lograge_config.rb

Constant Summary collapse

LOGSTASH_URL =
ENV['LOGSTASH_URL']
LOGSTASH_PORT =
ENV['LOGSTASH_PORT']
LOGSTASH_PROTOCOL =
ENV.fetch('LOGSTASH_PROTOCOL', 'udp')
LOGSTASH_BUFFER_MAX_ITEMS =
ENV.fetch('LOGSTASH_BUFFER_MAX_ITEMS', 50).to_i
LOGSTASH_BUFFER_MAX_INTERVAL =
ENV.fetch('LOGSTASH_BUFFER_MAX_INTERVAL', 5).to_i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ LogrageConfig

Returns a new instance of LogrageConfig.



28
29
30
# File 'lib/log_toolbox/lograge_config.rb', line 28

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/log_toolbox/lograge_config.rb', line 8

def config
  @config
end

Class Method Details

.configureObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/log_toolbox/lograge_config.rb', line 16

def self.configure
  Rails.application.configure do
    log_config = LogToolbox::LogrageConfig.new(config)
    log_config.configure_log_format
    if %w[development test].include?(ENV['RAILS_ENV'])
      log_config.configure_log_output_stdout
    else
      log_config.configure_log_output_logstash
    end
  end
end

Instance Method Details

#configure_log_formatObject

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/log_toolbox/lograge_config.rb', line 49

def configure_log_format
  config.lograge.enabled = true
  config.lograge.base_controller_class = 'ActionController::API'
  config.lograge.formatter = Lograge::Formatters::Json.new

  config.lograge.custom_options = lambda do |event|
    {
      event_at: event.payload[:event_at],
      parameters: event.payload[:params].to_json,
      level: 'INFO',
      service_name: event.payload[:service_name] || LogUtil.system_service_name,
      version: event.payload[:version],
      origin_ipv4: event.payload[:origin_ipv4],
      destination_ipv4: event.payload[:destination_ipv4],
      username: wso2_user_name(event.payload[:headers][:HTTP_USER_IDENTIFIER])
    }
  end
end

#configure_log_output_logstashObject



32
33
34
35
36
37
38
# File 'lib/log_toolbox/lograge_config.rb', line 32

def configure_log_output_logstash
  config.logstash.type = LOGSTASH_PROTOCOL
  config.logstash.host = LOGSTASH_URL
  config.logstash.port = LOGSTASH_PORT
  config.logstash.buffer_max_items = LOGSTASH_BUFFER_MAX_ITEMS
  config.logstash.buffer_max_interval = LOGSTASH_BUFFER_MAX_INTERVAL
end

#configure_log_output_stdoutObject



40
41
42
43
44
45
46
# File 'lib/log_toolbox/lograge_config.rb', line 40

def configure_log_output_stdout
  return unless ENV['RAILS_LOG_TO_STDOUT'].present?

  logger           = ActiveSupport::Logger.new($stdout)
  logger.formatter = config.log_formatter
  config.logger    = ActiveSupport::TaggedLogging.new(logger)
end

#wso2_user_name(http_user_identifier) ⇒ Object

rubocop:enable Metrics/AbcSize,Metrics/MethodLength



69
70
71
72
73
# File 'lib/log_toolbox/lograge_config.rb', line 69

def wso2_user_name(http_user_identifier)
  return nil unless http_user_identifier.is_a? String

  http_user_identifier ? JSON.parse(Base64.decode64(http_user_identifier))['user_name'] : nil
end