Class: Crossroads::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/crossroads/log.rb

Class Method Summary collapse

Class Method Details

.configure(logfile, keep = 10, maxsize = 1024, level = :info) ⇒ Object



15
16
17
18
19
20
# File 'lib/crossroads/log.rb', line 15

def configure(logfile, keep=10, maxsize=1024, level=:info)
    @logger = Logger.new(logfile, keep, maxsize)
    @logger.formatter = Logger::Formatter.new
    @logger.level = valid_levels[level.to_sym]
    @configured = true
end

.fromObject

figures out the filename that called us



23
24
25
# File 'lib/crossroads/log.rb', line 23

def from
    from = File.basename(caller[4])
end

.log(msg, severity = :debug) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/crossroads/log.rb', line 6

def log(msg, severity=:debug)
    configure unless @configured

    @logger.add(valid_levels[severity.to_sym]) { "#{from} #{msg}" }
rescue Exception => e
    STDERR.puts("Failed to log: #{e.class}: #{e}: original log message: #{severity}: #{msg}")
    STDERR.puts(e.backtrace.join("\n\t"))
end

.method_missing(level, *args, &block) ⇒ Object



35
36
37
38
39
# File 'lib/crossroads/log.rb', line 35

def method_missing(level, *args, &block)
    super unless [:info, :warn, :debug, :fatal, :error].include?(level)

    log(args[0], level)
end

.valid_levelsObject



27
28
29
30
31
32
33
# File 'lib/crossroads/log.rb', line 27

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