logstash-logger 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ require 'logstash-logger'
2
+
3
+ describe LogStashLogger::Device::UDP do
4
+ include_context 'device'
5
+
6
+ it "writes to a UDP socket" do
7
+ expect(udp_device.to_io).to be_a UDPSocket
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require 'logstash-logger'
2
+
3
+ describe LogStashLogger::Device do
4
+ include_context 'device'
5
+
6
+ context "when port is specified" do
7
+ it "defaults type to UDP" do
8
+ expect(device_with_port).to be_a LogStashLogger::Device::UDP
9
+ end
10
+ end
11
+ end
data/spec/logger_spec.rb CHANGED
@@ -1,24 +1,10 @@
1
- require 'spec_helper'
1
+ require 'logstash-logger'
2
2
 
3
3
  describe LogStashLogger do
4
- # The type of socket we're testing
5
- def socket_type
6
- @socket_type ||= (ENV['SOCKET_TYPE'] || 'UDP').to_s.downcase.to_sym
7
- end
8
-
9
- before(:all) { puts "Testing with #{socket_type.to_s.upcase} socket type" }
10
-
11
- let(:host) { '0.0.0.0' }
12
- let(:hostname) { Socket.gethostname }
13
- let(:port) { 5228 }
14
-
15
- # The logstash logger
16
- let(:logger) { LogStashLogger.new(host, port, socket_type) }
17
- # The log device that the logger writes to
18
- let(:logdev) { logger.instance_variable_get(:@logdev) }
19
-
4
+ include_context 'logger'
5
+
20
6
  let! :listener do
21
- case socket_type
7
+ case connection_type
22
8
  when :tcp
23
9
  TCPServer.new(port)
24
10
  when :udp
@@ -39,7 +25,7 @@ describe LogStashLogger do
39
25
 
40
26
  # The raw input received by the logstash listener
41
27
  let :listener_input do
42
- case socket_type
28
+ case connection_type
43
29
  when :tcp then tcp_client.readline
44
30
  when :udp then listener.recvfrom(8192)[0]
45
31
  end
@@ -61,9 +47,9 @@ describe LogStashLogger do
61
47
  # The socket that the logger is writing to
62
48
  #let(:socket) { @socket }
63
49
 
64
- it 'uses a LogStashLogger::Socket as the log device' do
50
+ it 'uses a LogStashLogger::Device as the log device' do
65
51
  expect(logdev).to be_a Logger::LogDevice
66
- expect(logdev.instance_variable_get(:@dev)).to be_a LogStashLogger::Socket
52
+ expect(logdev.instance_variable_get(:@dev)).to be_a LogStashLogger::Device::Base
67
53
  end
68
54
 
69
55
  it 'takes a string message as input and writes a logstash event' do
@@ -71,7 +57,7 @@ describe LogStashLogger do
71
57
 
72
58
  expect(logdev).to receive(:write).and_call_original do |event|
73
59
  expect(event).to be_a LogStash::Event
74
- expect(event.source).to eql(hostname)
60
+ expect(event.host).to eql(hostname)
75
61
  expect(event['message']).to eql(message)
76
62
  expect(event['severity']).to eql('INFO')
77
63
  end
@@ -79,20 +65,20 @@ describe LogStashLogger do
79
65
  logger.info(message)
80
66
 
81
67
  expect(listener_event['message']).to eq(message)
82
- expect(listener_event['source']).to eq(hostname)
68
+ expect(listener_event['host']).to eq(hostname)
83
69
  end
84
70
 
85
71
  it 'takes a logstash-formatted json string as input and writes out a logstash event' do
86
72
  expect(logdev).to receive(:write).and_call_original do |event|
87
73
  expect(event).to be_a LogStash::Event
88
74
  expect(event['message']).to eql(logstash_event['message'])
89
- expect(event.source).to eql(hostname)
75
+ expect(event.host).to eql(hostname)
90
76
  end
91
77
 
92
78
  logger.info(logstash_event.to_json)
93
79
 
94
80
  expect(listener_event['message']).to eq(logstash_event['message'])
95
- expect(listener_event['source']).to eq(hostname)
81
+ expect(listener_event['host']).to eq(hostname)
96
82
  end
97
83
 
98
84
  it 'takes a LogStash::Event as input and writes it out intact' do
@@ -101,14 +87,14 @@ describe LogStashLogger do
101
87
  expect(event['message']).to eql(logstash_event['message'])
102
88
  expect(event['severity']).to eql(logstash_event['severity'])
103
89
  expect(event.timestamp).to eql(logstash_event.timestamp)
104
- expect(event.source).to eql(hostname)
90
+ expect(event.host).to eql(hostname)
105
91
  end
106
92
 
107
93
  logger.warn(logstash_event)
108
94
 
109
95
  expect(listener_event['message']).to eq(logstash_event['message'])
