Module: BasicLogging
- Included in:
- Body, Configuration, Headers, Newsgroups, OverrideDlg, PostProcessor, String
- Defined in:
- lib/basic_logging.rb,
lib/bak_basic_logging.rb
Overview
Simplified logging. See example code at the bottom of this file. Execute this file to see the output.
Constant Summary collapse
- DEBUG =
0- INFO =
1- WARN =
2- ERROR =
3- FATAL =
4- UNKNOWN =
nil- Levels =
this is mainly for the translation of method calls into log levels
{:debug => DEBUG, :info => INFO, :warn => WARN, :error => ERROR, :fatal => FATAL, :unknown => UNKNOWN}
- @@log_level =
UNKNOWN- @@target =
STDOUT- @@muted =
[]
Class Method Summary collapse
-
.is_muted?(obj) ⇒ Boolean
return true if obj is muted and will not log.
-
.mute(obj) ⇒ Object
do not log, if caller is obj (class or instance).
Instance Method Summary collapse
-
#clear_log ⇒ Object
Clear the log (-file).
- #level ⇒ Object
-
#log(message) ⇒ Object
(also: #debug, #info, #warn, #error, #fatal)
Output log messages, depending on the log level set for the calling class and the name of the alias method which is actually called.
-
#log_name ⇒ Object
find the name of the calling object’s class or the file name in case of the top level object…
-
#set_level(lv) ⇒ Object
set the log level.
-
#set_target(tg) ⇒ Object
set the log target.
- #target ⇒ Object
Class Method Details
.is_muted?(obj) ⇒ Boolean
return true if obj is muted and will not log.
45 46 47 48 |
# File 'lib/bak_basic_logging.rb', line 45 def self.is_muted?(obj) name = obj.class == Class ? obj.name.dup : obj.class.name @@muted.include?(name) end |
.mute(obj) ⇒ Object
do not log, if caller is obj (class or instance)
39 40 41 42 |
# File 'lib/basic_logging.rb', line 39 def self.mute(obj) name = obj.class == Class ? obj.name.dup : obj.class.name @@muted << name end |
Instance Method Details
#clear_log ⇒ Object
Clear the log (-file)
107 108 109 110 111 |
# File 'lib/basic_logging.rb', line 107 def clear_log if @@target && @@target.respond_to?(:truncate) lock_target{ @@target.truncate(0) } end end |
#level ⇒ Object
102 103 104 |
# File 'lib/basic_logging.rb', line 102 def level @@level.to_s if @@level end |
#log(message) ⇒ Object Also known as: debug, info, warn, error, fatal
Output log messages, depending on the log level set for the calling class and the name of the alias method which is actually called.
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/basic_logging.rb', line 84 def log() if !BasicLogging.is_muted?(self) # how has this method been called? mlevel = __callee__ if Levels.has_key?(mlevel) && Levels[mlevel] <= FATAL # output only for levels equal or above the value that corresponds to # the calling alias. format_log( , mlevel) if @@log_level && Levels[mlevel] >= @@log_level else STDERR.puts calling_method << ": ERROR : invalid log level \"" << mlevel.to_s << "\"" end end end |
#log_name ⇒ Object
find the name of the calling object’s class or the file name in case of the top level object… or any dumb instance of Object
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/basic_logging.rb', line 115 def log_name() if !@log_name clname = self.class.name # limit analysis of the call stack to 'Object'. if clname == 'Object' @log_name = File::basename(caller.last.split(':')[0]) else @log_name = self.class == Class ? self.name.dup << ' [class]' : clname end end @log_name end |
#set_level(lv) ⇒ Object
set the log level
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/basic_logging.rb', line 50 def set_level(lv) if lv.respond_to?(:to_str) && Levels.keys.include?(lv.strip.to_sym) lv = Levels[lv.to_sym] elsif lv.respond_to?(:to_sym) && Levels.keys.include?(lv) lv = Levels[lv] end if(!lv || (lv.respond_to?(:to_int) && lv >= DEBUG && lv <= FATAL) ) @@log_level = lv else msg = calling_method << ": ERROR : invalid log level \"" << lv.to_s << "\"" msg << "\n" << "Keepinng old log level " << Levels.keys.detect {| k| Levels[k] == @@log_level}.to_s STDERR.puts msg puts msg end end |
#set_target(tg) ⇒ Object
set the log target
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/basic_logging.rb', line 68 def set_target(tg) if tg.respond_to?(:to_io) @@target = tg elsif(!File::exist?(tg) || ( File.file?(tg) && File.writable?(tg) ) ) @@target = File.open(tg, 'w+') elsif !tg || tg.respond_to?(:to_str) && tg.strip.empty? @@target = nil else STDERR.puts calling_method << ': ERROR : target ' << tg.to_str << ' cannot be set' STDERR.puts "Keeping old target " << @@target.inspect return end end |
#target ⇒ Object
98 99 100 |
# File 'lib/basic_logging.rb', line 98 def target @@target.path if @@target end |