Class: Common::Log

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/common/log.rb

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

Constructor Details

#initializeLog

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

.newObject



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, message, progname, opts = {}, &block)
  message ||= ''
  raise "You can only pass String or Exception classes as a logger message, you have pass #{message.class}" unless (message.is_a?(String)) || (message.is_a?(Exception))
  progname||=""
  progname[0,0] = ("[%s] " % caller[1]) if opts.include? :line
  use_trace  = opts.include? :trace
  if (message.is_a?(Exception))
    message = "Exception: #{message.class}: #{message.message}"
    message += ", trace: #{message.backtrace}" if use_trace
  end
  if !message.nil? and block_given?
    message<<" "<< (yield).to_s
  end
  @logger.add(severity, message, 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(message=nil, *opts, &block)
  add(Logger::DEBUG, message, nil, opts, &block)
end

#debug?Boolean

Returns:

  • (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(message=nil, *opts, &block)
  add(Logger::ERROR, message, nil, opts, &block)
end

#fatal(message = nil, *opts, &block) ⇒ Object



58
59
60
# File 'lib/common/log.rb', line 58

def fatal(message=nil, *opts, &block)
  add(Logger::FATAL, message, nil, opts, &block)
end

#info(message = nil, *opts, &block) ⇒ Object



46
47
48
# File 'lib/common/log.rb', line 46

def info(message=nil, *opts, &block)
  add(Logger::INFO, message, nil, opts, &block)
end

#info?Boolean

Returns:

  • (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

#loggerObject



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(message=nil, *opts, &block)
  add(Logger::UNKNOWN, message, nil, opts, &block)
end

#warn(message = nil, *opts, &block) ⇒ Object



50
51
52
# File 'lib/common/log.rb', line 50

def warn(message=nil, *opts, &block)
  add(Logger::WARN, message, nil, opts, &block)
end