110
96
  expect(listener_event['severity']).to eq(logstash_event['severity'])
111
- expect(listener_event['source']).to eq(hostname)
97
+ expect(listener_event['host']).to eq(hostname)
112
98
  end
113
99
 
114
100
  it 'takes a data hash as input and writes out a logstash event' do
@@ -123,7 +109,7 @@ describe LogStashLogger do
123
109
  expect(event['message']).to eql('test')
124
110
  expect(event['severity']).to eql('INFO')
125
111
  expect(event['foo']).to eql('bar')
126
- expect(event.source).to eql(hostname)
112
+ expect(event.host).to eql(hostname)
127
113
  end
128
114
 
129
115
  logger.info(data.dup)
@@ -131,8 +117,8 @@ describe LogStashLogger do
131
117
  expect(listener_event['message']).to eq(data["message"])
132
118
  expect(listener_event['severity']).to eq(data['severity'])
133
119
  expect(listener_event['foo']).to eq(data['foo'])
134
- expect(listener_event['source']).to eq(hostname)
120
+ expect(listener_event['host']).to eq(hostname)
135
121
  expect(listener_event['@timestamp']).to_not be_nil
136
122
  end
137
123
 
138
- end
124
+ end
@@ -0,0 +1,52 @@
1
+ require 'rails/all'
2
+ require 'logstash-logger'
3
+ require 'logstash-logger/railtie'
4
+
5
+ module Test
6
+ class Application < ::Rails::Application
7
+ config.eager_load = false
8
+
9
+ config.logstash.port = PORT
10
+ config.log_level = :info
11
+ end
12
+ end
13
+
14
+ Test::Application.initialize!
15
+
16
+ describe LogStashLogger do
17
+ describe "Rails integration" do
18
+ let(:app) { Rails.application }
19
+ let(:config) { app.config }
20
+
21
+ describe 'Railtie' do
22
+ describe 'Rails.logger' do
23
+ subject { Rails.logger }
24
+
25
+ it { should be_a LogStashLogger }
26
+
27
+ it "defaults level to config.log_level" do
28
+ expect(subject.level).to eq(::Logger::INFO)
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '#setup' do
34
+ before do
35
+ app.config.logstash.port = PORT
36
+ LogStashLogger.setup(app)
37
+ end
38
+
39
+ context "when logstash is not configured" do
40
+ before do
41
+ app.config.logstash.clear
42
+ app.config.logger = nil
43
+ LogStashLogger.setup(app)
44
+ end
45
+
46
+ it "does not configure anything" do
47
+ expect(app.config.logger).to be_nil
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,41 @@
1
- require 'rubygems'
2
- require 'bundler/setup'
3
-
4
- require 'logstash-logger'
1
+ require 'pry'
5
2
 
6
3
  RSpec.configure do |config|
7
4
  config.order = "random"
8
- end
5
+
6
+ config.before(:suite) do
7
+ puts "Testing with #{CONNECTION_TYPE.to_s.upcase} socket type"
8
+ end
9
+
10
+ config.expect_with :rspec do |c|
11
+ c.syntax = :expect
12
+ end
13
+ end
14
+
15
+ HOST = ENV['HOST'] || '0.0.0.0'
16
+ PORT = ENV.fetch('PORT', 5228).to_i
17
+ CONNECTION_TYPE ||= (ENV['TYPE'] || 'UDP').to_s.downcase.to_sym
18
+
19
+ RSpec.shared_context 'logger' do
20
+ # The type of connection we're testing
21
+ def connection_type
22
+ CONNECTION_TYPE
23
+ end
24
+
25
+ let(:host) { HOST }
26
+ let(:hostname) { Socket.gethostname }
27
+ let(:port) { PORT }
28
+
29
+ # The logstash logger
30
+ let(:logger) { LogStashLogger.new(host: host, port: port, type: connection_type) }
31
+ # The log device that the logger writes to
32
+ let(:logdev) { logger.instance_variable_get(:@logdev) }
33
+ end
34
+
35
+ RSpec.shared_context 'device' do
36
+ let(:port) { PORT }
37
+ let(:device_with_port) { LogStashLogger::Device.new(port: port) }
38
+ let(:udp_device) { LogStashLogger::Device.new(type: :udp, port: port) }
39
+ let(:tcp_device) { LogStashLogger::Device.new(type: :tcp, port: port) }
40
+ let(:ssl_tcp_device) { LogStashLogger::Device.new(type: :tcp, port: port, ssl_enable: true) }
41
+ end
@@ -0,0 +1,32 @@
1
+ require 'logstash-logger'
2
+
3
+ describe LogStashLogger do
4
+ include_context 'logger'
5
+
6
+ describe "tagged logging" do
7
+ let(:message) { 'foo' }
8
+ let(:tag) { 'bar' }
9
+
10
+ it "puts tags into the tags array on the logstash event" do
11
+ expect(logdev).to receive(:write) do |event_string|
12
+ event = JSON.parse(event_string)
13
+ expect(event['tags']).to match_array([tag])
14
+ expect(event['message']).to eq(message)
15
+ end
16
+
17
+ logger.tagged(tag) do
18
+ logger.info(message)
19
+ end
20
+ end
21
+
22
+ it "doesn't put tags on the event when there are no tags" do
23
+ expect(logdev).to receive(:write) do |event_string|
24
+ event = JSON.parse(event_string)
25
+ expect(event['tags']).to be_nil
26
+ expect(event['message']).to eq(message)
27
+ end
28
+
29
+ logger.info(message)
30
+ end
31
+ end
32
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstash-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Butler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-22 00:00:00.000000000 Z
11
+ date: 2014-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logstash-event
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.2'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rspec
28
+ name: rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,20 @@ dependencies:
52
66
  - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: wwtd
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +94,20 @@ dependencies:
66
94
  - - ">="
