Class: Syslogstash::LogstashWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/syslogstash/logstash_writer.rb

Overview

Write messages to a logstash server.

Defined Under Namespace

Classes: Target

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cfg, stats) ⇒ LogstashWriter

Create a new logstash writer.

Once the object is created, you’re ready to give it messages by calling #send_entry. No messages will actually be delivered to logstash, though, until you call #run.



17
18
19
20
21
22
# File 'lib/syslogstash/logstash_writer.rb', line 17

def initialize(cfg, stats)
	@server_name, @logger, @backlog, @stats = cfg.logstash_server, cfg.logger, cfg.backlog_size, stats

	@entries = []
	@entries_mutex = Mutex.new
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



9
10
11
# File 'lib/syslogstash/logstash_writer.rb', line 9

def thread
  @thread
end

Instance Method Details

#runObject

Start sending messages to logstash servers. This method will return almost immediately, and actual message sending will occur in a separate thread.



44
45
46
# File 'lib/syslogstash/logstash_writer.rb', line 44

def run
	@thread = Thread.new { send_messages }
end

#send_entry(e) ⇒ Object

Add an entry to the list of messages to be sent to logstash. Actual message delivery will happen in a worker thread that is started with #run.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/syslogstash/logstash_writer.rb', line 28

def send_entry(e)
	@entries_mutex.synchronize do
		@entries << { content: e, arrival_timestamp: Time.now }
		while @entries.length > @backlog
			@entries.shift
			@stats.dropped
		end
	end

	@thread.run if @thread
end