Module: Rig::Log

Defined in:
lib/rig/log.rb

Constant Summary collapse

LEVELS =
{
    :debug => ::Logger::DEBUG,
    :info  => ::Logger::INFO,
    :warn  => ::Logger::WARN,
    :error => ::Logger::ERROR,
    :fatal => ::Logger::FATAL
}

Class Method Summary collapse

Class Method Details

.add(logger, formatter = nil) ⇒ Object



54
55
56
57
# File 'lib/rig/log.rb', line 54

def add(logger, formatter=nil)
  logger.formatter = formatter || proc { |severity, datetime, progname, msg| "#{$$}:#{datetime.strftime("%s")} #{severity[0]} #{msg}\n" }
  @loggers << logger
end

.level=(level) ⇒ Object



59
60
61
62
# File 'lib/rig/log.rb', line 59

def level=(level)
  int = LEVELS.key?(level) ? LEVELS[level] : level
  @loggers.each { |l| l.level = int }
end

.load(logging) ⇒ Object



15
16
17
18
19
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
# File 'lib/rig/log.rb', line 15

def load(logging)
  logging ||= {}
  begin
    console = Logger.new(STDOUT)
    console_level = nil
    console_config = logging.delete(:console)
    if console_config && console_config[:level]
      console_level = LEVELS[console_config[:level].to_sym]
    end
    console.level = console_level || Logger::INFO
    add(console, proc {|s, _, _, m| "#{"debug: " if s=='DEBUG'}#{m}\n"})

    logging.each do |name, data|
      level = data[:level]
      opts  = [*data[:opts]].flatten
      dest  = opts.shift

      if %w{STDOUT STDERR}.include?(dest)
        # convert string names to constants
        dest = Kernel.const_get(dest)
      elsif dest !~ /^\//
        # relative paths are relative to configdir
        dest = "#{Rig::Config.dir}/#{dest}"
      end

      opts = opts.unshift(dest)

      l       = Logger.new(*opts)
      l.level = Rig::Log::LEVELS[level.to_sym]

      Rig::Log.add(l)
    end

    Rig::Log.debug "logging initialized"
  rescue => e
    puts "error configuring logging: #{e.message} at #{e.backtrace.first}"
  end
end

.trace(backtrace) ⇒ Object



75
76
77
78
79
80
# File 'lib/rig/log.rb', line 75

def trace(backtrace)
  debug   "trace: #{backtrace.shift}"
  backtrace.each do |l|
    debug "       #{l}"
  end
end