Class: Yoda::Logger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/yoda/logger.rb

Constant Summary collapse

LOG_LEVELS =
%i(trace debug info warn error fatal).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ Logger

Returns a new instance of Logger.



48
49
50
51
52
# File 'lib/yoda/logger.rb', line 48

def initialize(io)
  @io = io
  @threads = {}
  @log_level = :info
end

Instance Attribute Details

#ioIO

Returns:

  • (IO)


38
39
40
# File 'lib/yoda/logger.rb', line 38

def io
  @io
end

#log_levelSymbol

Returns:

  • (Symbol)


44
45
46
# File 'lib/yoda/logger.rb', line 44

def log_level
  @log_level
end

#threadsHash<Thread> (readonly)

Returns:

  • (Hash<Thread>)


41
42
43
# File 'lib/yoda/logger.rb', line 41

def threads
  @threads
end

Class Method Details

.define_logging_method(level) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/yoda/logger.rb', line 22

def define_logging_method(level)
  define_method(level) do |content = nil, tag: nil|
    return unless public_send("allow_#{level}?")
    content = yield if block_given?
    content ||= ""
    prefix = "[#{level}]#{tag ? ' (' + tag + ')' : '' } "
    io.puts(prefix + content.to_s.lines.join(prefix))
  end

  define_method("allow_#{level}?") do
    allowed_log_levels.include?(level)
  end
end

.instanceYoda::Logger

Returns:



12
13
14
# File 'lib/yoda/logger.rb', line 12

def instance
  @instance ||= Yoda::Logger.new(STDERR)
end

Instance Method Details

#allowed_log_levelsObject



54
55
56
# File 'lib/yoda/logger.rb', line 54

def allowed_log_levels
  LOG_LEVELS.drop_while { |level| level != log_level }
end

#pipeline(tag:) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/yoda/logger.rb', line 58

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