Project

General

Profile

Download (1.86 KB) Statistics
| Branch: | Tag: | Revision:

root / lib / cyborghood / base / logger.rb @ 0888f3f5

1
#--
2
# CyborgHood, a distributed system management software.
3
# Copyright (c) 2009-2010 Marc Dequènes (Duck) <Duck@DuckCorp.org>
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
#++
18

    
19
require 'singleton'
20
require 'log4r'
21

    
22
module CyborgHood
23
  class Logger < Log4r::Logger
24
    include Singleton
25

    
26
    LOG_FORMAT = "[%5l - %d] #PREFIX#%m"
27

    
28
    def output_level(level)
29
      l = case level
30
      when :verbose
31
        Log4r::DEBUG
32
      when :quiet
33
        Log4r::FATAL
34
      else # normal
35
        Log4r::WARN
36
      end
37
      @main_outputter.level = l
38
    end
39

    
40
    def log_to_file(filename)
41
      file_outputter = Log4r::FileOutputter.new(filename, :filename => filename, :trunc => false)
42
      file_outputter.formatter = @default_formatter
43
      self.outputters << file_outputter
44
    end
45

    
46
    def set_prefix(prefix = "")
47
      prefix ||= ""
48
      log_format = LOG_FORMAT.gsub("#PREFIX#", prefix)
49
      @default_formatter = Log4r::PatternFormatter.new(:pattern => log_format)
50
      self.outputters.each{|outputter| outputter.formatter = @default_formatter }
51
    end
52

    
53
    private
54

    
55
    def initialize
56
      super(PRODUCT)
57

    
58
      self.level = Log4r::DEBUG
59
      @main_outputter = Log4r::Outputter.stdout
60
      @main_outputter.level = Log4r::WARN
61
      self.outputters = [@main_outputter]
62

    
63
      set_prefix()
64
    end
65
  end
66
end
(6-6/6)