dlogger 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +4 -0
- data/Gemfile +11 -2
- data/dlogger.gemspec +0 -4
- data/examples/splunk.rb +27 -0
- data/lib/dlogger/logger.rb +27 -26
- data/lib/dlogger/outputs/em/splunkstorm.rb +48 -0
- data/lib/dlogger/version.rb +1 -1
- data/specs/common.rb +23 -38
- data/specs/lib/outputs/em/logstash_spec.rb +34 -0
- data/specs/lib/outputs/em/splunkstorm_spec.rb +43 -0
- metadata +11 -39
data/ChangeLog
ADDED
data/Gemfile
CHANGED
@@ -3,11 +3,17 @@ source 'https://rubygems.org'
|
|
3
3
|
# Specify your gem's dependencies in dlogger.gemspec
|
4
4
|
gemspec
|
5
5
|
|
6
|
-
gem 'eventmachine', '~> 1.0.0.
|
6
|
+
gem 'eventmachine', '~> 1.0.0.rc.1'
|
7
|
+
gem 'em-http-request'
|
7
8
|
gem 'yajl-ruby'
|
8
9
|
|
9
10
|
group(:test) do
|
10
|
-
gem '
|
11
|
+
gem 'rake'
|
12
|
+
|
13
|
+
gem 'thin'
|
14
|
+
gem 'em-synchrony'
|
15
|
+
|
16
|
+
gem 'schmurfy-bacon', '~> 1.4.1'
|
11
17
|
gem 'mocha', '~> 0.10.0'
|
12
18
|
gem 'simplecov'
|
13
19
|
gem 'schmurfy-em-spec'
|
@@ -15,5 +21,8 @@ group(:test) do
|
|
15
21
|
gem 'guard-bacon'
|
16
22
|
gem 'rb-fsevent'
|
17
23
|
gem 'growl'
|
24
|
+
|
25
|
+
gem 'tilt'
|
26
|
+
gem 'guard-tilt'
|
18
27
|
end
|
19
28
|
|
data/dlogger.gemspec
CHANGED
@@ -14,8 +14,4 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.name = "dlogger"
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = Dlogger::VERSION
|
17
|
-
|
18
|
-
gem.add_development_dependency('rake')
|
19
|
-
gem.add_development_dependency('tilt')
|
20
|
-
gem.add_development_dependency('guard-tilt')
|
21
17
|
end
|
data/examples/splunk.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'eventmachine'
|
3
|
+
require 'em-http-request'
|
4
|
+
require 'yajl'
|
5
|
+
|
6
|
+
require 'bundler/setup'
|
7
|
+
|
8
|
+
require 'dlogger'
|
9
|
+
require 'dlogger/outputs/em/splunkstorm'
|
10
|
+
|
11
|
+
logger = DLogger::Logger.new
|
12
|
+
logger.add_output( DLogger::Output::Stdout.new )
|
13
|
+
|
14
|
+
EM::run do
|
15
|
+
|
16
|
+
logger.add_output( DLogger::Output::SplunkStorm.new('http://api.splunkstorm.com',
|
17
|
+
:host => 'test.local',
|
18
|
+
:project => 'xxx',
|
19
|
+
:access_token => 'yyy'
|
20
|
+
))
|
21
|
+
|
22
|
+
n = 0
|
23
|
+
EM::add_periodic_timer(2) do
|
24
|
+
logger.log("yeah it worked", :n => n+= 1)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
data/lib/dlogger/logger.rb
CHANGED
@@ -4,7 +4,6 @@ module DLogger
|
|
4
4
|
class Logger
|
5
5
|
def initialize(name = "_default")
|
6
6
|
@name = name
|
7
|
-
@mutex = Mutex.new
|
8
7
|
@outputs = []
|
9
8
|
|
10
9
|
# initialize context
|
@@ -19,38 +18,40 @@ module DLogger
|
|
19
18
|
# @param [Hash] metadata Additional data
|
20
19
|
#
|
21
20
|
def log(msg, metadata = {})
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
ctx_data.
|
35
|
-
merged_metadata[attr_name] = ctx_data.send(attr_name)
|
36
|
-
end
|
37
|
-
|
21
|
+
# clearing a small hash is slightly faster than creating a new
|
22
|
+
# one each time.
|
23
|
+
merged_metadata.clear
|
24
|
+
|
25
|
+
# first load context data
|
26
|
+
context.each do |ctx_data|
|
27
|
+
case ctx_data
|
28
|
+
when Hash
|
29
|
+
merged_metadata.merge!(ctx_data)
|
30
|
+
|
31
|
+
when Extension
|
32
|
+
ctx_data.class.properties.each do |attr_name|
|
33
|
+
merged_metadata[attr_name] = ctx_data.send(attr_name)
|
38
34
|
end
|
39
|
-
end
|
40
|
-
|
41
|
-
# then add our data
|
42
|
-
merged_metadata.merge!(metadata)
|
43
35
|
|
44
|
-
|
45
|
-
dispatch(msg, merged_metadata)
|
36
|
+
end
|
46
37
|
end
|
38
|
+
|
39
|
+
# then add our data
|
40
|
+
merged_metadata.merge!(metadata)
|
41
|
+
|
42
|
+
# and dispatch the result
|
43
|
+
dispatch(msg, merged_metadata)
|
47
44
|
end
|
48
45
|
|
49
46
|
# Helper methods to mimic the standard ruby logger interface.
|
50
47
|
%w(debug info error warn).each do |level|
|
51
|
-
|
52
|
-
|
53
|
-
|
48
|
+
class_eval %{
|
49
|
+
def #{level}(msg, metadata = {})
|
50
|
+
# metadata << [:severity, :#{level}]
|
51
|
+
metadata[:severity] = :#{level}
|
52
|
+
log(msg, metadata)
|
53
|
+
end
|
54
|
+
}
|
54
55
|
end
|
55
56
|
|
56
57
|
##
|
@@ -0,0 +1,48 @@
|
|
1
|
+
gem 'eventmachine'; require 'eventmachine'
|
2
|
+
gem 'em-http-request'; require 'em-http-request'
|
3
|
+
gem 'yajl-ruby'; require 'yajl'
|
4
|
+
|
5
|
+
#
|
6
|
+
module DLogger
|
7
|
+
module Output
|
8
|
+
|
9
|
+
class SplunkStorm < Base
|
10
|
+
API_VERSION = 1
|
11
|
+
ENDPOINT = 'inputs/http'
|
12
|
+
|
13
|
+
def initialize(url, opts = {})
|
14
|
+
@url = "#{url}/#{API_VERSION}/#{ENDPOINT}"
|
15
|
+
|
16
|
+
@access_token = opts.delete(:access_token)
|
17
|
+
|
18
|
+
@event_params = {
|
19
|
+
:sourcetype => 'json_auto_timestamp',
|
20
|
+
:host => opts.delete(:host),
|
21
|
+
:project => opts.delete(:project)
|
22
|
+
}
|
23
|
+
|
24
|
+
raise ":access_token required" unless @access_token
|
25
|
+
raise ":host required" unless @event_params[:host]
|
26
|
+
raise ":project required" unless @event_params[:project]
|
27
|
+
|
28
|
+
raise "unknown options: #{opts.inspect}" unless opts.empty?
|
29
|
+
end
|
30
|
+
|
31
|
+
def dispatch(msg, metadata)
|
32
|
+
metadata = metadata.merge(:message => msg)
|
33
|
+
http = EM::HttpRequest.new(@url).post(
|
34
|
+
:query => @event_params,
|
35
|
+
:head => {'authorization' => [@access_token, 'x']},
|
36
|
+
:body => Yajl::Encoder.encode(metadata)
|
37
|
+
)
|
38
|
+
|
39
|
+
# http.errback do
|
40
|
+
# p "ERR: #{http.response_header}"
|
41
|
+
# end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
data/lib/dlogger/version.rb
CHANGED
data/specs/common.rb
CHANGED
@@ -2,51 +2,36 @@
|
|
2
2
|
require 'rubygems'
|
3
3
|
require 'bundler/setup'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
require 'bacon'
|
6
|
+
|
7
|
+
if ENV['COVERAGE']
|
8
|
+
Bacon.allow_focused_run = false
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
require 'simplecov'
|
11
|
+
SimpleCov.start do
|
12
|
+
add_filter ".*_spec"
|
13
|
+
add_filter "/helpers/"
|
12
14
|
end
|
15
|
+
|
13
16
|
end
|
14
17
|
|
15
|
-
|
16
|
-
require 'mocha'
|
17
|
-
|
18
|
-
$LOAD_PATH.unshift( File.expand_path('../../lib', __FILE__) )
|
18
|
+
$LOAD_PATH.unshift( File.expand_path('../../lib' , __FILE__) )
|
19
19
|
require 'dlogger'
|
20
20
|
|
21
|
+
require 'bacon/ext/mocha'
|
22
|
+
require 'bacon/ext/em'
|
23
|
+
require 'bacon/ext/http'
|
24
|
+
# require 'time_units'
|
25
|
+
# Thread.abort_on_exception = true
|
21
26
|
|
22
|
-
Bacon.
|
27
|
+
Bacon.summary_on_exit()
|
23
28
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
class Context
|
32
|
-
include Mocha::API
|
33
|
-
|
34
|
-
alias_method :it_before_mocha, :it
|
35
|
-
|
36
|
-
def it(description)
|
37
|
-
it_before_mocha(description) do
|
38
|
-
# TODO: find better than that...
|
39
|
-
1.should == 1
|
40
|
-
begin
|
41
|
-
mocha_setup
|
42
|
-
yield
|
43
|
-
mocha_verify(MochaRequirementsCounter)
|
44
|
-
rescue Mocha::ExpectationError => e
|
45
|
-
raise Error.new(:failed, "#{e.message}\n#{e.backtrace[0...10].join("\n")}")
|
46
|
-
ensure
|
47
|
-
mocha_teardown
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
29
|
+
|
30
|
+
def freeze_time(t = Time.now)
|
31
|
+
Time.stubs(:now).returns(t)
|
32
|
+
if block_given?
|
33
|
+
yield
|
34
|
+
Time.unstub(:now)
|
51
35
|
end
|
52
36
|
end
|
37
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative "../../../common"
|
2
|
+
|
3
|
+
require 'dlogger/outputs/em/logstash'
|
4
|
+
|
5
|
+
$n = 0
|
6
|
+
|
7
|
+
describe "LogStash Output" do
|
8
|
+
before do
|
9
|
+
@port = 4000 + ($n += 1)
|
10
|
+
@received_data = ""
|
11
|
+
|
12
|
+
tmp = @received_data
|
13
|
+
|
14
|
+
@handler_class = Class.new(EM::Connection) do
|
15
|
+
define_method(:receive_data) do |data|
|
16
|
+
tmp << data
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
@server = EM::start_server('127.0.0.1', @port, @handler_class)
|
22
|
+
|
23
|
+
@logger = DLogger::Output::LogStash.new('127.0.0.1', @port)
|
24
|
+
end
|
25
|
+
|
26
|
+
should 'send message to logstash' do
|
27
|
+
@logger.dispatch('a log message', {:data => "something", :n => 21})
|
28
|
+
|
29
|
+
wait(0.1) do
|
30
|
+
@received_data.should == "{\"data\":\"something\",\"n\":21,\"message\":\"a log message\"}\n"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require_relative "../../../common"
|
2
|
+
|
3
|
+
require 'dlogger/outputs/em/splunkstorm'
|
4
|
+
|
5
|
+
$n = 0
|
6
|
+
|
7
|
+
describe "SplunkStorm Output" do
|
8
|
+
before do
|
9
|
+
@http_port = 4000 + ($n += 1)
|
10
|
+
|
11
|
+
@request_uri = ""
|
12
|
+
@received_data = ""
|
13
|
+
|
14
|
+
received_data = @received_data
|
15
|
+
request_uri = @request_uri
|
16
|
+
|
17
|
+
start_server do |rack|
|
18
|
+
run ->(env){
|
19
|
+
request_uri << env['REQUEST_URI']
|
20
|
+
received_data << env['rack.input'].read()
|
21
|
+
[200, {}, []]
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
# 1/inputs/http
|
27
|
+
@logger = DLogger::Output::SplunkStorm.new("http://127.0.0.1:#{@http_port}",
|
28
|
+
:host => 'test.com',
|
29
|
+
:access_token => "TOKEN",
|
30
|
+
:project => "PR"
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
should 'send message to logstash' do
|
35
|
+
@logger.dispatch('a log message', {:data => "something", :n => 21})
|
36
|
+
|
37
|
+
wait(0.1) do
|
38
|
+
@request_uri.should == "/1/inputs/http?sourcetype=json_auto_timestamp&host=test.com&project=PR"
|
39
|
+
@received_data.should == "{\"data\":\"something\",\"n\":21,\"message\":\"a log message\"}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dlogger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,41 +9,8 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
13
|
-
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: rake
|
16
|
-
requirement: &70168315282880 !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :development
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: *70168315282880
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: tilt
|
27
|
-
requirement: &70168315298480 !ruby/object:Gem::Requirement
|
28
|
-
none: false
|
29
|
-
requirements:
|
30
|
-
- - ! '>='
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
version: '0'
|
33
|
-
type: :development
|
34
|
-
prerelease: false
|
35
|
-
version_requirements: *70168315298480
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: guard-tilt
|
38
|
-
requirement: &70168315297920 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
|
-
requirements:
|
41
|
-
- - ! '>='
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: '0'
|
44
|
-
type: :development
|
45
|
-
prerelease: false
|
46
|
-
version_requirements: *70168315297920
|
12
|
+
date: 2012-06-25 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
47
14
|
description: Advanced logger allowing you to include metadata with your messages
|
48
15
|
email:
|
49
16
|
- schmurfy@gmail.com
|
@@ -52,6 +19,7 @@ extensions: []
|
|
52
19
|
extra_rdoc_files: []
|
53
20
|
files:
|
54
21
|
- .gitignore
|
22
|
+
- ChangeLog
|
55
23
|
- Gemfile
|
56
24
|
- Guardfile
|
57
25
|
- LICENSE
|
@@ -64,18 +32,22 @@ files:
|
|
64
32
|
- examples/hash_context.rb
|
65
33
|
- examples/logstash.rb
|
66
34
|
- examples/nested_context.rb
|
35
|
+
- examples/splunk.rb
|
67
36
|
- examples/threads.rb
|
68
37
|
- lib/dlogger.rb
|
69
38
|
- lib/dlogger/extension.rb
|
70
39
|
- lib/dlogger/logger.rb
|
71
40
|
- lib/dlogger/outputs/base.rb
|
72
41
|
- lib/dlogger/outputs/em/logstash.rb
|
42
|
+
- lib/dlogger/outputs/em/splunkstorm.rb
|
73
43
|
- lib/dlogger/outputs/stdlib_logger.rb
|
74
44
|
- lib/dlogger/outputs/stdout.rb
|
75
45
|
- lib/dlogger/version.rb
|
76
46
|
- specs/common.rb
|
77
47
|
- specs/lib/extension_spec.rb
|
78
48
|
- specs/lib/logger_spec.rb
|
49
|
+
- specs/lib/outputs/em/logstash_spec.rb
|
50
|
+
- specs/lib/outputs/em/splunkstorm_spec.rb
|
79
51
|
- specs/lib/outputs/stdlib_logger_spec.rb
|
80
52
|
- specs/lib/outputs/stdout_spec.rb
|
81
53
|
homepage: ''
|
@@ -92,7 +64,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
64
|
version: '0'
|
93
65
|
segments:
|
94
66
|
- 0
|
95
|
-
hash:
|
67
|
+
hash: -981071182900887075
|
96
68
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
69
|
none: false
|
98
70
|
requirements:
|
@@ -101,10 +73,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
73
|
version: '0'
|
102
74
|
segments:
|
103
75
|
- 0
|
104
|
-
hash:
|
76
|
+
hash: -981071182900887075
|
105
77
|
requirements: []
|
106
78
|
rubyforge_project:
|
107
|
-
rubygems_version: 1.8.
|
79
|
+
rubygems_version: 1.8.24
|
108
80
|
signing_key:
|
109
81
|
specification_version: 3
|
110
82
|
summary: ! 'Dynamic logger: add a context to your log messages'
|