module Loggerator

Public Class Methods

config() { |config| ... } click to toggle source
# File lib/loggerator/loggerator.rb, line 11
def self.config
  @config ||= Configuration.new

  return @config unless block_given?

  yield(@config)
end
config=(cfg) click to toggle source
# File lib/loggerator/loggerator.rb, line 19
def self.config=(cfg)
  @config = Configuration.new(cfg)
end
included(mod) click to toggle source
# File lib/loggerator/loggerator.rb, line 7
def self.included(mod)
  mod.extend self
end
log?() click to toggle source
# File lib/loggerator/test.rb, line 23
def log?
  @@log_switch
end
turn_log(on_or_off) click to toggle source
# File lib/loggerator/test.rb, line 16
def turn_log(on_or_off)
  return unless %i[on off].include?(on_or_off.to_sym)
  alias_method :log,       :"log_#{on_or_off}"
  alias_method :log_error, :"log_error_#{on_or_off}"
  @@log_switch = on_or_off.to_sym == :on
end

Public Instance Methods

log(data, &block) click to toggle source
# File lib/loggerator/loggerator.rb, line 23
def log(data, &block)
  Log.to_stream(Log.stdout, Log.contexts(data), &block)
end
Also aliased as: log_on
log_context(data, &block) click to toggle source
# File lib/loggerator/loggerator.rb, line 54
def log_context(data, &block)
  old = Log.local_context
  Log.local_context = old.merge(data)
  res = block.call
ensure
  Log.local_context = old
  res
end
log_error(error, data = {}) click to toggle source
# File lib/loggerator/loggerator.rb, line 27
def log_error(error, data = {})
  exception_id = error.object_id

  # Log backtrace in reverse order for easier digestion.
  if error.backtrace
    error.backtrace.reverse.each do |backtrace|
      Log.to_stream(Log.stderr, Log.contexts(
        exception_id: exception_id,
        backtrace:    backtrace
      ))
    end
  end

  # then log the exception message last so that it's as close to the end of
  # a log trace as possible
  data.merge!(
    exception:    true,
    class:        error.class.name,
    message:      error.message,
    exception_id: exception_id
  )

  data[:status] = error.status if error.respond_to?(:status)

  Log.to_stream(Log.stderr, Log.contexts(data))
end
Also aliased as: log_error_on
log_error_off(e, data={}, &block) click to toggle source
# File lib/loggerator/test.rb, line 9
def log_error_off(e, data={}, &block)
  block.call if block
end
log_error_on(error, data = {})
Alias for: log_error
log_off(data, &block) click to toggle source
# File lib/loggerator/test.rb, line 5
def log_off(data, &block)
  block.call if block
end
log_on(data, &block)
Alias for: log
m() click to toggle source

included Metrics shortcut

# File lib/loggerator/metrics.rb, line 26
def m; Metrics; end