Class: Babysitter::LoggerWithStats

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/babysitter/logger_with_stats.rb

Constant Summary collapse

STATS_SUFFIX_BY_METHOD =
{ warn: :warnings, error: :errors, fatal: :fatals }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(stat_name_prefix) ⇒ LoggerWithStats

Returns a new instance of LoggerWithStats.



9
10
11
# File 'lib/babysitter/logger_with_stats.rb', line 9

def initialize(stat_name_prefix) 
  @stat_name_prefix = stat_name_prefix 
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *opts) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/babysitter/logger_with_stats.rb', line 13

def method_missing(meth, *opts)
  unless %w{ info debug error fatal}.include?(meth.to_s)
    super
    return
  end
  stats_suffix_from_method(meth).tap{ |suffix| increment(suffix) if suffix }
  logger.send(meth, *opts)
end

Instance Attribute Details

#stat_name_prefixObject

Returns the value of attribute stat_name_prefix.



5
6
7
# File 'lib/babysitter/logger_with_stats.rb', line 5

def stat_name_prefix
  @stat_name_prefix
end

Instance Method Details

#warn(*opts) ⇒ Object



22
23
24
25
# File 'lib/babysitter/logger_with_stats.rb', line 22

def warn(*opts)
  increment(stats_suffix_from_method(:warn))
  logger.warn(*opts)
end