module Skylight::Core::Util::Logging

Public Instance Methods

config_for_logging() click to toggle source
# File lib/skylight/core/util/logging.rb, line 104
def config_for_logging
  if respond_to?(:config)
    config
  elsif is_a?(Config)
    self
  end
end
debug(msg, *args) click to toggle source

@param msg (see log) @param args (see log)

# File lib/skylight/core/util/logging.rb, line 69
def debug(msg, *args)
  log :debug, msg, *args
end
Also aliased as: log_debug
error(msg, *args) click to toggle source

@param msg (see log) @param args (see log)

# File lib/skylight/core/util/logging.rb, line 87
def error(msg, *args)
  log :error, msg, *args
  raise format(msg, *args) if raise_on_error?
end
Also aliased as: log_error
fmt(*args) click to toggle source

Alias for `Kernel#sprintf` @return [String]

# File lib/skylight/core/util/logging.rb, line 100
def fmt(*args)
  sprintf(*args)
end
info(msg, *args) click to toggle source

@param msg (see log) @param args (see log)

# File lib/skylight/core/util/logging.rb, line 75
def info(msg, *args)
  log :info, msg, *args
end
Also aliased as: log_info
log(level, msg, *args) click to toggle source

@param level [String,Symbol] the method on `logger` to use for logging @param msg [String] the message to log @param args [Array] values for `Kernel#sprintf` on `msg`

# File lib/skylight/core/util/logging.rb, line 115
def log(level, msg, *args)
  c = config_for_logging
  logger = c ? c.logger : nil

  msg = log_context.map { |(k, v)| "#{k}=#{v}; " }.join << msg

  if logger
    if logger.respond_to?(level)
      if !args.empty?
        logger.send level, format(msg, *args)
      else
        logger.send level, msg
      end
      return
    else
      Kernel.warn "Invalid logger"
    end
  end

  # Fallback
  if (module_name = is_a?(Module) ? name : self.class.name)
    root_name = module_name.split("::").first.upcase
    msg.prepend("[#{root_name}] ")
  end
  puts format(msg, *args)
rescue Exception => e
  if trace?
    puts "[ERROR] #{e.message}"
    puts e.backtrace
  end
end
log_context() click to toggle source
# File lib/skylight/core/util/logging.rb, line 26
def log_context
  {}
end
log_debug(msg, *args)
Alias for: debug
log_env_prefix() click to toggle source
# File lib/skylight/core/util/logging.rb, line 30
def log_env_prefix
  if (c = config_for_logging)
    c.class.env_prefix
  else
    "SKYLIGHT_"
  end
end
log_error(msg, *args)
Alias for: error
log_info(msg, *args)
Alias for: info
log_trace(msg, *args)
Alias for: trace
log_warn(msg, *args)
Alias for: warn
raise_on_error?() click to toggle source
# File lib/skylight/core/util/logging.rb, line 42
def raise_on_error?
  !!ENV["#{log_env_prefix}RAISE_ON_ERROR"]
end
t() { || ... } click to toggle source

Evaluates and logs the result of the block if tracing

@yield block to be evaluted @yieldreturn arguments for {#debug}

See {trace?}.

# File lib/skylight/core/util/logging.rb, line 62
def t
  return unless trace?
  log :debug, yield
end
trace(msg, *args) click to toggle source

Logs if tracing

@param (see debug)

See {trace?}.

# File lib/skylight/core/util/logging.rb, line 51
def trace(msg, *args)
  return unless trace?
  log :debug, msg, *args
end
Also aliased as: log_trace
trace?() click to toggle source
# File lib/skylight/core/util/logging.rb, line 38
def trace?
  !!ENV["#{log_env_prefix}ENABLE_TRACE_LOGS"]
end
warn(msg, *args) click to toggle source

@param msg (see log) @param args (see log)

# File lib/skylight/core/util/logging.rb, line 81
def warn(msg, *args)
  log :warn, msg, *args
end
Also aliased as: log_warn