Class: Yoda::Logger
- Inherits:
-
Object
- Object
- Yoda::Logger
- 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
- #allowed_log_levels ⇒ Object
-
#initialize(io) ⇒ Logger
constructor
A new instance of Logger.
- #pipeline(tag:) ⇒ Object
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
#io ⇒ IO
38 39 40 |
# File 'lib/yoda/logger.rb', line 38 def io @io end |
#log_level ⇒ Symbol
44 45 46 |
# File 'lib/yoda/logger.rb', line 44 def log_level @log_level end |
#threads ⇒ Hash<Thread> (readonly)
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 |
.instance ⇒ Yoda::Logger
12 13 14 |
# File 'lib/yoda/logger.rb', line 12 def instance @instance ||= Yoda::Logger.new(STDERR) end |
Instance Method Details
#allowed_log_levels ⇒ Object
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 |