Class: Vx::Lib::Logger::LogstashDevice
- Inherits:
-
Object
- Object
- Vx::Lib::Logger::LogstashDevice
- Defined in:
- lib/vx/lib/logger/logstash_device.rb
Instance Method Summary collapse
- #close ⇒ Object
- #connected? ⇒ Boolean
- #enabled? ⇒ Boolean
- #flush ⇒ Object
-
#initialize ⇒ LogstashDevice
constructor
A new instance of LogstashDevice.
- #logger_thread ⇒ Object
- #uri ⇒ Object
- #wait ⇒ Object
- #write(message) ⇒ Object
Constructor Details
#initialize ⇒ LogstashDevice
Returns a new instance of LogstashDevice.
9 10 11 12 |
# File 'lib/vx/lib/logger/logstash_device.rb', line 9 def initialize @mutex = Mutex.new @queue = Queue.new end |
Instance Method Details
#close ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/vx/lib/logger/logstash_device.rb', line 30 def close @mutex.synchronize do begin @io && @io.close rescue Exception => e warn "#{self.class} - #{e.class} - #{e.}" ensure @io = nil end end end |
#connected? ⇒ Boolean
26 27 28 |
# File 'lib/vx/lib/logger/logstash_device.rb', line 26 def connected? !!@io end |
#enabled? ⇒ Boolean
22 23 24 |
# File 'lib/vx/lib/logger/logstash_device.rb', line 22 def enabled? !!uri end |
#flush ⇒ Object
49 50 51 52 53 |
# File 'lib/vx/lib/logger/logstash_device.rb', line 49 def flush @mutex.synchronize do @io && @io.flush end end |
#logger_thread ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/vx/lib/logger/logstash_device.rb', line 61 def logger_thread @logger_loop ||= Thread.new do loop do m = @queue.pop with_connection do @io.write m end end end end |
#uri ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/vx/lib/logger/logstash_device.rb', line 14 def uri @uri ||= begin h = ENV['LOGSTASH_HOST'] URI("logstash://#{h}") if h end end |
#wait ⇒ Object
55 56 57 58 59 |
# File 'lib/vx/lib/logger/logstash_device.rb', line 55 def wait while !@queue.empty? sleep 0.1 end end |
#write(message) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/vx/lib/logger/logstash_device.rb', line 42 def write() if enabled? logger_thread @queue.push end end |