Module: Chemlab::Runtime::Logger

Included in:
Configuration
Defined in:
lib/chemlab/runtime/logger.rb

Overview

Logger module

Instance Method Summary collapse

Instance Method Details

#log(obj, type = :info, **args) ⇒ Object

Log something to a stream

Examples:

log('something', :info)                                   #=> logs INFO :: something, to $stdout
log('error', :err)                                        #=> logs ERR  :: error, to $stderr
log('important', :important, stream: $stderr)             #=> logs IMPO :: important, to $stderr
log('to file', :info, stream: File.open('file.txt', 'w')) #=> logs INFO :: to file, to the file.txt file

Parameters:

  • obj (Any)

    Any object that responds to #to_s

  • type (Symbol) (defaults to: :info)

    The type of message logged

  • args (Hash)

    Any additional arguments

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
# File 'lib/chemlab/runtime/logger.rb', line 16

def log(obj, type = :info, **args)
  raise ArgumentError, "Cannot log #{obj} as it does not respond to to_s!" unless obj.respond_to?(:to_s)

  args[:stream] = type.match?(/(err(or)?|warn(ing)?)/) ? $stderr : $stdout

  prefix = Time.now.strftime('%Y-%m-%d %H:%M:%S / ')
  prefix << type[0..3].upcase << "\t:: " # we only want the prefix to be four chars long
  args[:stream].puts(prefix << obj.to_s)
end