Class: Redwood::Logger

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/sup/logger.rb

Overview

simple centralized logger. outputs to multiple sinks by calling << on them. also keeps a record of all messages, so that adding a new sink will send all previous messages to it by default.

Constant Summary collapse

LEVELS =

in order!

%w(debug info warn error)

Instance Method Summary collapse

Methods included from Singleton

included

Constructor Details

#initialize(level = nil) ⇒ Logger

Returns a new instance of Logger.



15
16
17
18
19
20
21
# File 'lib/sup/logger.rb', line 15

def initialize level=nil
  level ||= ENV["SUP_LOG_LEVEL"] || "info"
  self.level = level
  @mutex = Mutex.new
  @buf = StringIO.new
  @sinks = []
end

Instance Method Details

#add_sink(s, copy_current = true) ⇒ Object



26
27
28
29
30
31
# File 'lib/sup/logger.rb', line 26

def add_sink s, copy_current=true
  @mutex.synchronize do
    @sinks << s
    s << @buf.string if copy_current
  end
end

#clear!Object



35
# File 'lib/sup/logger.rb', line 35

def clear!; @mutex.synchronize { @buf = StringIO.new } end

#force_message(m) ⇒ Object

send a message regardless of the current logging level



46
# File 'lib/sup/logger.rb', line 46

def force_message m; send_message format_message(nil, Time.now, m) end

#levelObject



23
# File 'lib/sup/logger.rb', line 23

def level; LEVELS[@level] end

#level=(level) ⇒ Object



24
# File 'lib/sup/logger.rb', line 24

def level=(level); @level = LEVELS.index(level) || raise(ArgumentError, "invalid log level #{level.inspect}: should be one of #{LEVELS * ', '}"); end

#remove_all_sinks!Object



34
# File 'lib/sup/logger.rb', line 34

def remove_all_sinks!; @mutex.synchronize { @sinks.clear } end

#remove_sink(s) ⇒ Object



33
# File 'lib/sup/logger.rb', line 33

def remove_sink s; @mutex.synchronize { @sinks.delete s } end