Class: Common::Log
Overview
Logger singleton wrapper for the project
Constant Summary collapse
- @@default_cfg =
{ :level => Logger::DEBUG, :device => STDOUT, :formatter => "%Y-%m-%dT%H:%M:%S " }
- @@cfg =
{}
- @@configured =
false
Class Method Summary collapse
Instance Method Summary collapse
- #add(severity, message, progname, opts = {}, &block) ⇒ Object
- #configure(log_cfg = {}) ⇒ Object
- #debug(message = nil, *opts, &block) ⇒ Object
- #debug? ⇒ Boolean
- #error(message = nil, *opts, &block) ⇒ Object
- #fatal(message = nil, *opts, &block) ⇒ Object
- #info(message = nil, *opts, &block) ⇒ Object
- #info? ⇒ Boolean
-
#initialize ⇒ Log
constructor
A new instance of Log.
- #level=(val) ⇒ Object
- #logger ⇒ Object
- #reinitialize(cfg) ⇒ Object
- #unknown(message = nil, *opts, &block) ⇒ Object
- #warn(message = nil, *opts, &block) ⇒ Object
Constructor Details
#initialize ⇒ Log
Returns a new instance of Log.
34 35 36 |
# File 'lib/common/log.rb', line 34 def initialize reinitialize @@default_cfg.merge(@@cfg) end |
Class Method Details
.configure(log_cfg = {}) ⇒ Object
23 24 25 26 |
# File 'lib/common/log.rb', line 23 def configure(log_cfg = {}) @@cfg = @@default_cfg.merge(log_cfg) @@configured = true end |
.new ⇒ Object
28 29 30 |
# File 'lib/common/log.rb', line 28 def new super end |
Instance Method Details
#add(severity, message, progname, opts = {}, &block) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/common/log.rb', line 66 def add(severity, , progname, opts = {}, &block) ||= '' raise "You can only pass String or Exception classes as a logger message, you have pass #{.class}" unless (.is_a?(String)) || (.is_a?(Exception)) progname||="" progname[0,0] = ("[%s] " % caller[1]) if opts.include? :line use_trace = opts.include? :trace if (.is_a?(Exception)) = "Exception: #{.class}: #{.}" += ", trace: #{.backtrace}" if use_trace end if !.nil? and block_given? <<" "<< (yield).to_s end @logger.add(severity, , progname) end |
#configure(log_cfg = {}) ⇒ Object
92 93 94 95 96 |
# File 'lib/common/log.rb', line 92 def configure(log_cfg = {}) @@cfg = @@default_cfg.merge(log_cfg) @@configured = true reinitialize @@cfg end |
#debug(message = nil, *opts, &block) ⇒ Object
42 43 44 |
# File 'lib/common/log.rb', line 42 def debug(=nil, *opts, &block) add(Logger::DEBUG, , nil, opts, &block) end |
#debug? ⇒ Boolean
82 |
# File 'lib/common/log.rb', line 82 def debug?; @logger.debug? end |
#error(message = nil, *opts, &block) ⇒ Object
54 55 56 |
# File 'lib/common/log.rb', line 54 def error(=nil, *opts, &block) add(Logger::ERROR, , nil, opts, &block) end |
#fatal(message = nil, *opts, &block) ⇒ Object
58 59 60 |
# File 'lib/common/log.rb', line 58 def fatal(=nil, *opts, &block) add(Logger::FATAL, , nil, opts, &block) end |
#info(message = nil, *opts, &block) ⇒ Object
46 47 48 |
# File 'lib/common/log.rb', line 46 def info(=nil, *opts, &block) add(Logger::INFO, , nil, opts, &block) end |
#info? ⇒ Boolean
83 |
# File 'lib/common/log.rb', line 83 def info?; @logger.info? end |
#level=(val) ⇒ Object
84 |
# File 'lib/common/log.rb', line 84 def level=(val);@logger.level= val end |
#logger ⇒ Object
38 39 40 |
# File 'lib/common/log.rb', line 38 def logger @logger end |
#reinitialize(cfg) ⇒ Object
86 87 88 89 90 |
# File 'lib/common/log.rb', line 86 def reinitialize(cfg) @logger = Logger.new(cfg[:device]) @logger.level= cfg[:level] @logger.datetime_format= cfg[:formatter] end |
#unknown(message = nil, *opts, &block) ⇒ Object
62 63 64 |
# File 'lib/common/log.rb', line 62 def unknown(=nil, *opts, &block) add(Logger::UNKNOWN, , nil, opts, &block) end |
#warn(message = nil, *opts, &block) ⇒ Object
50 51 52 |
# File 'lib/common/log.rb', line 50 def warn(=nil, *opts, &block) add(Logger::WARN, , nil, opts, &block) end |