Class: EscalatingLogger::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/escalating_logger.rb

Overview

EscalatingLogger::Logger subclasses the standard Logger class. This subclass maintains the same API as Logger itself, so it can be used anywhere you use Logger today

Constant Summary collapse

DECREASE_VERBOSITY =
1
INCREASE_VERBOSITY =
-1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logdev, shift_age = 0, shift_size = 1_048_576, level: DEBUG, progname: nil, formatter: nil, datetime_format: nil, binmode: false, shift_period_suffix: '%Y%m%d', min_verbosity: Logger::ERROR, max_verbosity: Logger::DEBUG, initial_token_count: 100, refill_rate: 1, max_token_count: 200, triggering_log_levels: [Logger::ERROR]) ⇒ Logger

rubocop:disable Metrics/ParameterLists rubocop:disable Metrics/MethodLength



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
53
54
55
56
57
58
59
# File 'lib/escalating_logger.rb', line 20

def initialize(
  logdev,
  shift_age = 0,
  shift_size = 1_048_576,
  level:                 DEBUG,
  progname:              nil,
  formatter:             nil,
  datetime_format:       nil,
  binmode:               false,
  shift_period_suffix: '%Y%m%d',
  min_verbosity:         Logger::ERROR,
  max_verbosity:         Logger::DEBUG,
  initial_token_count:   100,
  refill_rate:           1,
  max_token_count:       200,
  triggering_log_levels: [Logger::ERROR]
)

  @triggering_log_levels = triggering_log_levels
  @max_verbosity = max_verbosity
  @min_verbosity = min_verbosity
  @initial_token_count = initial_token_count
  @refill_rate = refill_rate
  @max_token_count = max_token_count

  @bucket = BozosBuckets::Bucket.new(initial_token_count: initial_token_count,
                                     refill_rate: refill_rate, max_token_count: max_token_count)

  super(
    logdev,
    shift_age,
    shift_size,
    level: level,
    progname: progname,
    formatter: formatter,
    datetime_format: datetime_format,
    binmode: binmode,
    shift_period_suffix: shift_period_suffix
  )
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



15
16
17
# File 'lib/escalating_logger.rb', line 15

def bucket
  @bucket
end

#initial_token_countObject

Returns the value of attribute initial_token_count.



15
16
17
# File 'lib/escalating_logger.rb', line 15

def initial_token_count
  @initial_token_count
end

#max_token_countObject

Returns the value of attribute max_token_count.



15
16
17
# File 'lib/escalating_logger.rb', line 15

def max_token_count
  @max_token_count
end

#max_verbosityObject

Returns the value of attribute max_verbosity.



15
16
17
# File 'lib/escalating_logger.rb', line 15

def max_verbosity
  @max_verbosity
end

#min_verbosityObject

Returns the value of attribute min_verbosity.



15
16
17
# File 'lib/escalating_logger.rb', line 15

def min_verbosity
  @min_verbosity
end

#refill_rateObject

Returns the value of attribute refill_rate.



15
16
17
# File 'lib/escalating_logger.rb', line 15

def refill_rate
  @refill_rate
end

#triggering_log_levelsObject

Returns the value of attribute triggering_log_levels.



15
16
17
# File 'lib/escalating_logger.rb', line 15

def triggering_log_levels
  @triggering_log_levels
end

Instance Method Details

#add(severity, message = nil, progname = nil) ⇒ Object

Overrides add method to catch all forms of invocations. See https://github.com/ruby/ruby/blob/b56c8f814e656e6a680acf2e5c96812e84af238d/lib/logger.rb#L459



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/escalating_logger.rb', line 65

def add(severity, message = nil, progname = nil)
  # If severity is error/warning/whatever, check if the rate limit has
  # been exceeded. If so, increase verbosity. If severity check is not
  # triggered, decrease verbosity if the token bucket is full

  # Check if verbosity should be increased if logger was invoked with
  # the a triggering severity
  if @triggering_log_levels.include?(severity)
    # Increase verbosity if rate limit exceeded/bucket is empty
    increase_verbosity unless @bucket.use_tokens
  # Only attempt to decrease verbosity when the lowest severity is called
  elsif severity == level
    # Decrease verbosity if the bucket is full
    decrease_verbosity if @bucket.current_token_count == @bucket.max_token_count
  end

  # Call the superclass method with the same signature
  super
end