File: simple_logger.rb

package info (click to toggle)
ruby-god 0.12.1-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 752 kB
  • sloc: ruby: 5,913; ansic: 217; makefile: 3
file content (59 lines) | stat: -rw-r--r-- 1,174 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
53
54
55
56
57
58
59
module God

  class SimpleLogger
    DEBUG = 2
    INFO = 4
    WARN = 8
    ERROR = 16
    FATAL = 32

    SEV_LABEL = {DEBUG => 'DEBUG',
                 INFO => 'INFO',
                 WARN => 'WARN',
                 ERROR => 'ERROR',
                 FATAL => 'FATAL'}

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

    attr_accessor :datetime_format, :level

    def initialize(io)
      @io = io
      @level = INFO
      @datetime_format = "%Y-%m-%d %H:%M:%S"
    end

    def output(level, msg)
      return if level < self.level

      time = Time.now.strftime(self.datetime_format)
      label = SEV_LABEL[level]
      @io.print("#{label[0..0]} [#{time}] #{label.rjust(5)}: #{msg}\n")
    end

    def fatal(msg)
      self.output(FATAL, msg)
    end

    def error(msg)
      self.output(ERROR, msg)
    end

    def warn(msg)
      self.output(WARN, msg)
    end

    def info(msg)
      self.output(INFO, msg)
    end

    def debug(msg)
      self.output(DEBUG, msg)
    end
  end

end