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.



46
47
48
49
50
# File 'lib/yoda/logger.rb', line 46

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

Instance Attribute Details

#ioIO

Returns:

  • (IO)


36
37
38
# File 'lib/yoda/logger.rb', line 36

def io
  @io
end

#log_levelSymbol

Returns:

  • (Symbol)


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

def log_level
  @log_level
end

#threadsHash<Thread> (readonly)

Returns:

  • (Hash<Thread>)


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

def threads
  @threads
end

Class Method Details

.define_logging_method(level) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# 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

.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



52
53
54
# File 'lib/yoda/logger.rb', line 52

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

#pipeline(tag:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# 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