Class: ThreadedLogger
- Inherits:
-
Object
- Object
- ThreadedLogger
- 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.2.1'
- @@instances =
nil
Class Method Summary collapse
Instance Method Summary collapse
- #enqueue(severity, msg = nil) ⇒ Object
-
#initialize(file, rotation = 'daily', level = 'info', formatter = nil) ⇒ ThreadedLogger
constructor
A new instance of ThreadedLogger.
- #level=(level) ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize(file, rotation = 'daily', level = 'info', formatter = nil) ⇒ ThreadedLogger
Returns a new instance of ThreadedLogger.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/threadedlogger/core.rb', line 59 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
41 42 43 44 45 46 |
# File 'lib/threadedlogger/core.rb', line 41 def self.clear(shutdown = false) if shutdown and ! @@instances[self].nil? @@instances[self].shutdown end @@instances[self] = nil end |
.clear_all(shutdown = false) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/threadedlogger/core.rb', line 48 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 |
.instance(*args) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/threadedlogger/core.rb', line 30 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
104 105 106 107 108 109 110 111 112 |
# File 'lib/threadedlogger/core.rb', line 104 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
83 84 85 86 87 88 89 |
# File 'lib/threadedlogger/core.rb', line 83 def level=(level) if LOGLEVELS.has_key?(level) @log.level = LOGLEVELS[level] else raise ArgumentError, "invalid log level #{level}" end end |
#shutdown ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/threadedlogger/core.rb', line 91 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 |