Module: Lumber::LevelUtil

Extended by:
LevelUtil, MonitorMixin
Included in:
LevelUtil
Defined in:
lib/lumber/level_util.rb

Defined Under Namespace

Classes: MemoryCacheProvider, MonitorThread

Constant Summary collapse

LOG_LEVELS_KEY =
"lumber:log_levels"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_providerMemoryCacheProvider

Returns Where to persist the log level mappings (Rails.cache interface), defaults to Memory.

Returns:

  • (MemoryCacheProvider)

    Where to persist the log level mappings (Rails.cache interface), defaults to Memory



28
29
30
# File 'lib/lumber/level_util.rb', line 28

def cache_provider
  @cache_provider
end

#ttlInteger

Returns The time in seconds till the overrides expires, defaults to 3600.

Returns:

  • (Integer)

    The time in seconds till the overrides expires, defaults to 3600



32
33
34
# File 'lib/lumber/level_util.rb', line 32

def ttl
  @ttl
end

Instance Method Details

#activate_levelsObject

Activates previously set logger level overrides. Should be called at code entry points, e.g. an ApplicationController before_filter, or Resque::Worker callback



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lumber/level_util.rb', line 53

def activate_levels
  levels = get_levels
  if levels.size == 0
    restore_levels
  else
    
    levels = expand_heirarchy(levels)
    backup_levels(levels.keys)
    
    levels.each do |name, level|
      level_val = Log4r::LNAMES.index(level)
      outputter = Log4r::Outputter[name]
      if outputter
        outputter.level = level_val if level_val && outputter.level != level_val
      else
        logger = Lumber.find_or_create_logger(name)
        logger.level = level_val if level_val && logger.level != level_val
      end
    end
  end
end

#get_levelsObject



45
46
47
# File 'lib/lumber/level_util.rb', line 45

def get_levels()
  @cache_provider.read(LOG_LEVELS_KEY) || {}
end

#set_levels(levels) ⇒ Object

Sets the logger level overrides into the cache_provider so that we can temporarily use a lower level for specific loggers to aid in debugging

Parameters:

  • Logger (Hash)

    fullname mapping to level name, e.g. => ‘DEBUG’



40
41
42
43
# File 'lib/lumber/level_util.rb', line 40

def set_levels(levels)
  levels = expand_heirarchy(levels)
  @cache_provider.write(LOG_LEVELS_KEY, levels, :expires_in => @ttl)
end

#start_monitor(interval = 10) ⇒ Thread

Convenience method for starting a thread to watch for changes in log levels and apply them. You don’t need to use this if you are manually calling activate levels at all your entry points.

Parameters:

  • How (Integer)

    long to sleep between checks

Returns:

  • (Thread)

    The monitor thread



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lumber/level_util.rb', line 82

def start_monitor(interval=10)
  t = MonitorThread.new do
    loop do
      break if Thread.current.should_exit

      begin
        activate_levels
      rescue => e
        $stderr.puts "Failure activating log levels: #{e}"
      end
      sleep interval
    end
  end

  at_exit { t.should_exit = true }

  t
end