67
95
  - !ruby/object:Gem::Version
68
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: appraisal
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
69
111
  description: Ruby logger that writes directly to LogStash
70
112
  email:
71
113
  - dwbutler@ucla.edu
@@ -74,19 +116,44 @@ extensions: []
74
116
  extra_rdoc_files: []
75
117
  files:
76
118
  - ".gitignore"
119
+ - ".rspec"
77
120
  - ".travis.yml"
121
+ - Appraisals
78
122
  - CHANGELOG.md
79
123
  - Gemfile
80
124
  - LICENSE.txt
81
125
  - README.md
82
126
  - Rakefile
127
+ - gemfiles/rails_3.2.gemfile
128
+ - gemfiles/rails_4.0.gemfile
129
+ - gemfiles/rails_4.1.gemfile
83
130
  - lib/logstash-logger.rb
131
+ - lib/logstash-logger/device.rb
132
+ - lib/logstash-logger/device/base.rb
133
+ - lib/logstash-logger/device/socket.rb
134
+ - lib/logstash-logger/device/stdout.rb
135
+ - lib/logstash-logger/device/tcp.rb
136
+ - lib/logstash-logger/device/udp.rb
137
+ - lib/logstash-logger/formatter.rb
84
138
  - lib/logstash-logger/logger.rb
85
- - lib/logstash-logger/socket.rb
139
+ - lib/logstash-logger/railtie.rb
140
+ - lib/logstash-logger/tagged_logging.rb
86
141
  - lib/logstash-logger/version.rb
87
142
  - logstash-logger.gemspec
143
+ - samples/example.crt
144
+ - samples/example.key
145
+ - samples/ssl.conf
146
+ - samples/tcp.conf
147
+ - samples/udp.conf
148
+ - spec/device/socket_spec.rb
149
+ - spec/device/stdout_spec.rb
150
+ - spec/device/tcp_spec.rb
151
+ - spec/device/udp_spec.rb
152
+ - spec/device_spec.rb
88
153
  - spec/logger_spec.rb
154
+ - spec/rails_spec.rb
89
155
  - spec/spec_helper.rb
156
+ - spec/tagged_logging_spec.rb
90
157
  homepage: http://github.com/dwbutler/logstash-logger
91
158
  licenses:
92
159
  - MIT
@@ -112,5 +179,12 @@ signing_key:
112
179
  specification_version: 4
113
180
  summary: LogStash Logger for ruby
114
181
  test_files:
182
+ - spec/device/socket_spec.rb
183
+ - spec/device/stdout_spec.rb
184
+ - spec/device/tcp_spec.rb
185
+ - spec/device/udp_spec.rb
186
+ - spec/device_spec.rb
115
187
  - spec/logger_spec.rb
188
+ - spec/rails_spec.rb
116
189
  - spec/spec_helper.rb
190
+ - spec/tagged_logging_spec.rb
@@ -1,34 +0,0 @@
1
- class LogStashLogger::Socket
2
- def initialize(host, port, socket_type = :udp)
3
- @host = host
4
- @port = port
5
- @type = socket_type
6
- @socket = nil
7
- end
8
-
9
- def write(event)
10
- begin
11
- connect unless @socket
12
-
13
- @socket.write("#{event.to_hash.to_json}\n")
14
- rescue => e
15
- warn "#{self.class} - #{e.class} - #{e.message}"
16
- close
17
- @socket = nil
18
- end
19
- end
20
-
21
- def close
22
- @socket && @socket.close
23
- rescue => e
24
- warn "#{self.class} - #{e.class} - #{e.message}"
25
- end
26
-
27
- private
28
- def connect
29
- @socket = case @type
30
- when :udp then UDPSocket.new.tap {|s| s.connect(@host, @port)}
31
- when :tcp then TCPSocket.new(@host, @port)
32
- end
33
- end
34
- end