Class: Innate::LogHub

Inherits:
Object
  • Object
show all
Includes:
Optioned, Logger::Severity
Defined in:
lib/innate/log/hub.rb

Overview

Innate only provides logging via stdlib Logger to avoid bloat and dependencies, you may specify multiple loggers in the Log instance of LogHub to accomendate your needs, by default we log to $stderr to be compatible with CGI.

Please read the documentation of logger.rb (or even better, its source) to get a feeling of how to use it correctly within Innate

A few shortcuts:

  1. Create logger for stderr/stdout

    logger = Logger.new($stdout)
    logger = Logger.new($stderr)
    
  2. Create logger for a file

    logger = Logger.new('test.log')
    
  3. Create logger for file object

    file = File.open('test.log', 'a+')
    logger = Logger.new(file)
    
  4. Create logger with rotation on specified file size

    # 10 files history, 5 MB each
    logger = Logger.new('test.log', 10, (5 << 20))
    
    # 100 files history, 1 MB each
    logger = Logger.new('test.log', 100, (1 << 20))
    
  5. Create a logger which ages logfiles daily/weekly/monthly

    logger = Logger.new('test.log', 'daily')
    logger = Logger.new('test.log', 'weekly')
    logger = Logger.new('test.log', 'monthly')
    

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Optioned

included

Constructor Details

#initialize(*loggers) ⇒ LogHub

loggers should be a list of Logger instances



46
47
48
49
50
51
# File 'lib/innate/log/hub.rb', line 46

def initialize(*loggers)
  @loggers = loggers.flatten
  @program = nil
  @active = true
  self.level = DEBUG
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/innate/log/hub.rb', line 62

def method_missing(meth, *args, &block)
  eval %~
    def #{meth}(*args, &block)
      return unless @active
      args.each{|arg| @loggers.each{|logger| logger.#{meth}(arg, &block) }}
    end
  ~

  send(meth, *args, &block)
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



43
44
45
# File 'lib/innate/log/hub.rb', line 43

def active
  @active
end

#loggersObject

Returns the value of attribute loggers.



43
44
45
# File 'lib/innate/log/hub.rb', line 43

def loggers
  @loggers
end

#programObject

Returns the value of attribute program.



43
44
45
# File 'lib/innate/log/hub.rb', line 43

def program
  @program
end

Instance Method Details

#level=(lvl) ⇒ Object

set level for all loggers



54
55
56
57
# File 'lib/innate/log/hub.rb', line 54

def level=(lvl)
  @loggers.each{|l| l.level = lvl }
  @level = lvl
end

#startObject



59
# File 'lib/innate/log/hub.rb', line 59

def start; @active = true;  end

#stopObject



60
# File 'lib/innate/log/hub.rb', line 60

def stop;  @active = false; end

#write(*args) ⇒ Object



73
74
75
# File 'lib/innate/log/hub.rb', line 73

def write(*args)
  self.<<(*args)
end