X-Git-Url: https://review.fuel-infra.org/gitweb?a=blobdiff_plain;f=doc%2Fclasses%2FMCollective%2FLog.html;fp=doc%2Fclasses%2FMCollective%2FLog.html;h=4d9c85a7944b100bf14d0eddfcc8c92b3da11dae;hb=d1f1649ba43c5cbc43c4beb2380096ba051d646a;hp=0000000000000000000000000000000000000000;hpb=8a3fe7daeecccf43dd71c59371c5005400d35101;p=packages%2Fprecise%2Fmcollective.git diff --git a/doc/classes/MCollective/Log.html b/doc/classes/MCollective/Log.html new file mode 100644 index 0000000..4d9c85a --- /dev/null +++ b/doc/classes/MCollective/Log.html @@ -0,0 +1,738 @@ + + + + + + Class: MCollective::Log + + + + + + + + + + +
+ + + + + + + + + + + + + + +
ClassMCollective::Log
In: + + lib/mcollective/log.rb + +
+
Parent: + Object +
+
+ + +
+ + + +
+ +
+

+A simple class that allows logging at various levels. +

+ +
+ + +
+ +
+

Methods

+ +
+ check_level   + config_and_check_level   + configure   + cycle_level   + debug   + error   + execution_stack   + fatal   + from   + info   + instance   + log   + logexception   + logger   + logmsg   + message_for   + set_logger   + unconfigure   + valid_level?   + warn   +
+
+ +
+ + + + +
+ + +
+

Constants

+ +
+ + + + + + +
VALID_LEVELS=[:error, :fatal, :debug, :warn, :info]
+
+
+ + + + + + + +
+

Public Class methods

+ +
+ + + + +
+

[Source]

+
+
+    # File lib/mcollective/log.rb, line 55
+55:       def check_level(level)
+56:         raise "Unknown log level" unless valid_level?(level)
+57:       end
+
+
+
+
+ +
+ + + + +
+

[Source]

+
+
+    # File lib/mcollective/log.rb, line 49
+49:       def config_and_check_level(level)
+50:         configure unless @configured
+51:         check_level(level)
+52:         @logger.should_log?(level)
+53:       end
+
+
+
+
+ +
+ + + + +
+

+configures the logger class, if the config +has not yet been loaded we default to the console logging class and do not +set @configured so that future calls to the log method will keep attempting to configure the logger till we eventually get a logging +preference from the config module +

+

[Source]

+
+
+     # File lib/mcollective/log.rb, line 130
+130:       def configure(logger=nil)
+131:         unless logger
+132:           logger_type = "console"
+133: 
+134:           config = Config.instance
+135: 
+136:           if config.configured
+137:             logger_type = config.logger_type
+138:             @configured = true
+139:           end
+140: 
+141:           require "mcollective/logger/%s_logger" % logger_type.downcase
+142: 
+143:           logger_class = MCollective::Logger.const_get("%s_logger" % logger_type.capitalize)
+144: 
+145:           set_logger(logger_class.new)
+146:         else
+147:           set_logger(logger)
+148:           @configured = true
+149:         end
+150: 
+151: 
+152:         @logger.start
+153:       rescue Exception => e
+154:         @configured = false
+155:         STDERR.puts "Could not start logger: #{e.class} #{e}"
+156:       end
+
+
+
+
+ +
+ + + + +
+

+increments the active log level +

+

[Source]

+
+
+    # File lib/mcollective/log.rb, line 45
+45:       def cycle_level
+46:         @logger.cycle_level if @configured
+47:       end
+
+
+
+
+ +
+ + + + +
+

+Logs at debug level +

+

[Source]

+
+
+    # File lib/mcollective/log.rb, line 25
+25:       def debug(msg)
+26:         log(:debug, msg)
+27:       end
+
+
+
+
+ +
+ + + + +
+

+Logs at error level +

+

[Source]

+
+
+    # File lib/mcollective/log.rb, line 35
+35:       def error(msg)
+36:         log(:error, msg)
+37:       end
+
+
+
+
+ +
+ + + + +
+

+this method is here to facilitate testing +

+

[Source]

+
+
+     # File lib/mcollective/log.rb, line 170
+170:       def execution_stack
+171:         caller
+172:       end
+
+
+
+
+ +
+ + + + +
+

+Logs at fatal level +

+

[Source]

+
+
+    # File lib/mcollective/log.rb, line 30
+30:       def fatal(msg)
+31:         log(:fatal, msg)
+32:       end
+
+
+
+
+ +
+ + + + +
+

