logstash-logger 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/.travis.yml +3 -0
- data/CHANGELOG.md +24 -0
- data/README.md +37 -6
- data/lib/logstash-logger/logger.rb +5 -16
- data/lib/logstash-logger/version.rb +1 -1
- data/logstash-logger.gemspec +1 -0
- data/spec/logger_spec.rb +38 -30
- metadata +18 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2cb98c8cb41ee7a5cd49492cb17c1c3c06df5f37
|
4
|
+
data.tar.gz: 2c09577089a1a3f4dcb71ae16a426080d456d2b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb7d7c1d9f6305721b8aea10d33de1c2d37c19b0de495b06d358fc783082ec3b1ab16d0e7b61be6c6ab621c4452b3509dd6798ca08789c38685d93260e8b8861
|
7
|
+
data.tar.gz: 0ec0923505339d1c9d561aa2e52a54dd7a9cb9cc045ffdaf9e8117db15a58b7eb7df1b5c13b1a7b96426743a0a789326940ea8d2891831f94effb8777c1870d6
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
0.4.1
|
2
|
+
-----
|
3
|
+
- Fixed support for `LogStash::Event` v1 format when logging a hash. Extra data
|
4
|
+
now goes to the top level instead of into the `@fields` key.
|
5
|
+
|
6
|
+
0.4.0
|
7
|
+
-----
|
8
|
+
- Support for new `LogStash::Event` v1 format. v0 is supported in 0.3+.
|
9
|
+
|
10
|
+
0.3.0
|
11
|
+
-----
|
12
|
+
- Added support for logging to a UDP listener.
|
13
|
+
|
14
|
+
0.2.1
|
15
|
+
-----
|
16
|
+
- Fixed to use Logstash's default time format for timestamps.
|
17
|
+
|
18
|
+
0.2.0
|
19
|
+
-----
|
20
|
+
- Better use of Ruby Logger's built-in LogDevice.
|
21
|
+
|
22
|
+
0.1.0
|
23
|
+
-----
|
24
|
+
- Initial release. Support for logging to a TCP listener.
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
# LogStashLogger
|
1
|
+
# LogStashLogger
|
2
|
+
[![Build Status](https://travis-ci.org/dwbutler/logstash-logger.png?branch=master)](https://travis-ci.org/dwbutler/logstash-logger) [![Code Climate](https://codeclimate.com/github/dwbutler/logstash-logger.png)](https://codeclimate.com/github/dwbutler/logstash-logger)
|
2
3
|
|
3
4
|
This gem implements a subclass of Ruby's Logger class that logs directly to [logstash](http://logstash.net).
|
4
5
|
It writes to a logstash listener over a UDP (default) or TCP connection, in logstash JSON format. This is an improvement over
|
@@ -28,20 +29,48 @@ Or install it yourself as:
|
|
28
29
|
## Basic Usage
|
29
30
|
|
30
31
|
First set up a logstash agent to receive input over a UDP or TCP port.
|
31
|
-
Then in ruby, create a LogStashLogger that writes to that port.
|
32
|
+
Then in ruby, create a `LogStashLogger` that writes to that port.
|
33
|
+
|
34
|
+
The logger accepts a string message, a JSON string, a hash, or a `LogStash::Event`.
|
32
35
|
|
33
36
|
```ruby
|
34
37
|
require 'logstash-logger'
|
35
38
|
|
36
39
|
# Defaults to UDP
|
37
40
|
logger = LogStashLogger.new('localhost', 5228)
|
38
|
-
logger.info 'test'
|
39
|
-
# Writes the following to UDP port 5228:
|
40
|
-
# {"@source":"server-host-name","@tags":[],"@fields":{"severity":"INFO"},"@message":"test","@timestamp":"2013-04-08T18:56:23.767273+00:00"}
|
41
41
|
|
42
42
|
# Specify UDP or TCP explicitly
|
43
43
|
udp_logger = LogStashLogger.new('localhost', 5228, :udp)
|
44
44
|
tcp_logger = LogStashLogger.new('localhost', 5229, :tcp)
|
45
|
+
|
46
|
+
# The following messages are written to UDP port 5228:
|
47
|
+
|
48
|
+
logger.info 'test'
|
49
|
+
# {"message":"test","@timestamp":"2014-05-22T09:37:19.204-07:00","@version":"1","severity":"INFO","source":"[server-hostname]"}
|
50
|
+
|
51
|
+
logger.error '{"message": "error"}'
|
52
|
+
# {"message":"error","@timestamp":"2014-05-22T10:10:55.877-07:00","@version":"1","severity":"ERROR","source":"[server-hostname]"}
|
53
|
+
|
54
|
+
logger.debug message: 'test', foo: 'bar'
|
55
|
+
# {"message":"test","foo":"bar","@timestamp":"2014-05-22T09:43:24.004-07:00","@version":"1","severity":"DEBUG","source":"[server-hostname]"}
|
56
|
+
|
57
|
+
logger.warn LogStash::Event.new(message: 'test', foo: 'bar')
|
58
|
+
# {"message":"test","foo":"bar","@timestamp":"2014-05-22T16:44:37.364Z","@version":"1","severity":"WARN","source":"[server-hostname]"}
|
59
|
+
```
|
60
|
+
|
61
|
+
## Logstash configuration
|
62
|
+
|
63
|
+
In order for Logstash to correctly receive and parse the event, you will need to
|
64
|
+
configure and run a UDP listener that uses the `json_lines` codec:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
input {
|
68
|
+
udp {
|
69
|
+
host => "0.0.0.0"
|
70
|
+
port => 5228
|
71
|
+
codec => json_lines
|
72
|
+
}
|
73
|
+
}
|
45
74
|
```
|
46
75
|
|
47
76
|
## Rails integration
|
@@ -49,7 +78,9 @@ tcp_logger = LogStashLogger.new('localhost', 5229, :tcp)
|
|
49
78
|
Add the following to your config/environments/production.rb:
|
50
79
|
|
51
80
|
```ruby
|
52
|
-
|
81
|
+
logger = LogStashLogger.new('localhost', 5228)
|
82
|
+
logger.level = Logger::INFO # default is Logger::DEBUG
|
83
|
+
config.logger = ActiveSupport::TaggedLogging.new(logger)
|
53
84
|
```
|
54
85
|
|
55
86
|
To get Rails to nicely output its logs in structured logstash format, try one of the following gems:
|
@@ -1,8 +1,5 @@
|
|
1
1
|
class LogStashLogger < ::Logger
|
2
2
|
|
3
|
-
attr_reader :client
|
4
|
-
|
5
|
-
LOGSTASH_EVENT_FIELDS = %w(@timestamp @tags @type @source @fields message).freeze
|
6
3
|
HOST = ::Socket.gethostname
|
7
4
|
|
8
5
|
def initialize(host, port, socket_type=:udp)
|
@@ -30,7 +27,7 @@ class LogStashLogger < ::Logger
|
|
30
27
|
|
31
28
|
def format_message(severity, time, progname, message)
|
32
29
|
data = message
|
33
|
-
if data.is_a?(String) && data
|
30
|
+
if data.is_a?(String) && data.start_with?('{')
|
34
31
|
data = (JSON.parse(message) rescue nil) || message
|
35
32
|
end
|
36
33
|
|
@@ -38,17 +35,7 @@ class LogStashLogger < ::Logger
|
|
38
35
|
when LogStash::Event
|
39
36
|
data.clone
|
40
37
|
when Hash
|
41
|
-
event_data =
|
42
|
-
"@tags" => [],
|
43
|
-
"@fields" => {},
|
44
|
-
"@timestamp" => time
|
45
|
-
}
|
46
|
-
LOGSTASH_EVENT_FIELDS.each do |field_name|
|
47
|
-
if field_data = data.delete(field_name)
|
48
|
-
event_data[field_name] = field_data
|
49
|
-
end
|
50
|
-
end
|
51
|
-
event_data["@fields"].merge!(data)
|
38
|
+
event_data = data.merge("@timestamp" => time)
|
52
39
|
LogStash::Event.new(event_data)
|
53
40
|
when String
|
54
41
|
LogStash::Event.new("message" => data, "@timestamp" => time)
|
@@ -56,10 +43,12 @@ class LogStashLogger < ::Logger
|
|
56
43
|
|
57
44
|
event['severity'] ||= severity
|
58
45
|
#event.type = progname
|
46
|
+
|
47
|
+
event['source'] ||= HOST
|
59
48
|
if event['source'] == 'unknown'
|
60
49
|
event['source'] = HOST
|
61
50
|
end
|
62
51
|
|
63
52
|
event
|
64
53
|
end
|
65
|
-
end
|
54
|
+
end
|
data/logstash-logger.gemspec
CHANGED
@@ -11,6 +11,7 @@ Gem::Specification.new do |gem|
|
|
11
11
|
gem.description = %q{Ruby logger that writes directly to LogStash}
|
12
12
|
gem.summary = %q{LogStash Logger for ruby}
|
13
13
|
gem.homepage = "http://github.com/dwbutler/logstash-logger"
|
14
|
+
gem.license = "MIT"
|
14
15
|
|
15
16
|
gem.files = `git ls-files`.split($/)
|
16
17
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
data/spec/logger_spec.rb
CHANGED
@@ -62,69 +62,77 @@ describe LogStashLogger do
|
|
62
62
|
#let(:socket) { @socket }
|
63
63
|
|
64
64
|
it 'uses a LogStashLogger::Socket as the log device' do
|
65
|
-
logdev.
|
66
|
-
logdev.instance_variable_get(:@dev).
|
65
|
+
expect(logdev).to be_a Logger::LogDevice
|
66
|
+
expect(logdev.instance_variable_get(:@dev)).to be_a LogStashLogger::Socket
|
67
67
|
end
|
68
68
|
|
69
69
|
it 'takes a string message as input and writes a logstash event' do
|
70
70
|
message = 'test'
|
71
71
|
|
72
|
-
logdev.
|
73
|
-
event.
|
74
|
-
event.source.
|
75
|
-
event['message'].
|
76
|
-
event['severity'].
|
72
|
+
expect(logdev).to receive(:write).and_call_original do |event|
|
73
|
+
expect(event).to be_a LogStash::Event
|
74
|
+
expect(event.source).to eql(hostname)
|
75
|
+
expect(event['message']).to eql(message)
|
76
|
+
expect(event['severity']).to eql('INFO')
|
77
77
|
end
|
78
78
|
|
79
79
|
logger.info(message)
|
80
80
|
|
81
|
-
listener_event['message'].
|
81
|
+
expect(listener_event['message']).to eq(message)
|
82
|
+
expect(listener_event['source']).to eq(hostname)
|
82
83
|
end
|
83
84
|
|
84
85
|
it 'takes a logstash-formatted json string as input and writes out a logstash event' do
|
85
|
-
logdev.
|
86
|
-
event.
|
87
|
-
event['message'].
|
88
|
-
event.source.
|
86
|
+
expect(logdev).to receive(:write).and_call_original do |event|
|
87
|
+
expect(event).to be_a LogStash::Event
|
88
|
+
expect(event['message']).to eql(logstash_event['message'])
|
89
|
+
expect(event.source).to eql(hostname)
|
89
90
|
end
|
90
91
|
|
91
92
|
logger.info(logstash_event.to_json)
|
92
93
|
|
93
|
-
listener_event['message'].
|
94
|
+
expect(listener_event['message']).to eq(logstash_event['message'])
|
95
|
+
expect(listener_event['source']).to eq(hostname)
|
94
96
|
end
|
95
97
|
|
96
98
|
it 'takes a LogStash::Event as input and writes it out intact' do
|
97
|
-
logdev.
|
98
|
-
event.
|
99
|
-
event['message'].
|
100
|
-
event['severity'].
|
101
|
-
event.timestamp.
|
102
|
-
event.source.
|
99
|
+
expect(logdev).to receive(:write).and_call_original do |event|
|
100
|
+
expect(event).to be_a LogStash::Event
|
101
|
+
expect(event['message']).to eql(logstash_event['message'])
|
102
|
+
expect(event['severity']).to eql(logstash_event['severity'])
|
103
|
+
expect(event.timestamp).to eql(logstash_event.timestamp)
|
104
|
+
expect(event.source).to eql(hostname)
|
103
105
|
end
|
104
106
|
|
105
107
|
logger.warn(logstash_event)
|
106
108
|
|
107
|
-
listener_event['message'].
|
108
|
-
listener_event['severity'].
|
109
|
+
expect(listener_event['message']).to eq(logstash_event['message'])
|
110
|
+
expect(listener_event['severity']).to eq(logstash_event['severity'])
|
111
|
+
expect(listener_event['source']).to eq(hostname)
|
109
112
|
end
|
110
113
|
|
111
114
|
it 'takes a data hash as input and writes out a logstash event' do
|
112
115
|
data = {
|
113
|
-
|
114
|
-
'severity' => 'INFO'
|
116
|
+
'message' => 'test',
|
117
|
+
'severity' => 'INFO',
|
118
|
+
'foo' => 'bar'
|
115
119
|
}
|
116
120
|
|
117
|
-
logdev.
|
118
|
-
event.
|
119
|
-
event['message'].
|
120
|
-
event['severity'].
|
121
|
-
event.
|
121
|
+
expect(logdev).to receive(:write).and_call_original do |event|
|
122
|
+
expect(event).to be_a LogStash::Event
|
123
|
+
expect(event['message']).to eql('test')
|
124
|
+
expect(event['severity']).to eql('INFO')
|
125
|
+
expect(event['foo']).to eql('bar')
|
126
|
+
expect(event.source).to eql(hostname)
|
122
127
|
end
|
123
128
|
|
124
129
|
logger.info(data.dup)
|
125
130
|
|
126
|
-
listener_event['message'].
|
127
|
-
listener_event['severity'].
|
131
|
+
expect(listener_event['message']).to eq(data["message"])
|
132
|
+
expect(listener_event['severity']).to eq(data['severity'])
|
133
|
+
expect(listener_event['foo']).to eq(data['foo'])
|
134
|
+
expect(listener_event['source']).to eq(hostname)
|
135
|
+
expect(listener_event['@timestamp']).to_not be_nil
|
128
136
|
end
|
129
137
|
|
130
138
|
end
|
metadata
CHANGED
@@ -1,69 +1,69 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logstash-logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Butler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: logstash-event
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: wwtd
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
description: Ruby logger that writes directly to LogStash
|
@@ -73,8 +73,9 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
-
- .gitignore
|
77
|
-
- .travis.yml
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- CHANGELOG.md
|
78
79
|
- Gemfile
|
79
80
|
- LICENSE.txt
|
80
81
|
- README.md
|
@@ -87,7 +88,8 @@ files:
|
|
87
88
|
- spec/logger_spec.rb
|
88
89
|
- spec/spec_helper.rb
|
89
90
|
homepage: http://github.com/dwbutler/logstash-logger
|
90
|
-
licenses:
|
91
|
+
licenses:
|
92
|
+
- MIT
|
91
93
|
metadata: {}
|
92
94
|
post_install_message:
|
93
95
|
rdoc_options: []
|
@@ -95,17 +97,17 @@ require_paths:
|
|
95
97
|
- lib
|
96
98
|
required_ruby_version: !ruby/object:Gem::Requirement
|
97
99
|
requirements:
|
98
|
-
- -
|
100
|
+
- - ">="
|
99
101
|
- !ruby/object:Gem::Version
|
100
102
|
version: '0'
|
101
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
104
|
requirements:
|
103
|
-
- -
|
105
|
+
- - ">="
|
104
106
|
- !ruby/object:Gem::Version
|
105
107
|
version: '0'
|
106
108
|
requirements: []
|
107
109
|
rubyforge_project:
|
108
|
-
rubygems_version: 2.
|
110
|
+
rubygems_version: 2.2.2
|
109
111
|
signing_key:
|
110
112
|
specification_version: 4
|
111
113
|
summary: LogStash Logger for ruby
|