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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -3
- data/Appraisals +11 -0
- data/CHANGELOG.md +20 -13
- data/Gemfile +1 -7
- data/README.md +94 -35
- data/Rakefile +3 -5
- data/gemfiles/rails_3.2.gemfile +7 -0
- data/gemfiles/rails_4.0.gemfile +7 -0
- data/gemfiles/rails_4.1.gemfile +7 -0
- data/lib/logstash-logger.rb +4 -3
- data/lib/logstash-logger/device.rb +28 -0
- data/lib/logstash-logger/device/base.rb +30 -0
- data/lib/logstash-logger/device/socket.rb +56 -0
- data/lib/logstash-logger/device/stdout.rb +14 -0
- data/lib/logstash-logger/device/tcp.rb +43 -0
- data/lib/logstash-logger/device/udp.rb +11 -0
- data/lib/logstash-logger/formatter.rb +51 -0
- data/lib/logstash-logger/logger.rb +32 -46
- data/lib/logstash-logger/railtie.rb +23 -0
- data/lib/logstash-logger/tagged_logging.rb +40 -0
- data/lib/logstash-logger/version.rb +2 -4
- data/logstash-logger.gemspec +5 -1
- data/samples/example.crt +16 -0
- data/samples/example.key +15 -0
- data/samples/ssl.conf +15 -0
- data/samples/tcp.conf +11 -0
- data/samples/udp.conf +11 -0
- data/spec/device/socket_spec.rb +15 -0
- data/spec/device/stdout_spec.rb +14 -0
- data/spec/device/tcp_spec.rb +36 -0
- data/spec/device/udp_spec.rb +9 -0
- data/spec/device_spec.rb +11 -0
- data/spec/logger_spec.rb +16 -30
- data/spec/rails_spec.rb +52 -0
- data/spec/spec_helper.rb +38 -5
- data/spec/tagged_logging_spec.rb +32 -0
- metadata +78 -4
- data/lib/logstash-logger/socket.rb +0 -34
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6696816384e341e562b5cf39921a1f8d71accb0
|
4
|
+
data.tar.gz: a510f56283c8a70101dd5604efa2b5470edb5f5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a9b42b29215905770c72c3ad7ada65df908302a24e0389d636e0e7c3dbef631c06dfd57a304180adc5acdf3138ed81bb47ed9e8b465393a7c3130be107390892
|
7
|
+
data.tar.gz: ce2711e17ef4ce503da02e1e9561f55b61525d4766bfefda2ba5a794418e990116351b3c57f25939e0bd9be58242cf3e5b68096b72dfeb5576e3267d890df1cc
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.travis.yml
CHANGED
data/Appraisals
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,24 +1,31 @@
|
|
1
|
-
0.
|
2
|
-
|
1
|
+
## 0.5.0
|
2
|
+
- Support for tagged logging. The interface was extracted from `ActiveSupport::TaggedLogging`
|
3
|
+
and outputs to the `tags` key. (Thanks [pctj101](https://github.com/pctj101)!)
|
4
|
+
- The `(host, port, type)` constructor has been deprecated in favor of an options hash constructor.
|
5
|
+
- Support for using SSL for TCP connections. (Thanks [Gary Rennie](https://github.com/Gazler)!)
|
6
|
+
- Support for configuring logger to write to STDOUT. (Thanks [Nick Ethier](https://github.com/nickethier)!)
|
7
|
+
- Support for Rails configuration.
|
8
|
+
- Fixed output to STDOUT in Rails console (Rails 4+).
|
9
|
+
- `host` is no longer required for TCP/UDP. It will default to `0.0.0.0`, the same default port that logstash listens on.
|
10
|
+
- Changed event key `source` to `host` to match what the latest logstash expects.
|
11
|
+
- Output event timestamp consistently even if `Time#to_json` is overridden.
|
12
|
+
- Major refactoring which will lead the way to support other log types.
|
13
|
+
|
14
|
+
## 0.4.1
|
3
15
|
- Fixed support for `LogStash::Event` v1 format when logging a hash. Extra data
|
4
16
|
now goes to the top level instead of into the `@fields` key.
|
5
17
|
|
6
|
-
0.4.0
|
7
|
-
-----
|
18
|
+
## 0.4.0
|
8
19
|
- Support for new `LogStash::Event` v1 format. v0 is supported in 0.3+.
|
9
20
|
|
10
|
-
0.3.0
|
11
|
-
-----
|
21
|
+
## 0.3.0
|
12
22
|
- Added support for logging to a UDP listener.
|
13
23
|
|
14
|
-
0.2.1
|
15
|
-
-----
|
24
|
+
## 0.2.1
|
16
25
|
- Fixed to use Logstash's default time format for timestamps.
|
17
26
|
|
18
|
-
0.2.0
|
19
|
-
-----
|
27
|
+
## 0.2.0
|
20
28
|
- Better use of Ruby Logger's built-in LogDevice.
|
21
29
|
|
22
|
-
0.1.0
|
23
|
-
|
24
|
-
- Initial release. Support for logging to a TCP listener.
|
30
|
+
## 0.1.0
|
31
|
+
- Initial release. Support for logging to a TCP listener.
|
data/Gemfile
CHANGED
@@ -1,11 +1,5 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
# Specify your gem's dependencies in logstash-logger.gemspec
|
4
3
|
gemspec
|
5
4
|
|
6
|
-
gem 'json', :platform => :ruby_18
|
7
|
-
|
8
|
-
group :development do
|
9
|
-
#gem 'ruby-debug', :platform => :ruby_18
|
10
|
-
#gem 'debugger', :platforms => [:ruby_19, :ruby_20]
|
11
|
-
end
|
5
|
+
#gem 'json', :platform => :ruby_18
|
data/README.md
CHANGED
@@ -36,29 +36,34 @@ The logger accepts a string message, a JSON string, a hash, or a `LogStash::Even
|
|
36
36
|
```ruby
|
37
37
|
require 'logstash-logger'
|
38
38
|
|
39
|
-
# Defaults to UDP
|
40
|
-
logger = LogStashLogger.new(
|
39
|
+
# Defaults to UDP on 0.0.0.0
|
40
|
+
logger = LogStashLogger.new(port: 5228)
|
41
41
|
|
42
|
-
# Specify UDP or TCP explicitly
|
43
|
-
udp_logger = LogStashLogger.new('localhost', 5228, :udp)
|
44
|
-
tcp_logger = LogStashLogger.new('localhost', 5229, :tcp)
|
42
|
+
# Specify host and type (UDP or TCP) explicitly
|
43
|
+
udp_logger = LogStashLogger.new(host: 'localhost', port: 5228, type: :udp)
|
44
|
+
tcp_logger = LogStashLogger.new(host: 'localhost', port: 5229, type: :tcp)
|
45
|
+
stdout_logger = LogStashLogger.new(type: :stdout)
|
45
46
|
|
46
47
|
# The following messages are written to UDP port 5228:
|
47
48
|
|
48
49
|
logger.info 'test'
|
49
|
-
# {"message":"test","@timestamp":"2014-05-22T09:37:19.204-07:00","@version":"1","severity":"INFO","
|
50
|
+
# {"message":"test","@timestamp":"2014-05-22T09:37:19.204-07:00","@version":"1","severity":"INFO","host":"[hostname]"}
|
50
51
|
|
51
52
|
logger.error '{"message": "error"}'
|
52
|
-
# {"message":"error","@timestamp":"2014-05-22T10:10:55.877-07:00","@version":"1","severity":"ERROR","
|
53
|
+
# {"message":"error","@timestamp":"2014-05-22T10:10:55.877-07:00","@version":"1","severity":"ERROR","host":"[hostname]"}
|
53
54
|
|
54
55
|
logger.debug message: 'test', foo: 'bar'
|
55
|
-
# {"message":"test","foo":"bar","@timestamp":"2014-05-22T09:43:24.004-07:00","@version":"1","severity":"DEBUG","
|
56
|
+
# {"message":"test","foo":"bar","@timestamp":"2014-05-22T09:43:24.004-07:00","@version":"1","severity":"DEBUG","host":"[hostname]"}
|
56
57
|
|
57
58
|
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","
|
59
|
+
# {"message":"test","foo":"bar","@timestamp":"2014-05-22T16:44:37.364Z","@version":"1","severity":"WARN","host":"[hostname]"}
|
60
|
+
|
61
|
+
# Tagged logging
|
62
|
+
logger.tagged('foo') { logger.fatal('bar') }
|
63
|
+
# {"message":"bar","@timestamp":"2014-05-26T20:35:14.685-07:00","@version":"1","severity":"FATAL","host":"[hostname]","tags":["foo"]}
|
59
64
|
```
|
60
65
|
|
61
|
-
## Logstash
|
66
|
+
## Logstash Configuration
|
62
67
|
|
63
68
|
In order for Logstash to correctly receive and parse the event, you will need to
|
64
69
|
configure and run a UDP listener that uses the `json_lines` codec:
|
@@ -73,52 +78,106 @@ input {
|
|
73
78
|
}
|
74
79
|
```
|
75
80
|
|
76
|
-
|
81
|
+
See the [samples](https://github.com/dwbutler/logstash-logger/tree/master/samples) directory for more configuration samples.
|
77
82
|
|
78
|
-
|
83
|
+
## UDP vs TCP
|
84
|
+
Should you write to a UDP or TCP listener? It depends on your specific needs, but most applications should use the default (UDP).
|
85
|
+
|
86
|
+
* UDP is faster because it's asynchronous (fire-and-forget). However, this means that log messages could get dropped. This is okay for most applications.
|
87
|
+
* TCP verifies that every message has been received via two-way communication . This could slow your app down to a crawl if the TCP listener is under heavy load.
|
88
|
+
|
89
|
+
For a more detailed discussion of UDP vs TCP, I recommend reading this article: [UDP vs. TCP](http://gafferongames.com/networking-for-game-programmers/udp-vs-tcp/)
|
90
|
+
|
91
|
+
## SSL
|
92
|
+
|
93
|
+
If you are using TCP then there is the option of adding an SSL certificate to the options hash on initialize.
|
79
94
|
|
80
95
|
```ruby
|
81
|
-
|
82
|
-
logger.level = Logger::INFO # default is Logger::DEBUG
|
83
|
-
config.logger = ActiveSupport::TaggedLogging.new(logger)
|
96
|
+
LogStashLogger.new(type: :tcp, port: 5228, ssl_certificate: "/path/to/certificate.crt")
|
84
97
|
```
|
85
98
|
|
86
|
-
|
99
|
+
The SSL certificate and key can be generated using
|
87
100
|
|
88
|
-
|
89
|
-
* [yarder](https://github.com/rurounijones/yarder)
|
101
|
+
openssl req -x509 -batch -nodes -newkey rsa:2048 -keyout logstash.key -out logstash.crt
|
90
102
|
|
91
|
-
|
92
|
-
Future versions of these gems could potentially have deeper integration with LogStashLogger (i.e. by writing LogStash::Event objects).
|
103
|
+
You can also enable SSL without a certificate:
|
93
104
|
|
94
|
-
|
95
|
-
|
105
|
+
```ruby
|
106
|
+
LogStashLogger.new(type: :tcp, port: 5228, ssl_enable: true)
|
107
|
+
```
|
96
108
|
|
97
|
-
|
98
|
-
* TCP verifies that every message has been received via two-way communication . This could slow your app down to a crawl if the TCP listener is under heavy load.
|
109
|
+
The following Logstash configuration is required for SSL:
|
99
110
|
|
100
|
-
|
111
|
+
```ruby
|
112
|
+
input {
|
113
|
+
tcp {
|
114
|
+
host => "0.0.0.0"
|
115
|
+
port => 5228
|
116
|
+
codec => json_lines
|
117
|
+
ssl_enable => true
|
118
|
+
ssl_cert => "/path/to/certificate.crt"
|
119
|
+
ssl_key => "/path/to/key.key"
|
120
|
+
}
|
121
|
+
}
|
122
|
+
```
|
101
123
|
|
102
|
-
##
|
124
|
+
## Rails Integration
|
103
125
|
|
104
|
-
Verified to work with
|
126
|
+
Verified to work with both Rails 3 and 4.
|
127
|
+
|
128
|
+
Add the following to your `config/environments/production.rb`:
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
# Optional, defaults to '0.0.0.0'
|
132
|
+
config.logstash.host = 'localhost'
|
133
|
+
|
134
|
+
# Required for TCP/UDP
|
135
|
+
config.logstash.port = 5228
|
136
|
+
|
137
|
+
# Optional, defaults to :udp. Possible values are :udp, :tcp, or :stdout
|
138
|
+
config.logstash.type = :udp
|
139
|
+
|
140
|
+
# Optional, enables SSL when combined with :tcp type
|
141
|
+
config.logstash.ssl_enable = true
|
105
142
|
|
106
|
-
|
107
|
-
|
108
|
-
|
143
|
+
# Optional, Rails sets the default to :info
|
144
|
+
config.log_level = :debug
|
145
|
+
```
|
146
|
+
|
147
|
+
By default, every Rails log message will be written to logstash in `LogStash::Event` JSON format.
|
148
|
+
|
149
|
+
For minimal, more-structured logstash events, try one of the following gems:
|
150
|
+
|
151
|
+
* [lograge](https://github.com/roidrage/lograge)
|
152
|
+
* [yarder](https://github.com/rurounijones/yarder)
|
109
153
|
|
110
|
-
|
154
|
+
Currently these gems output a JSON string, which LogStashLogger then parses.
|
155
|
+
Future versions of these gems could potentially have deeper integration with LogStashLogger (i.e. by writing `LogStash::Event` objects).
|
156
|
+
|
157
|
+
## Ruby Compatibility
|
158
|
+
|
159
|
+
Verified to work with:
|
160
|
+
|
161
|
+
* MRI Ruby 1.9.3, 2.0+, 2.1+
|
162
|
+
* JRuby 1.7+
|
163
|
+
* Rubinius 2.2+
|
111
164
|
|
112
|
-
|
165
|
+
Ruby 1.8.7 is not supported.
|
113
166
|
|
114
167
|
## Breaking changes
|
115
168
|
|
169
|
+
### Version 0.5+
|
170
|
+
* The `source` event key has been replaced with `host` to better match the latest logstash.
|
171
|
+
* The `(host, port, type)` constructor has been deprecated in favor of an options hash constructor.
|
172
|
+
|
116
173
|
### Version 0.4+
|
117
|
-
|
118
|
-
LogStashLogger version 0.4+. This is not backwards compatible with the old LogStash::Event v1.1.5
|
174
|
+
`LogStash::Event` uses the v1 format starting version 1.2+. If you're using the v1, you'll need to install
|
175
|
+
LogStashLogger version 0.4+. This is not backwards compatible with the old `LogStash::Event` v1.1.5, which uses
|
176
|
+
the v0 format.
|
119
177
|
|
120
178
|
### Version 0.3+
|
121
|
-
Earlier versions of this gem (<= 0.2.1) only implemented a TCP connection.
|
179
|
+
Earlier versions of this gem (<= 0.2.1) only implemented a TCP connection.
|
180
|
+
Newer versions (>= 0.3) also implement UDP, and use that as the new default.
|
122
181
|
Please be aware if you are using the default constructor and still require TCP, you should add an additional argument:
|
123
182
|
|
124
183
|
```ruby
|
data/Rakefile
CHANGED
@@ -1,24 +1,22 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
2
|
require "bundler/gem_tasks"
|
3
|
+
require "bundler/setup"
|
3
4
|
require 'rspec/core/rake_task'
|
4
5
|
|
5
6
|
desc "Run all specs with default options"
|
6
7
|
RSpec::Core::RakeTask.new(:spec) do |t|
|
7
|
-
t.rspec_opts = %w[--color]
|
8
8
|
t.verbose = false
|
9
9
|
end
|
10
10
|
|
11
11
|
desc "Run specs with TCP socket"
|
12
12
|
RSpec::Core::RakeTask.new("spec:tcp") do |t|
|
13
|
-
ENV['
|
14
|
-
t.rspec_opts = %w[--color]
|
13
|
+
ENV['TYPE'] = 'tcp'
|
15
14
|
t.verbose = false
|
16
15
|
end
|
17
16
|
|
18
17
|
desc "Run specs with UDP socket"
|
19
18
|
RSpec::Core::RakeTask.new("spec:udp") do |t|
|
20
|
-
ENV['
|
21
|
-
t.rspec_opts = %w[--color]
|
19
|
+
ENV['TYPE'] = 'udp'
|
22
20
|
t.verbose = false
|
23
21
|
end
|
24
22
|
|
data/lib/logstash-logger.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'logstash-logger/version'
|
2
2
|
|
3
|
-
require 'socket'
|
4
3
|
require 'logstash/event'
|
5
4
|
|
6
|
-
require 'logstash-logger/
|
5
|
+
require 'logstash-logger/device'
|
6
|
+
|
7
7
|
require 'logstash-logger/logger'
|
8
|
-
require 'logstash-logger/
|
8
|
+
require 'logstash-logger/formatter'
|
9
9
|
|
10
|
+
require 'logstash-logger/railtie' if defined? Rails
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'logstash-logger/device/base'
|
2
|
+
|
3
|
+
module LogStashLogger
|
4
|
+
module Device
|
5
|
+
DEFAULT_TYPE = :udp
|
6
|
+
|
7
|
+
autoload :Base, 'logstash-logger/device/base'
|
8
|
+
autoload :Socket, 'logstash-logger/device/socket'
|
9
|
+
autoload :UDP, 'logstash-logger/device/udp'
|
10
|
+
autoload :TCP, 'logstash-logger/device/tcp'
|
11
|
+
autoload :Stdout, 'logstash-logger/device/stdout'
|
12
|
+
|
13
|
+
def self.new(opts)
|
14
|
+
type = opts.delete(:type) || DEFAULT_TYPE
|
15
|
+
|
16
|
+
device_klass_for(type).new(opts)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.device_klass_for(type)
|
20
|
+
case type
|
21
|
+
when :udp then UDP
|
22
|
+
when :tcp then TCP
|
23
|
+
when :stdout then Stdout
|
24
|
+
else fail ArgumentError, 'Invalid type'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module LogStashLogger
|
2
|
+
module Device
|
3
|
+
class Base
|
4
|
+
attr_reader :io
|
5
|
+
|
6
|
+
def initialize(opts={})
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_io
|
10
|
+
@io
|
11
|
+
end
|
12
|
+
|
13
|
+
def write(message)
|
14
|
+
@io.write(message)
|
15
|
+
end
|
16
|
+
|
17
|
+
def flush
|
18
|
+
@io && @io.flush
|
19
|
+
end
|
20
|
+
|
21
|
+
def close
|
22
|
+
@io && @io.close
|
23
|
+
rescue => e
|
24
|
+
warn "#{self.class} - #{e.class} - #{e.message}"
|
25
|
+
ensure
|
26
|
+
@io = nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'socket'
|
2
|
+
|
3
|
+
module LogStashLogger
|
4
|
+
module Device
|
5
|
+
class Socket < Base
|
6
|
+
DEFAULT_HOST = '0.0.0.0'
|
7
|
+
|
8
|
+
attr_reader :host, :port
|
9
|
+
|
10
|
+
def initialize(opts)
|
11
|
+
@port = opts[:port] || fail(ArgumentError, "Port is required")
|
12
|
+
@host = opts[:host] || DEFAULT_HOST
|
13
|
+
end
|
14
|
+
|
15
|
+
def write(message)
|
16
|
+
with_connection do
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def flush
|
22
|
+
return unless connected?
|
23
|
+
with_connection do
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_io
|
29
|
+
with_connection do
|
30
|
+
@io
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def connected?
|
35
|
+
!!@io
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
|
40
|
+
# Implemented by TCP and UDP devices
|
41
|
+
def connect
|
42
|
+
fail NotImplementedError
|
43
|
+
end
|
44
|
+
|
45
|
+
# Ensure the block is executed with a valid connection
|
46
|
+
def with_connection(&block)
|
47
|
+
connect unless @io
|
48
|
+
yield
|
49
|
+
rescue => e
|
50
|
+
warn "#{self.class} - #{e.class} - #{e.message}"
|
51
|
+
close
|
52
|
+
@io = nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|