+figures out the filename that called us +

+

[Source]

+
+
+     # File lib/mcollective/log.rb, line 164
+164:       def from
+165:         path, line, method = execution_stack[3].split(/:(\d+)/)
+166:         "%s:%s%s" % [File.basename(path), line, method]
+167:       end
+
+
+
+
+ +
+ + + + +
+

+Logs at info level +

+

[Source]

+
+
+    # File lib/mcollective/log.rb, line 15
+15:       def info(msg)
+16:         log(:info, msg)
+17:       end
+
+
+
+
+ +
+ + + + +
+

+handle old code that relied on this class being a singleton +

+

[Source]

+
+
+    # File lib/mcollective/log.rb, line 40
+40:       def instance
+41:         self
+42:       end
+
+
+
+
+ +
+ + + + +
+

+logs a message at a certain level +

+

[Source]

+
+
+     # File lib/mcollective/log.rb, line 106
+106:       def log(level, msg, origin=nil)
+107:         return unless config_and_check_level(level)
+108: 
+109:         origin = from unless origin
+110: 
+111:         if @logger
+112:           @logger.log(level, origin, msg)
+113:         else
+114:           t = Time.new.strftime("%H:%M:%S")
+115: 
+116:           STDERR.puts "#{t}: #{level}: #{origin}: #{msg}"
+117:         end
+118:       end
+
+
+
+
+ +
+ + + + +
+

[Source]

+
+
+    # File lib/mcollective/log.rb, line 67
+67:       def logexception(msgid, level, e, backtrace=false, args={})
+68:         return false unless config_and_check_level(level)
+69: 
+70:         path, line, method = e.backtrace[1].split(/:(\d+)/)
+71:         origin = "%s:%s%s" % [File.basename(path), line, method]
+72: 
+73:         if e.is_a?(CodedError)
+74:           msg = "%s: %s" % [e.code, e.to_s]
+75:         else
+76:           error_string = "%s: %s" % [e.class, e.to_s]
+77:           msg = message_for(msgid, args.merge(:error => error_string))
+78:         end
+79: 
+80:         log(level, msg, origin)
+81: 
+82:         if backtrace
+83:           e.backtrace.each do |line|
+84:             log(level, "%s:          %s" % [msgid, line], origin)
+85:           end
+86:         end
+87:       end
+
+
+
+
+ +
+ + + + +
+

+Obtain the class name of the currently configured logger +

+

[Source]

+
+
+    # File lib/mcollective/log.rb, line 10
+10:       def logger
+11:         @logger.class
+12:       end
+
+
+
+
+ +
+ + + + +
+

+Logs a message at a certain level, the message must be a token that will be +looked up from the i18n localization +database +

+

+Messages can interprolate strings from the +args hash, a message with "foo %{bar}" in the localization +database will use args[:bar] for the value there, the interprolation is +handled by the i18n library itself +

+

[Source]

+
+
+     # File lib/mcollective/log.rb, line 97
+ 97:       def logmsg(msgid, default, level, args={})
+ 98:         return false unless config_and_check_level(level)
+ 99: 
+100:         msg = message_for(msgid, {:default => default}.merge(args))
+101: 
+102:         log(level, msg)
+103:       end
+
+
+
+
+ +
+ + + + +
+

[Source]

+
+
+    # File lib/mcollective/log.rb, line 63
+63:       def message_for(msgid, args={})
+64:         "%s: %s" % [msgid, Util.t(msgid, args)]
+65:       end
+
+
+
+
+ +
+ + + + +
+

+sets the logger class to use +

+

[Source]

+
+
+     # File lib/mcollective/log.rb, line 121
+121:       def set_logger(logger)
+122:         @logger = logger
+123:       end
+
+
+
+
+ +
+ + + + +
+

[Source]

+
+
+     # File lib/mcollective/log.rb, line 158
+158:       def unconfigure
+159:         @configured = false
+160:         set_logger(nil)
+161:       end
+
+
+
+
+ +
+ + + + +
+

[Source]

+
+
+    # File lib/mcollective/log.rb, line 59
+59:       def valid_level?(level)
+60:         VALID_LEVELS.include?(level)
+61:       end
+
+
+
+
+ +
+ + + + +
+

+Logs at warn level +

+

[Source]

+
+
+    # File lib/mcollective/log.rb, line 20
+20:       def warn(msg)
+21:         log(:warn, msg)
+22:       end
+
+
+
+
+ + +
+ + +
+ + +
+

[Validate]

+
+ + + \ No newline at end of file