~ubuntu-branches/ubuntu/oneiric/mcollective/oneiric-proposed

« back to all changes in this revision

Viewing changes to lib/mcollective/logger/console_logger.rb

  • Committer: Bazaar Package Importer
  • Author(s): Marc Cluet, Marc Cluet, Chuck Short
  • Date: 2011-05-05 07:37:54 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110505073754-klk1jkz8afi4fomx
Tags: 1.2.0-0ubuntu1
[Marc Cluet]
* Update to 1.2.0
* Build for Oneiric.

[Chuck Short]
* Drop ruby from mcollective, mcollective-middleware, mcollective-client
  since its a dependency of mcollective-common.
* Bump standards to 3.9.2.
* Fix up lintian warnings.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module MCollective
 
2
    module Logger
 
3
        # Impliments a syslog based logger using the standard ruby syslog class
 
4
        class Console_logger<Base
 
5
            def start
 
6
                set_level(:info)
 
7
 
 
8
                config = Config.instance
 
9
                set_level(config.loglevel.to_sym) if config.configured
 
10
            end
 
11
 
 
12
            def set_logging_level(level)
 
13
                # nothing to do here, we ignore high levels when we log
 
14
            end
 
15
 
 
16
            def valid_levels
 
17
                {:info  => :info,
 
18
                 :warn  => :warning,
 
19
                 :debug => :debug,
 
20
                 :fatal => :crit,
 
21
                 :error => :err}
 
22
            end
 
23
 
 
24
            def log(level, from, msg)
 
25
                if @known_levels.index(level) >= @known_levels.index(@active_level)
 
26
                    time = Time.new.strftime("%Y/%m/%d %H:%M:%S")
 
27
                    lvltxt = colorize(level, level)
 
28
                    STDERR.puts("#{lvltxt} #{time}: #{from} #{msg}")
 
29
                end
 
30
            rescue
 
31
                # if this fails we probably cant show the user output at all,
 
32
                # STDERR it as last resort
 
33
                STDERR.puts("#{level}: #{msg}")
 
34
            end
 
35
 
 
36
            # Set some colors for various logging levels, will honor the
 
37
            # color configuration option and return nothing if its configured
 
38
            # not to
 
39
            def color(level)
 
40
                colorize = Config.instance.color
 
41
 
 
42
                colors = {:error => "",
 
43
                          :fatal => "",
 
44
                          :warn => "",
 
45
                          :info => "",
 
46
                          :reset => ""}
 
47
 
 
48
                if colorize
 
49
                    return colors[level] || ""
 
50
                else
 
51
                    return ""
 
52
                end
 
53
            end
 
54
 
 
55
            # Helper to return a string in specific color
 
56
            def colorize(level, msg)
 
57
                "#{self.color(level)}#{msg}#{self.color(:reset)}"
 
58
            end
 
59
        end
 
60
    end
 
61
end