class Logz::LoggerWrapper

Attributes

logger[RW]

Public Class Methods

new(logger) click to toggle source
# File lib/logz/logger_wrapper.rb, line 5
def initialize(logger)
  @logger = Logger.new(logger)
  reset
end

Public Instance Methods

add(severity, message = nil, progname = nil) click to toggle source
# File lib/logz/logger_wrapper.rb, line 43
def add(severity, message = nil, progname = nil)
  severity = Logger.const_get(severity.upcase) if severity.is_a?(Symbol)
  logger.send(:add, severity, to_output(message), progname)
ensure
  reset
end
color(color_name) click to toggle source
# File lib/logz/logger_wrapper.rb, line 15
def color(color_name)
  @color = color_name.to_sym
  self
end
has_level?(level) click to toggle source
# File lib/logz/logger_wrapper.rb, line 39
def has_level?(level)
  Logz.config.levels.include?(level)
end
method_missing(m, *args, &block) click to toggle source
Calls superclass method
# File lib/logz/logger_wrapper.rb, line 52
def method_missing(m, *args, &block)
  if has_level?(m)
    add(m, *args)
  elsif logger.respond_to?(m)
    logger.send(m, *args)
  elsif Rainbow::X11ColorNames::NAMES.include?(m)
    color(m)
  else
    super
  end
end
reset() click to toggle source
# File lib/logz/logger_wrapper.rb, line 10
def reset
  @tags = []
  @color = nil
end
tag(tags) click to toggle source

logger.tag(user_id: 1).info(“foo”) logger.tag(“user is nil”).error(“bar”) logger.tag([“a”, “b”]).debug(“bar”)

# File lib/logz/logger_wrapper.rb, line 23
def tag(tags)
  if tags.is_a?(Hash)
    @tags << tags.map { |k, v| "[#{k.to_s.upcase}=#{v}]" }.join(" ")
  elsif tags.is_a?(Array)
    @tags << tags.map { |v| "[#{v.to_s}]" }.join(" ")
  else
    @tags << "[#{tags.to_s}]"
  end

  @tags.uniq!

  self
end
Also aliased as: tagged
tagged(tags)
Also aliased as: tag
Alias for: tag

Private Instance Methods

to_output(msg) click to toggle source
# File lib/logz/logger_wrapper.rb, line 66
def to_output(msg)
  output = @tags.empty? ? msg : "#{@tags.join(" ")} #{msg}"
  output = @color ? Rainbow(output).color(@color.to_sym) : output
end