Class: Log4r::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/lumber/log4r.rb

Instance Method Summary collapse

Instance Method Details

#log_exception(exception, details = {}) ⇒ Object

Convenience method for logging exceptions


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lumber/log4r.rb', line 28

def log_exception(exception, details = {})
  details = details.stringify_keys
  max = details.keys.max { |a,b| a.length <=> b.length }
  env = details.keys.sort.inject [] do |env, key|
    env << '* ' + ("%-*s: %s" % [max.length, key, details[key].to_s.strip])
  end

  details_str = env.join("\n")
  trace = exception.backtrace.join("\n")
  error("Exception '#{exception.class_name}', '#{exception.message}', details:\n#{details_str}\nBacktrace:\n#{trace}")
end

#silence(temporary_level = nil) ⇒ Object

Silences the logger for the duration of the block.


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lumber/log4r.rb', line 13

def silence(temporary_level = nil)
  temporary_level = Log4r::ERROR unless temporary_level
  if silencer
    begin
      old_logger_level, self.level = level, temporary_level
      yield self
    ensure
      self.level = old_logger_level
    end
  else
    yield self
  end
end