splunk_logger 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/lib/splunk_logger/client/log.rb +59 -0
- data/lib/splunk_logger/client.rb +94 -0
- data/lib/splunk_logger/version.rb +3 -0
- data/lib/splunk_logger.rb +10 -0
- metadata +88 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 70847608b3d78ba75b5b81d848605163f33e5d18
|
4
|
+
data.tar.gz: 16fdfa884272a5e99f20e233a7b9bb016943e104
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 95df78d83e0d9d126598e0963beca5abab4605f602f1b1b580eac95115a2b8a22ee767882c6c1600b59e542232bc50aaa90a2b06e08dc3c44edd008a72b7c6c2
|
7
|
+
data.tar.gz: 8b96d9739bda7f5f83aac63336a1c88b2174b1a06959ce031cdb802bfb931fdd48aecbde2563723750855a59bbe31174be447a67a2da605ee26103a070beca52
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (C) 2013 Robert Ikeoka <rikeoka@gmail.com>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module SplunkLogger
|
2
|
+
class Client
|
3
|
+
|
4
|
+
# Methods to log events to Splunk
|
5
|
+
module Log
|
6
|
+
|
7
|
+
# Log event with default level
|
8
|
+
#
|
9
|
+
# Example:
|
10
|
+
# SplunkLogger.log("hello world")
|
11
|
+
def log(message)
|
12
|
+
queue_or_send_message(@default_level, message)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Log event as debug
|
16
|
+
#
|
17
|
+
# Example:
|
18
|
+
# SplunkLogger.debug("hello world")
|
19
|
+
def debug(message)
|
20
|
+
queue_or_send_message('debug', message)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Log event as info
|
24
|
+
#
|
25
|
+
# Example:
|
26
|
+
# SplunkLogger.info("hello world")
|
27
|
+
def info(message)
|
28
|
+
queue_or_send_message('info', message)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Log event as warning
|
32
|
+
#
|
33
|
+
# Example:
|
34
|
+
# SplunkLogger.warn("hello world")
|
35
|
+
def warn(message)
|
36
|
+
queue_or_send_message('warn', message)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Log event as error
|
40
|
+
#
|
41
|
+
# Example:
|
42
|
+
# SplunkLogger.error("hello world")
|
43
|
+
def error(message)
|
44
|
+
queue_or_send_message('error', message)
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def queue_or_send_message(level, message)
|
49
|
+
if delayed?
|
50
|
+
@message_queue << {severity: level, message: message}
|
51
|
+
@message_queue.shift if @message_queue.length > @max_queue_size + @max_batch_size
|
52
|
+
return true
|
53
|
+
else
|
54
|
+
send_log_now({severity: level, message: message})
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
require 'splunk_logger/client/log'
|
6
|
+
|
7
|
+
module SplunkLogger
|
8
|
+
class Client
|
9
|
+
include SplunkLogger::Client::Log
|
10
|
+
COLLECTOR_PATH = '/services/collector/event'
|
11
|
+
|
12
|
+
def initialize(options = {})
|
13
|
+
token = options[:token]
|
14
|
+
url = options[:url]
|
15
|
+
verify_ssl = options[:verify_ssl].nil? ? true : options[:verify_ssl]
|
16
|
+
@default_level = options[:default_level].to_s || 'info'
|
17
|
+
@send_interval = options[:send_interval].to_i
|
18
|
+
@max_batch_size = (options[:max_batch_size] || 100).to_i
|
19
|
+
@max_queue_size = (options[:max_queue_size] || 10000).to_i
|
20
|
+
@message_queue = []
|
21
|
+
@current_message_size = 0
|
22
|
+
headers = {'Authorization': "Splunk #{token}", 'Content-Type': 'application/json'}
|
23
|
+
@conn = Faraday.new(url: url, headers: headers) do |faraday|
|
24
|
+
faraday.request :json
|
25
|
+
faraday.response :json, content_type: 'application/json'
|
26
|
+
faraday.response :logger
|
27
|
+
faraday.adapter Faraday.default_adapter
|
28
|
+
end
|
29
|
+
@conn.ssl.verify = verify_ssl
|
30
|
+
start
|
31
|
+
end
|
32
|
+
|
33
|
+
attr_accessor :message_queue
|
34
|
+
|
35
|
+
def start
|
36
|
+
return unless (@timer.nil? && delayed?)
|
37
|
+
@timer = Thread.new do
|
38
|
+
while true do
|
39
|
+
sleep @send_interval
|
40
|
+
send_log
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def stop
|
46
|
+
return if @timer.nil?
|
47
|
+
@timer.kill
|
48
|
+
@timer = nil
|
49
|
+
end
|
50
|
+
|
51
|
+
def delayed?
|
52
|
+
return @send_interval > 0
|
53
|
+
end
|
54
|
+
|
55
|
+
protected
|
56
|
+
|
57
|
+
def send_log
|
58
|
+
return if @message_queue.empty? || @current_message_size > 0
|
59
|
+
|
60
|
+
while(!@message_queue.empty?) do
|
61
|
+
@current_message_size = [@message_queue.length, @max_batch_size].min
|
62
|
+
messages = { "event": @message_queue.slice(0, @current_message_size) }
|
63
|
+
body = messages[:event].map { |m| "#{{event: m}.to_json}" }.join("\n")
|
64
|
+
|
65
|
+
|
66
|
+
begin
|
67
|
+
response = @conn.post SplunkLogger::Client::COLLECTOR_PATH, body
|
68
|
+
if response.body['text'] == 'Success' && response.body['code'] == 0
|
69
|
+
@message_queue.shift(@current_message_size)
|
70
|
+
else
|
71
|
+
@current_message_size = 0
|
72
|
+
return
|
73
|
+
end
|
74
|
+
rescue Faraday::Error => e
|
75
|
+
@current_message_size = 0
|
76
|
+
return
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
@current_message_size = 0
|
81
|
+
end
|
82
|
+
|
83
|
+
def send_log_now(message)
|
84
|
+
body = "#{{event: message}.to_json}"
|
85
|
+
|
86
|
+
begin
|
87
|
+
response = @conn.post SplunkLogger::Client::COLLECTOR_PATH, body
|
88
|
+
return ( response.body['text'] == 'Success' && response.body['code'] == 0 )
|
89
|
+
rescue Faraday::Error => e
|
90
|
+
return false
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: splunk_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Ikeoka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.9'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0.9'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.9'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0.9'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: faraday_middleware
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
description:
|
54
|
+
email: rikeoka@gmail.com
|
55
|
+
executables: []
|
56
|
+
extensions: []
|
57
|
+
extra_rdoc_files: []
|
58
|
+
files:
|
59
|
+
- MIT-LICENSE
|
60
|
+
- lib/splunk_logger.rb
|
61
|
+
- lib/splunk_logger/client.rb
|
62
|
+
- lib/splunk_logger/client/log.rb
|
63
|
+
- lib/splunk_logger/version.rb
|
64
|
+
homepage: https://github.com/rikeoka/splunk_logger
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
metadata: {}
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 2.0.0
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 2.6.4
|
85
|
+
signing_key:
|
86
|
+
specification_version: 4
|
87
|
+
summary: Simple logger to send logs to your Splunk instance
|
88
|
+
test_files: []
|