Class: Vivarium::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/vivarium/logger.rb

Constant Summary collapse

FORMATS =
%i[human json].freeze
ANSI_RED =
"\e[31m"
ANSI_RESET =
"\e[0m"

Instance Method Summary collapse

Constructor Details

#initialize(dest: $stdout, format: :human) ⇒ Logger

dest: IO object or file path string format: :human or :json TODO: support flushing in bulk for performance

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vivarium/logger.rb', line 14

def initialize(dest: $stdout, format: :human)
  @format = format.to_sym
  raise ArgumentError, "unknown format: #{@format}; choose from #{FORMATS.join(', ')}" unless FORMATS.include?(@format)

  if dest.is_a?(String)
    @io = File.open(dest, "a")
    @owned = true
  else
    @io = dest
    @owned = false
  end
end

Instance Method Details

#closeObject



40
41
42
# File 'lib/vivarium/logger.rb', line 40

def close
  @io.close if @owned
end

#info(message) ⇒ Object



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

def info(message)
  @io.puts("[vivarium] #{message}")
  @io.flush
end

#log(events, tp, stack) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/vivarium/logger.rb', line 27

def log(events, tp, stack)
  case @format
  when :human then log_human(events, tp, stack)
  when :json  then log_json(events, tp, stack)
  end
  @io.flush
end