class F4R::F4RLogger

Singleton to provide a common logging mechanism for all objects. It exposes essentially the same interface as the Logger class but just as a singleton and with some additional methods like 'debug', 'warn', 'info'.

It also facilitates configurable log output redirection based on severity levels to help reduce noise in the different output devices.

Public Instance Methods

color=(bool) click to toggle source

Allow other programs to enable or disable colour output.

@example

F4R::Log.color = true

@param [Boolean] bool

# File lib/f4r.rb, line 183
def color=(bool)
  @color = bool
end
color?() click to toggle source

When set to True enables logger colour output.

@return [Boolean] +@color+

# File lib/f4r.rb, line 192
def color?
  @color ||= false
end
debug(msg = '', items = {}, &block) click to toggle source

DEBUG level messages.

@param [String, Array<String>] msg

Mostly used to locate or describe items in the +items+ parameter.

String: Simple text message.

Array<String>: List of key words to be concatenated with a '#' inside
'<>' (see: {format_message}). Meant to be used for describing the class
and method where the log message was called from.

  Example:
    >> ['F4R::Record', 'fields'] #=> '<F4R::Record#fields>'

@param [Hash] items

Key/Value list of items for debugging.

@yield [block] passed directly to the [F4RLogger] logger. @return [String] formatted message.

Example:

>> Log.debug [self.class, __method__], {a:1, b:2}
=> DEBUG  <F4R::Record#fields> a: 1 b: 2
# File lib/f4r.rb, line 223
def debug(msg = '', items = {},  &block)
  logger.debug(format_message(msg, items), &block)
end
decode(msg, items = {}, &block) click to toggle source

DECODE level messages.

Similar to {debug} but with its specific [F4RLogger] logger.

@param [String, Array<String>] msg @param [Hash] items @yield [block] passed directly to the [F4RLogger] logger @return [String] formatted message

# File lib/f4r.rb, line 290
def decode(msg, items = {}, &block)
  decode_logger.decode(format_message(msg, items), &block)
end
decode_logger() click to toggle source

@return [Logger] +@decode_logger+

# File lib/f4r.rb, line 146
def decode_logger
  @decode_logger ||= Logger.new('/tmp/f4r-decode.log')
end
decode_logger=(logger) click to toggle source

@example

F4R::Log.decode_logger = F4R::Logger.new($stdout)

@param [Logger] logger @return [Logger] +@decode_logger+

# File lib/f4r.rb, line 125
def decode_logger=(logger)
  log_formater(logger) && @decode_logger = logger
end
encode(msg, items = {}, &block) click to toggle source

ENCODE level messages.

Similar to {debug} but with its specific [F4RLogger] logger

@param [String, Array<String>] msg @param [Hash] items @yield [block] passed directly to the [F4RLogger] logger @return [String] formatted message

# File lib/f4r.rb, line 276
def encode(msg, items = {}, &block)
  decode_logger.encode(format_message(msg, items), &block)
end
encode_logger() click to toggle source

@return [Logger] +@encode_logger+

# File lib/f4r.rb, line 139
def encode_logger
  @encode_logger ||= Logger.new('/tmp/f4r-encode.log')
end
encode_logger=(logger) click to toggle source

@example

F4R::Log.encode_logger = F4R::Logger.new($stdout)

@param [Logger] logger @return [Logger] +@encode_logger+

# File lib/f4r.rb, line 114
def encode_logger=(logger)
  log_formater(logger) && @encode_logger = logger
end
error(msg, &block) click to toggle source

ERROR level messages.

Raises [F4R::ERROR].

@param [String] msg Passed directly to the [F4RLogger] logger. @yield [block] passed directly to the [F4RLogger] logger. @raise [F4R::Error] with formatted message.

# File lib/f4r.rb, line 261
def error(msg, &block)
  logger.error(msg, &block)
  raise Error, msg
end
format_message(msg, items) click to toggle source

Formats message and items for the [F4RLogger] logger output. It also adds colour when {color?} has been set to true.

@param [String, Array<String, Object>] msg @param [Hash] items @return [String] formatted message

# File lib/f4r.rb, line 318
def format_message(msg, items)
  if msg.is_a?(Array)
    if Log.color?
      msg =  Log.tint(:blue, "<#{msg.join('#')}>")
    else
      msg = "<#{msg.join('#')}>"
    end
  end

  items.each do |k, v|
    k = Log.color? ? Log.tint(:green, k.to_s): k.to_s
    msg += " #{k}: #{v.to_s}"
  end
  msg
end
info(msg, &block) click to toggle source

INFO level messages.

@param [String] msg passed directly to the [F4RLogger] logger @yield [block] passed directly to the [F4RLogger] logger @return [String] formatted message

# File lib/f4r.rb, line 234
def info(msg, &block)
  logger.info(msg, &block)
end
level() click to toggle source

Severity level for all [F4RLogger] loggers.

@return [Symbol, String, Integer] @@level

# File lib/f4r.rb, line 171
def level
  @level ||= :error
end
level=(level) click to toggle source

Method for setting the severity level for all loggers.

@example

F4R::Log.level = :error

@param [Symbol, String, Integer] level

# File lib/f4r.rb, line 158
def level=(level)
  [
    logger,
    decode_logger,
    encode_logger
  ].each { |lgr| lgr.level = level}
end
log_formater(logger) click to toggle source

Logger formatter configuration

# File lib/f4r.rb, line 337
def log_formater(logger)
  logger.formatter = proc do |severity, _, _, msg|

    if Log.color?
      sc = {
        'DEBUG' => :magenta,
        'INFO' => :blue,
        'WARN' => :yellow,
        'ERROR' => :red,
        'ENCODE' => :green,
        'DECODE' => :cyan,
      }
      Log.tint(sc[severity], "#{'%-6s' % severity} ") + "#{msg}\n"
    else
      severity + " #{msg}\n"
    end

  end
end
logger() click to toggle source

@return [Logger] +@logger+

# File lib/f4r.rb, line 132
def logger
  @logger ||= Logger.new($stdout)
end
logger=(logger) click to toggle source

@example

F4R::Log.logger = F4R::Logger.new($stdout)

@param [Logger] logger @return [Logger] +@logger+

# File lib/f4r.rb, line 103
def logger=(logger)
  log_formater(logger) && @logger = logger
end
tint(clr, text) click to toggle source

Simple colour codes mapping.

@param [Symbol] clr to define colour code to use @param [String] text to be coloured @return [String] text with the proper colour code

# File lib/f4r.rb, line 301
def tint(clr, text)
  codes = {
    none: 0, bright: 1, black: 30, red: 31,
    green: 32, yellow: 33, blue: 34,
    magenta: 35, cyan: 36, white: 37, default: 39,
  }
  ["\x1B[", codes[clr].to_s, 'm', text.to_s, "\x1B[0m"].join
end
warn(msg, &block) click to toggle source

WARN level messages.

@param [String] msg

Passed directly to the [F4RLogger] logger after removing all newlines.

@yield [block] passed directly to the [F4RLogger] logger @return [String] formatted message

# File lib/f4r.rb, line 248
def warn(msg, &block)
  logger.warn(msg.gsub(/\n/, ' '), &block)
end