Module: LogSource

Defined in:
lib/highlogger.rb

Overview

LogSource sets up simple logging functions accessible from a class’s namespace when it is included. A logger and component name are usually set in the initialize() function of the including object. The default logger will log to STDERR, and the default component name is the caller’s file name.

Constant Summary collapse

@@highlogger_default_logger =
HighLogger.new(STDERR)

Instance Method Summary collapse

Instance Method Details

#debug(progname = nil, &block) ⇒ Object

Log a DEBUG message to the current logger and component.



242
243
244
# File 'lib/highlogger.rb', line 242

def debug(progname = nil, &block)
  add(Logger::DEBUG, nil, progname, &block)
end

#error(progname = nil, &block) ⇒ Object

Log an ERROR message to the current logger and component.



260
261
262
# File 'lib/highlogger.rb', line 260

def error(progname = nil, &block)
  add(Logger::ERROR, nil, progname, &block)
end

#fatal(progname = nil, &block) ⇒ Object

Log a FATAL message to the current logger and component.



266
267
268
# File 'lib/highlogger.rb', line 266

def fatal(progname = nil, &block)
  add(Logger::FATAL, nil, progname, &block)
end

#info(progname = nil, &block) ⇒ Object

Log an INFO message to the current logger and component.



248
249
250
# File 'lib/highlogger.rb', line 248

def info(progname = nil, &block)
  add(Logger::INFO, nil, progname, &block)
end

#log(severity, *strings, &block) ⇒ Object Also known as: add

Add a message to the current logger, with the appropriate component name.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/highlogger.rb', line 222

def log(severity, *strings, &block)
  if @highlogger_component
    comp = @highlogger_component
  else
    caller_str = caller()[0]
    matchobj = /([^\/]+)\.rb/.match(caller_str)
    comp = matchobj[1]
  end

  if @highlogger_logger
    @highlogger_logger.add(comp, severity, *strings, &block)
  else
    @@highlogger_default_logger.add(comp, severity, *strings, &block)
  end
end

#set_component(comp_name) ⇒ Object

Set the component string that the LogSource reports with.



215
216
217
# File 'lib/highlogger.rb', line 215

def set_component(comp_name)
  @highlogger_component = comp_name
end

#set_logger(logger_obj) ⇒ Object

Set the HighLogger or other Logger that this LogSource will log to. The given object must accept an appropriate @add()@ method call.



208
209
210
211
# File 'lib/highlogger.rb', line 208

def set_logger(logger_obj)
  @highlogger_logger = logger_obj
  @highlogger_component = nil
end

#unknown(progname = nil, &block) ⇒ Object

Log an UNKNOWN message to the current logger and component. UNKNOWN is more severe than FATAL.



273
274
275
# File 'lib/highlogger.rb', line 273

def unknown(progname = nil, &block)
  add(Logger::UNKNOWN, nil, progname, &block)
end

#warn(progname = nil, &block) ⇒ Object

Log a WARN message to the current logger and component.



254
255
256
# File 'lib/highlogger.rb', line 254

def warn(progname = nil, &block)
  add(Logger::WARN, nil, progname, &block)
end