Class: TaggedLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/sensible_logging/middlewares/tagged_logger.rb

Overview

Allow custom tags to be captured

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ TaggedLogger

rubocop:disable Metrics/MethodLength



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sensible_logging/middlewares/tagged_logger.rb', line 9

def initialize( # rubocop:disable Metrics/MethodLength
  app,
  options = {}
)
  @app = app

  options = {
    logger: Logger.new($stdout),
    tags: [],
    use_default_tags: true,
    tld_length: 1,
    include_log_severity: true
  }.merge(options)

  options[:logger] = setup_severity_tag(options[:logger]) if options[:include_log_severity]

  @logger = ActiveSupport::TaggedLogging.new(options[:logger])
  @tags = []
  @tags += default_tags(tld_length: options[:tld_length]) if options[:use_default_tags]
  @tags += options[:tags]
end

Instance Method Details

#call(env) ⇒ Object



31
32
33
34
35
36
# File 'lib/sensible_logging/middlewares/tagged_logger.rb', line 31

def call(env)
  @logger.tagged(*generate_tags(env)) do |logger|
    env['logger'] = logger
    @app.call(env)
  end
end