Class: SplunkLogger::Client

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/splunk_logger/client.rb,
lib/splunk_logger/client/log.rb

Defined Under Namespace

Modules: Log

Constant Summary collapse

COLLECTOR_PATH =
'/services/collector/event'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

#debug, #error, #info, #log, #warn

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/splunk_logger/client.rb', line 12

def initialize(options = {})
  token = options[:token]
  url = options[:url]
  verify_ssl = options[:verify_ssl].nil? ? true : options[:verify_ssl]
  @default_level = options[:default_level].to_s || 'info'
  @send_interval = options[:send_interval].to_i
  @max_batch_size = (options[:max_batch_size] || 100).to_i
  @max_queue_size = (options[:max_queue_size] || 10000).to_i
  @message_queue = []
  @current_message_size = 0
  headers = {'Authorization': "Splunk #{token}", 'Content-Type': 'application/json'}
  @conn = Faraday.new(url: url, headers: headers) do |faraday|
    faraday.request :json
    faraday.response :json, content_type: 'application/json'
    faraday.adapter  Faraday.default_adapter
  end
  @conn.ssl.verify = verify_ssl
  @semaphore = Mutex.new
  start
end

Instance Attribute Details

#message_queueObject

Returns the value of attribute message_queue.



33
34
35
# File 'lib/splunk_logger/client.rb', line 33

def message_queue
  @message_queue
end

Instance Method Details

#delayed?Boolean

Returns:



53
54
55
# File 'lib/splunk_logger/client.rb', line 53

def delayed?
  return @send_interval > 0
end

#startObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/splunk_logger/client.rb', line 35

def start
  return unless (@timer.nil? && delayed?)
  @timer = Thread.new do
    while true do
      sleep @send_interval
      @semaphore.synchronize do
        send_log
      end
    end
  end
end

#stopObject



47
48
49
50
51
# File 'lib/splunk_logger/client.rb', line 47

def stop
  return if @timer.nil?
  @timer.kill
  @timer = nil
end