Class: StdOutErrLogger
Overview
Custom logger to direct info to stdout and warn and above to stderr
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Instance Method Summary collapse
- #add(severity, message = nil, progname = nil, &block) ⇒ Object
-
#initialize(file = "#{__dir__}/../tmp/hash_delegator_next_link_state.yaml") ⇒ StdOutErrLogger
constructor
def initialize(file = nil).
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
#file ⇒ Object (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, = nil, progname = nil, &block) = ( || block&.call || progname) if .nil? = "- #{.to_json}\n" ### message = message.join("\n") if message.is_a? Array out = (format_severity(severity), Time.now, progname, ) 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 |