Class: TestData::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/test_data/log.rb

Constant Summary collapse

LEVELS =
[:debug, :info, :warn, :error, :quiet]
DEFAULT_WRITER =
->(message, level) do
  output = "[test_data:#{level}] #{message}"
  if [:warn, :error].include?(level)
    warn output
  else
    puts output
  end
end
PLAIN_WRITER =
->(message, level) do
  if [:warn, :error].include?(level)
    warn message
  else
    puts message
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLog

Returns a new instance of Log.



26
27
28
# File 'lib/test_data/log.rb', line 26

def initialize
  reset
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



24
25
26
# File 'lib/test_data/log.rb', line 24

def level
  @level
end

#writerObject

Returns the value of attribute writer.



24
25
26
# File 'lib/test_data/log.rb', line 24

def writer
  @writer
end

Instance Method Details

#resetObject



38
39
40
41
# File 'lib/test_data/log.rb', line 38

def reset
  self.level = ENV["TEST_DATA_LOG_LEVEL"]&.to_sym || :info
  @writer = DEFAULT_WRITER
end

#with_plain_writer(&blk) ⇒ Object



66
67
68
# File 'lib/test_data/log.rb', line 66

def with_plain_writer(&blk)
  with_writer(PLAIN_WRITER, &blk)
end

#with_writer(writer, &blk) ⇒ Object



59
60
61
62
63
64
# File 'lib/test_data/log.rb', line 59

def with_writer(writer, &blk)
  og_writer = self.writer
  self.writer = writer
  blk.call
  self.writer = og_writer
end