class Hive::Log

Hive logging Allow logging to be written to multiple locations.

Attributes

default_progname[RW]
hive_mind[RW]

Public Class Methods

new(args = []) click to toggle source

Create the logger:

# No log files will be written
log = Hive::Log.new()
# Write log files to standard out and a log file
log = Hive::Log.new( [
                        {
                          stream: 'Filename.log',
                          level: 'DEBUG'
                        },
                        {
                          stream: STDOUT,
                          level: 'INFO'
                        },
                      ] )
# File lib/hive/log.rb, line 25
def initialize(args = [])
  @loggers = {}
  args.each do |l|
    add_logger(l[:stream], l[:level])
  end
end

Public Instance Methods

add_logger(stream, level) click to toggle source

Add a new log location:

# INFO level log to 'Filename.log'
log.add_logger( 'Filename.log', 'INFO' )
# DEBUG level log to standard output
log.add_logger( STDOUT, 'DEBUG' )
# File lib/hive/log.rb, line 38
def add_logger(stream, level)
  log = MonoLogger.new(stream)
  log.formatter = proc do |severity, datetime, progname, msg|
    "#{severity[0, 1]} #{datetime.strftime('%Y-%m-%d %H:%M:%S')} -- #{progname}: #{msg}\n"
  end
  log.level = MonoLogger.const_get(level)
  @loggers[stream] = log
end
clear(options) click to toggle source

Currently this will clear the Hive Mind log but do nothing to the local files

# File lib/hive/log.rb, line 85
def clear(options)
  if self.hive_mind
    self.hive_mind.clear_state(component: options[:component], level: options[:level])
  end
end
debug(*args, &block) click to toggle source

These methods were originally created using define_method as they are all the same. However, blocks cannot be used with define_method.

# File lib/hive/log.rb, line 59
def debug(*args, &block)
  write_log('debug', *args, &block)
end
error(*args, &block) click to toggle source
# File lib/hive/log.rb, line 71
def error(*args, &block)
  write_log('error', *args, &block)
end
fatal(*args, &block) click to toggle source
# File lib/hive/log.rb, line 75
def fatal(*args, &block)
  write_log('fatal', *args, &block)
end
info(*args, &block) click to toggle source
# File lib/hive/log.rb, line 63
def info(*args, &block)
  write_log('info', *args, &block)
end
stop_logger(stream) click to toggle source

Stop a log stream:

# Stop the log to standard output
log.stop_logger( STDOUT )
# Stop the log to 'Filename.log'
log.stop_logger( 'Filename.log' )
# File lib/hive/log.rb, line 53
def stop_logger(stream)
  @loggers.delete(stream)
end
unknown(*args, &block) click to toggle source
# File lib/hive/log.rb, line 79
def unknown(*args, &block)
  write_log('unknown', *args, &block)
end
warn(*args, &block) click to toggle source
# File lib/hive/log.rb, line 67
def warn(*args, &block)
  write_log('warn', *args, &block)
end

Private Instance Methods

write_log(level, *args) { |: args| ... } click to toggle source
# File lib/hive/log.rb, line 92
def write_log(level, *args, &block)
  progname = ( block && args.length > 0 ) ? args[0] : @default_progname

  @loggers.each do |_s, l|
    l.send(level, progname) { block ? yield : args[0] }
  end

  if self.hive_mind
    params = {
      state: level,
      component: progname,
      message: block ? yield : args[0]
    }

    self.hive_mind.set_state params
  end
end