Class: StdOutErrLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/std_out_err_logger.rb

Overview

Custom logger to direct info to stdout and warn and above to stderr

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = "#{__dir__}/../tmp/hash_delegator_next_link_state.yaml") ⇒ StdOutErrLogger

def initialize(file = nil)



19
20
21
22
23
24
25
# File 'lib/std_out_err_logger.rb', line 19

def initialize(file = "#{__dir__}/../tmp/hash_delegator_next_link_state.yaml")
  @file = file
  super(file || STDOUT)
  self.formatter = proc do |severity, datetime, progname, msg|
    "#{msg}\n"
  end
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



16
17
18
# File 'lib/std_out_err_logger.rb', line 16

def file
  @file
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/std_out_err_logger.rb', line 27

def add(severity, message = nil, progname = nil, &block)
  message = (message || block&.call || progname) if message.nil?
  message = "- #{message.to_json}\n"
  ### message = message.join("\n") if message.is_a? Array
  out = format_message(format_severity(severity), Time.now, progname, message)
  if severity == Logger::UNKNOWN # does not follow spec, outputs to stderr for IO
    # $stderr.puts(out)
    super
  elsif severity >= Logger::WARN
    if @file
      super
    else
      $stderr.puts(out)
    end
  else
    if @file
      super
    else
      $stdout.puts(out)
    end
  end
end