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, **kwargs) ⇒ 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, **kwargs))

Parameters:

  • msg (String)

    the message.

  • args (Array)

    optional extra arguments.

  • kwargs (Hash)

    optional keyword arguments



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

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

#error(msg, *args, **kwargs) ⇒ Object

Send an error message to the logger.

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

Parameters:

  • msg (String)

    the message.

  • args (Array)

    optional extra arguments.

  • kwargs (Hash)

    optional keyword arguments



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

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

#fatal_error(msg, *args, **kwargs) ⇒ Object

Send a fatal message to the logger.

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

Parameters:

  • msg (String)

    the message.

  • args (Array)

    optional extra arguments.

  • kwargs (Hash)

    optional keyword arguments



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

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

#info(msg, *args, **kwargs) ⇒ Object

Send an info message to the logger.

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

Parameters:

  • msg (String)

    the message.

  • args (Array)

    optional extra arguments.

  • kwargs (Hash)

    optional keyword arguments



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

def info(msg, *args, **kwargs)
  self.message :INFO, msg, *args, **kwargs
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)



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

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

#message(severity, msg, *args, **kwargs) ⇒ 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, **kwargs))

Parameters:

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

    message string

  • args (Object)

    optional list of extra arguments



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

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

#set_application(name = nil) ⇒ Object



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

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

#set_subject(name = nil) ⇒ Object



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

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

#warn(msg, *args, **kwargs) ⇒ Object

Send a warning message to the logger.

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

Parameters:

  • msg (String)

    the message.

  • args (Array)

    optional extra arguments.

  • kwargs (Hash)

    optional keyword arguments



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

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