Module: Libis::Tools::Logger

Defined in:
lib/libis/tools/logger.rb

Overview

This module adds logging functionality to any class.

Just include the ::Libis::Tools::Logger module and the methods debug, info, warn, error and fatal will be available to the class instance. Each method takes a message argument and optional extra parameters.

It is possible to overwrite the #logger method with your own implementation to use a different logger for your class.

The methods all call the #message method with the logging level as first argument and the supplied arguments appended.

Example:

require 'libis/tools/logger'
class TestLogger
  include ::Libis::Tools::Logger
  attr_accessor :options, name
end
tl = TestLogger.new
tl.debug 'message'
tl.warn 'message'
tl.error 'huge error: [%d] %s', 1000, 'Exit'
tl.info 'Running application: %s', t.class.name

produces:

D, [...] DEBUG : message
W, [...]  WARN : message
E, [...] ERROR : huge error: [1000] Exit
I, [...]  INFO : Running application TestLogger

Instance Method Summary collapse

Instance Method Details

#debug(msg, *args) ⇒ Object

Send a debug message to the logger.

If the optional extra parameters are supplied, the first parameter will be interpreted as a format specification. It’s up to the caller to make sure the format specification complies with the number and types of the extra arguments. If the format substitution fails, the message will be printed as: ‘<msg> - [<args>]’.

@!method(debug(msg, *args))

Parameters:

  • msg (String)

    the message.

  • args (Array)

    optional extra arguments.



69
70
71
# File 'lib/libis/tools/logger.rb', line 69

def debug(msg, *args)
  self.message :DEBUG, msg, *args
end

#error(msg, *args) ⇒ Object

Send an error message to the logger.

(see #debug) @!method(error(msg, *args))

Parameters:

  • msg (String)

    the message.

  • args (Array)

    optional extra arguments.



96
97
98
# File 'lib/libis/tools/logger.rb', line 96

def error(msg, *args)
  self.message :ERROR, msg, *args
end

#fatal(msg, *args) ⇒ Object

Send a fatal message to the logger.

(see #debug) @!method(fatal(msg, *args))

Parameters:

  • msg (String)

    the message.

  • args (Array)

    optional extra arguments.



105
106
107
# File 'lib/libis/tools/logger.rb', line 105

def fatal(msg, *args)
  self.message :FATAL, msg, *args
end

#info(msg, *args) ⇒ Object

Send an info message to the logger.

(see #debug) @!method(info(msg, *args))

Parameters:

  • msg (String)

    the message.

  • args (Array)

    optional extra arguments.



78
79
80
# File 'lib/libis/tools/logger.rb', line 78

def info(msg, *args)
  self.message :INFO, msg, *args
end

#loggerObject

Get the logger instance

Default implementation is to get the root logger from the Config, but can be overwritten for sub-loggers. @!method(logger)



46
47
48
# File 'lib/libis/tools/logger.rb', line 46

def logger
  ::Libis::Tools::Config.logger
end

#message(severity, msg, *args) ⇒ Object

The method that performs the code logging action.

If extra arguments are supplied, the message string is expected to be a format specification string and the extra arguments will be applied to it.

This default message method implementation uses the logger of ::Libis::Tools::Config. If an ‘appname’ parameter is defined in the Config object, it will be used as program name by the logger, otherwise the class name is taken.

@!method(message(severity, msg, *args))

Parameters:

  • severity ({::Logger::Severity})
  • msg (String)

    message string

  • args (Object)

    optional list of extra arguments



122
123
124
125
# File 'lib/libis/tools/logger.rb', line 122

def message(severity, msg, *args)
  message_text = (msg % args rescue "#{msg}#{args.empty? ? '' : " - #{args}"}")
  self.logger.add(::Logging.level_num(severity), message_text)
end

#set_application(name = nil) ⇒ Object



50
51
52
53
# File 'lib/libis/tools/logger.rb', line 50

def set_application(name = nil)
  name ||= self.class.name
  ::Logging.mdc['Application'] = name.blank? ? '' : " -- #{name}"
end

#set_subject(name = nil) ⇒ Object



55
56
57
# File 'lib/libis/tools/logger.rb', line 55

def set_subject(name = nil)
  ::Logging.mdc['Subject'] = name.blank? ? '' : " - #{name}"
end

#warn(msg, *args) ⇒ Object

Send a warning message to the logger.

(see #debug) @!method(warn(msg, *args))

Parameters:

  • msg (String)

    the message.

  • args (Array)

    optional extra arguments.



87
88
89
# File 'lib/libis/tools/logger.rb', line 87

def warn(msg, *args)
  self.message :WARN, msg, *args
end