Class: Vivarium::Logger

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

Constant Summary collapse

FORMATS =
%i[human json].freeze

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)


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

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



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

def close
  @io.close if @owned
end

#info(message) ⇒ Object



33
34
35
36
# File 'lib/vivarium/logger.rb', line 33

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

#log(events, tp, stack) ⇒ Object



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

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