Class: Vx::Lib::Logger::LogstashDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/vx/lib/logger/logstash_device.rb

Instance Method Summary collapse

Constructor Details

#initializeLogstashDevice

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

#closeObject



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.message}"
    ensure
      @io = nil
    end
  end
end

#connected?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/vx/lib/logger/logstash_device.rb', line 26

def connected?
  !!@io
end

#enabled?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/vx/lib/logger/logstash_device.rb', line 22

def enabled?
  !!uri
end

#flushObject



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_threadObject



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

#uriObject



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

#waitObject



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(message)
  if enabled?
    logger_thread
    @queue.push message
  end
end