class ThreadedLogger

Constants

LOGLEVELS
VERSION

Public Class Methods

clear(shutdown = false) click to toggle source
# File lib/threadedlogger/core.rb, line 45
def self.clear(shutdown = false)
    if shutdown and ! @@instances[self].nil?
        @@instances[self].shutdown
    end
    @@instances[self] = nil
end
clear_all(shutdown = false) click to toggle source
# File lib/threadedlogger/core.rb, line 52
def self.clear_all(shutdown = false)
    if shutdown and ! @@instances.nil?
        @@instances.each_value do |obj|
            if ! obj.nil?
                obj.shutdown
            end
        end
    end
    @@instances = Hash.new
end
initialized?() click to toggle source
# File lib/threadedlogger/core.rb, line 30
def self.initialized?
    ! @@instances[self].nil?
end
instance(*args) click to toggle source
# File lib/threadedlogger/core.rb, line 34
def self.instance(*args)
    if @@instances[self].nil?
        @@instances[self] = new(*args)
    else
        if ! args.empty?
            raise ArgumentError, "instance for #{self} already constructed"
        end
    end
    return @@instances[self]
end
new(file, rotation = 'daily', level = 'info', formatter = nil) click to toggle source
# File lib/threadedlogger/core.rb, line 63
def initialize(file, rotation = 'daily', level = 'info', formatter = nil)
    if file.nil?
        raise ArgumentError, "log file name is required"
    end

    # create a logger
    @log = Logger.new(file, rotation)

    # set the min threshold
    send(:level=, level)

    # apply a formatter if one was given
    if ! formatter.nil?
        @log.formatter = formatter
    end

    # set up a queue and spawn a thread to do the logging
    @queue = Queue.new
    @shutdown = false
    @t = Thread.new {
        runlogger
    }
end

Public Instance Methods

enqueue(severity, msg=nil) click to toggle source
# File lib/threadedlogger/core.rb, line 108
def enqueue(severity, msg=nil)
    # don't enqueue if we're in shutdown
    if( @shutdown )
        return
    end

    # put the message on the queue
    @queue.push( [severity, msg] )
end
level=(level) click to toggle source
# File lib/threadedlogger/core.rb, line 87
def level=(level)
    if LOGLEVELS.has_key?(level)
        @log.level = LOGLEVELS[level]
    else
        raise ArgumentError, "invalid log level #{level}"
    end
end
shutdown() click to toggle source
# File lib/threadedlogger/core.rb, line 95
def shutdown

    # stops new messages from being enqueued and tells thread
    # to drain what's in the queue
    if ! @shutdown
        @shutdown = true
        @queue.push(nil)
        @t.join
        @t = nil
    end

end

Private Instance Methods

runlogger() click to toggle source
# File lib/threadedlogger/core.rb, line 120
def runlogger
    # do blocking pops off the queue until the shutdown flag is set
    while ! @shutdown
        msg = @queue.pop(false)
        if ! msg.nil?
            @log.add(msg[0], msg[1])
        end
    end

    # pop anything left on the queue off
    while ! @queue.empty?
        msg = @queue.pop(true)
        if ! msg.nil?
            @log.add(msg[0], msg[1])
        end
    end
end