module LunaPark::Extensions::SeverityLevels

Constants

LEVELS

This is class define interface for loggers and notifiers behavior. In main idea it based on rfc5424 tools.ietf.org/html/rfc5424, but in fact default ruby logger does not define all severities, and we use only most important:

- unknown: an unknown message that should always be logged
- fatal: An unhandleable error that results in a program crash
- error: system work incorrectly, and maintainer should know about that immediately
- warning: warning conditions, and maintainer should know about that, but not immediately
- info: informational messages, maintainer should know about that, if they want to analyse logs
- debug: debug messages, for developers don't use it on production

@example

class ChattyLogger < LunaPark::Notifiers::Abstract
  def message(obj, _details:, lvl:)
    puts "{lvl.upcase}: #{message}"
  end
end

logger = ChattyLogger.new min_lvl: :warning
logger.unknown 'Do not do that.' # => 'UNKNOWN: Do not do that.'
logger.fatal 'Do not do that.'   # => 'FATAL: Do not do that.'
logger.error 'Do not do that.'   # => 'ERROR: Do not do that.'
logger.warning 'Do not do that.' # => 'WARNING: Do not do that.'
logger.info 'Do not do that.'    # => nil
logger.debug 'Do not do that.'   # => nil

Public Instance Methods

debug(msg = '', **details) { |: msg| ... } click to toggle source

Post message with DEBUG severity level

@param msg [String,Exception] @param details [Hash]

# File lib/luna_park/extensions/severity_levels.rb, line 105
def debug(msg = '', **details)
  if min_lvl == :debug
    message = block_given? ? yield : msg
    post message, lvl: :debug, **details
  end
end
error(msg = '', **details) { |: msg| ... } click to toggle source

Post message with ERROR severity level

@param msg [String,Exception] @param details [Hash]

# File lib/luna_park/extensions/severity_levels.rb, line 70
def error(msg = '', **details)
  if %i[debug info warning error].include? min_lvl
    message = block_given? ? yield : msg
    post message, lvl: :error, **details
  end
end
fatal(msg = '', **details) { |: msg| ... } click to toggle source

Post message with FATAL severity level

@param msg [String,Exception] @param details [Hash]

# File lib/luna_park/extensions/severity_levels.rb, line 59
def fatal(msg = '', **details)
  if %i[debug info warning error fatal].include? min_lvl
    message = block_given? ? yield : msg
    post message, lvl: :fatal, **details
  end
end
info(msg = '', **details) { |: msg| ... } click to toggle source

Post message with INFO severity level

@example

@param msg [String,Exception] @param details [Hash]

# File lib/luna_park/extensions/severity_levels.rb, line 94
def info(msg = '', **details)
  if %i[debug info].include? min_lvl
    message = block_given? ? yield : msg
    post message, lvl: :info, **details
  end
end
min_lvl() click to toggle source

Defined minimum severity level

# File lib/luna_park/extensions/severity_levels.rb, line 34
def min_lvl
  @min_lvl ||= :debug
end
min_lvl=(value) click to toggle source
# File lib/luna_park/extensions/severity_levels.rb, line 38
def min_lvl=(value)
  raise ArgumentError, 'Undefined severity level' unless LEVELS.include? value

  @min_lvl = value
end
post(_msg = '', _lvl:, **_details) click to toggle source

@abstract

# File lib/luna_park/extensions/severity_levels.rb, line 115
def post(_msg = '', _lvl:, **_details)
  raise Errors::AbstractMethod
end
unknown(msg = '', **details) { |: msg| ... } click to toggle source

Post message with UNKNOWN severity level

@param msg [String,Exception] @param details [Hash]

# File lib/luna_park/extensions/severity_levels.rb, line 50
def unknown(msg = '', **details)
  message = block_given? ? yield : msg
  post message, lvl: :unknown, **details
end
warning(msg = '', **details) { |: msg| ... } click to toggle source

Post stdout message with WARNING severity level

@param msg [String,Exception] @param details [Hash]

# File lib/luna_park/extensions/severity_levels.rb, line 81
def warning(msg = '', **details)
  if %i[debug info warning].include? min_lvl
    message = block_given? ? yield : msg
    post message, lvl: :warning, **details
  end
end