Class: ThreadedLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/threadedlogger/core.rb,
lib/threadedlogger/version.rb

Constant Summary collapse

LOGLEVELS =
{
    'debug' => Logger::DEBUG,
    'info' => Logger::INFO,
    'warn' => Logger::WARN,
    'error' => Logger::ERROR,
    'fatal' => Logger::FATAL,
}
VERSION =
'1.3.0'
@@instances =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, rotation = 'daily', level = 'info', formatter = nil) ⇒ ThreadedLogger

Returns a new instance of ThreadedLogger.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# 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

Class Method Details

.clear(shutdown = false) ⇒ Object



45
46
47
48
49
50
# 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) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# 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?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/threadedlogger/core.rb', line 30

def self.initialized?
    ! @@instances[self].nil?
end

.instance(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# 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

Instance Method Details

#enqueue(severity, msg = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
# 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) ⇒ Object



87
88
89
90
91
92
93
# 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

#shutdownObject



95
96
97
98
99
100
101
102
103
104
105
106
# 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