class Yoda::Logger

Constants

LOG_LEVELS

Attributes

io[RW]

@return [IO]

log_level[RW]

@return [Symbol]

threads[R]

@return [Hash<Thread>]

Public Class Methods

define_logging_method(level) click to toggle source

@!macro [attach]

@!method $1(content, tag: nil)
  @param content [String]
  @param tag [String, nil]
@!method allow_$1?
  @return [true, false]
# File lib/yoda/logger.rb, line 22
def define_logging_method(level)
  define_method(level) do |content, tag: nil|
    return unless public_send("allow_#{level}?")
    prefix = "[#{level}]#{tag ? ' (' + tag + ')' : '' } "
    io.puts(prefix + content.to_s.split("\n").join(prefix))
  end

  define_method("allow_#{level}?") do
    allowed_log_levels.include?(level)
  end
end
instance() click to toggle source

@return [Yoda::Logger]

# File lib/yoda/logger.rb, line 12
def instance
  @instance ||= Yoda::Logger.new(STDERR)
end
new(io) click to toggle source
# File lib/yoda/logger.rb, line 46
def initialize(io)
  @io = io
  @threads = {}
  @log_level = :info
end

Public Instance Methods

allowed_log_levels() click to toggle source
# File lib/yoda/logger.rb, line 52
def allowed_log_levels
  LOG_LEVELS.drop_while { |level| level != log_level }
end
pipeline(tag:) click to toggle source
# File lib/yoda/logger.rb, line 56
def pipeline(tag:)
  threads[tag] ||= begin
    r, w = IO.pipe
    Thread.new do
      r.each do |content|
        debug(content.chomp, tag: tag)
      end
    end
    w
  end
end