Class: Tml::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/tml/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#external_loggerObject

Returns the value of attribute external_logger.



59
60
61
# File 'lib/tml/logger.rb', line 59

def external_logger
  @external_logger
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



101
102
103
104
# File 'lib/tml/logger.rb', line 101

def add(severity, message = nil, progname = nil, &block)
  return unless Tml.config.logger[:enabled]
  super
end

#debug(message) ⇒ Object



72
73
74
75
76
# File 'lib/tml/logger.rb', line 72

def debug(message)
  log_to_console(message)
  return external_logger.debug(format_message(Logger::Severity::DEBUG, Time.new, nil, message)) if external_logger
  super
end

#error(message) ⇒ Object



84
85
86
87
88
# File 'lib/tml/logger.rb', line 84

def error(message)
  log_to_console(message)
  return external_logger.error(format_message(Logger::Severity::ERROR, Time.new, nil, message)) if external_logger
  super
end

#fatal(message) ⇒ Object



90
91
92
93
94
# File 'lib/tml/logger.rb', line 90

def fatal(message)
  log_to_console(message)
  return external_logger.fatal(format_message(Logger::Severity::FATAL, Time.new, nil, message)) if external_logger
  super
end

#format_message(severity, timestamp, progname, msg) ⇒ Object



96
97
98
99
# File 'lib/tml/logger.rb', line 96

def format_message(severity, timestamp, progname, msg)
  return '' unless Tml.config.logger[:enabled]
  "[#{timestamp.strftime('%D %T')}]: tml: #{' ' * stack.size}#{msg}\n"
end

#info(message) ⇒ Object



66
67
68
69
70
# File 'lib/tml/logger.rb', line 66

def info(message)
  log_to_console(message)
  return external_logger.info(format_message(Logger::Severity::INFO, Time.new, nil, message)) if external_logger
  super
end

#log_to_console(msg) ⇒ Object



61
62
63
64
# File 'lib/tml/logger.rb', line 61

def log_to_console(msg)
  return unless Tml.config.logger[:console]
  puts msg
end

#stackObject



106
107
108
# File 'lib/tml/logger.rb', line 106

def stack
  @stack ||= []
end

#trace(message) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/tml/logger.rb', line 110

def trace(message)
  debug(message)
  stack.push(caller)
  t0 = Time.now
  if block_given?
    ret = yield
  end
  t1 = Time.now
  stack.pop
  debug("execution took #{t1 - t0} seconds")
  ret
end

#warn(message) ⇒ Object



78
79
80
81
82
# File 'lib/tml/logger.rb', line 78

def warn(message)
  log_to_console(message)
  return external_logger.warn(format_message(Logger::Severity::WARN, Time.new, nil, message)) if external_logger
  super
end