logstash-logger 0.26.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/tests.yml +51 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +85 -105
- data/Appraisals +6 -18
- data/CHANGELOG.md +28 -0
- data/Gemfile +0 -1
- data/README.md +12 -833
- data/docs/buffering.md +70 -0
- data/docs/customization.md +86 -0
- data/docs/outputs.md +42 -0
- data/docs/rails.md +344 -0
- data/docs/ssl.md +90 -0
- data/docs/troubleshooting.md +84 -0
- data/docs/usage.md +148 -0
- data/gemfiles/{rails_4.2.gemfile → rails_7.2.gemfile} +1 -2
- data/gemfiles/{rails_5.0.gemfile → rails_8.0.gemfile} +1 -2
- data/gemfiles/{rails_4.0.gemfile → rails_8.1.gemfile} +1 -2
- data/lib/logstash-logger/buffer.rb +0 -1
- data/lib/logstash-logger/configuration.rb +1 -2
- data/lib/logstash-logger/device/aws_stream.rb +1 -1
- data/lib/logstash-logger/device/base.rb +2 -2
- data/lib/logstash-logger/device/connectable.rb +3 -11
- data/lib/logstash-logger/device/file.rb +21 -4
- data/lib/logstash-logger/device/http.rb +33 -0
- data/lib/logstash-logger/device/kafka.rb +153 -36
- data/lib/logstash-logger/device/redis.rb +8 -1
- data/lib/logstash-logger/device/tcp.rb +1 -5
- data/lib/logstash-logger/device.rb +24 -2
- data/lib/logstash-logger/formatter/base.rb +54 -9
- data/lib/logstash-logger/formatter/cee_syslog.rb +1 -1
- data/lib/logstash-logger/formatter/json.rb +13 -0
- data/lib/logstash-logger/formatter/json_lines.rb +13 -0
- data/lib/logstash-logger/formatter.rb +14 -6
- data/lib/logstash-logger/logger.rb +6 -19
- data/lib/logstash-logger/multi_logger.rb +2 -1
- data/lib/logstash-logger/railtie.rb +1 -1
- data/lib/logstash-logger/tagged_logging.rb +3 -1
- data/lib/logstash-logger/version.rb +1 -1
- data/logstash-logger.gemspec +9 -1
- data/spec/device/file_spec.rb +65 -0
- data/spec/device/http_spec.rb +11 -0
- data/spec/device/kafka_spec.rb +337 -14
- data/spec/device_spec.rb +13 -0
- data/spec/formatter/base_spec.rb +46 -1
- data/spec/formatter/cee_syslog_spec.rb +3 -3
- data/spec/formatter/json_lines_spec.rb +23 -0
- data/spec/formatter/json_spec.rb +49 -0
- data/spec/formatter_spec.rb +19 -2
- data/spec/logger_spec.rb +5 -5
- data/spec/multi_logger_spec.rb +16 -0
- data/spec/spec_helper.rb +2 -5
- data/spec/tagged_logging_spec.rb +15 -0
- metadata +89 -16
- data/.travis.yml +0 -26
- data/gemfiles/rails_3.2.gemfile +0 -9
- data/gemfiles/rails_4.1.gemfile +0 -9
- data/gemfiles/rails_5.1.gemfile +0 -9
data/docs/buffering.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Buffering and Automatic Retries
|
|
2
|
+
|
|
3
|
+
For devices that establish a connection to a remote service, log messages are buffered internally
|
|
4
|
+
and flushed in a background thread. If there is a connection problem, the
|
|
5
|
+
messages are held in the buffer and automatically resent until it is successful.
|
|
6
|
+
Outputs that support batch writing (Redis and Kafka) will write log messages in bulk from the
|
|
7
|
+
buffer. This functionality is implemented using a fork of
|
|
8
|
+
[Stud::Buffer](https://github.com/jordansissel/ruby-stud/blob/master/lib/stud/buffer.rb).
|
|
9
|
+
|
|
10
|
+
## Configuration Options
|
|
11
|
+
|
|
12
|
+
You can configure buffering behavior by passing the following options to LogStashLogger:
|
|
13
|
+
|
|
14
|
+
* `:buffer_max_items` - Max number of items to buffer before flushing. Defaults to 50.
|
|
15
|
+
* `:buffer_max_interval` - Max number of seconds to wait between flushes. Defaults to 5.
|
|
16
|
+
* `:drop_messages_on_flush_error` - Drop messages when there is a flush error. Defaults to false.
|
|
17
|
+
* `:drop_messages_on_full_buffer` - Drop messages when the buffer is full. Defaults to true.
|
|
18
|
+
* `:sync` - Flush buffer every time a message is received (blocking). Defaults to false.
|
|
19
|
+
* `:buffer_flush_at_exit` - Flush messages when exiting the program. Defaults to true.
|
|
20
|
+
* `:buffer_logger` - Logger to write buffer debug/error messages to. Defaults to none.
|
|
21
|
+
|
|
22
|
+
You can turn buffering off by setting `sync = true`.
|
|
23
|
+
|
|
24
|
+
## Caveats
|
|
25
|
+
|
|
26
|
+
Please be aware of the following caveats to this behavior:
|
|
27
|
+
|
|
28
|
+
* It's possible for duplicate log messages to be sent when retrying. For outputs like Redis and
|
|
29
|
+
Kafka that write in batches, the whole batch could get re-sent. If this is a problem, you
|
|
30
|
+
can add a UUID field to each event to uniquely identify it. You can either do this
|
|
31
|
+
in a `customize_event` block, or by using logstash's
|
|
32
|
+
[UUID filter](https://www.elastic.co/guide/en/logstash/current/plugins-filters-uuid.html).
|
|
33
|
+
* It's still possible to lose log messages. Ruby won't detect a TCP/UDP connection problem
|
|
34
|
+
immediately. In my testing, it took Ruby about 4 seconds to notice the receiving end was down
|
|
35
|
+
and start raising exceptions. Since logstash listeners over TCP/UDP do not acknowledge received
|
|
36
|
+
messages, it's not possible to know which log messages to re-send.
|
|
37
|
+
* When `sync` is turned off, Ruby may buffer data internally before writing to
|
|
38
|
+
the IO device. This is why you may not see messages written immediately to a
|
|
39
|
+
UDP or TCP socket, even though LogStashLogger's buffer is periodically flushed.
|
|
40
|
+
|
|
41
|
+
## Full Buffer
|
|
42
|
+
|
|
43
|
+
By default, messages are discarded when the buffer gets full. This can happen
|
|
44
|
+
if the output source is down for too long or log messages are being received
|
|
45
|
+
too quickly. If your application suddenly terminates (for example, by SIGKILL or a power outage),
|
|
46
|
+
the whole buffer will be lost.
|
|
47
|
+
|
|
48
|
+
You can make message loss less likely by increasing `buffer_max_items`
|
|
49
|
+
(so that more events can be held in the buffer), and decreasing `buffer_max_interval` (to wait
|
|
50
|
+
less time between flushes). This will increase memory pressure on your application as log messages
|
|
51
|
+
accumulate in the buffer, so make sure you have allocated enough memory to your process.
|
|
52
|
+
|
|
53
|
+
If you don't want to lose messages when the buffer gets full, you can set
|
|
54
|
+
`drop_messages_on_full_buffer = false`. Note that if the buffer gets full, any
|
|
55
|
+
incoming log message will block, which could be undesirable.
|
|
56
|
+
|
|
57
|
+
## Sync Mode
|
|
58
|
+
|
|
59
|
+
All logger outputs support a `sync` setting. This is analogous to the "sync mode" setting on Ruby IO
|
|
60
|
+
objects. When set to `true`, output is immediately flushed and is not buffered internally. Normally,
|
|
61
|
+
for devices that connect to a remote service, buffering is a good thing because
|
|
62
|
+
it improves performance and reduces the likelihood of errors affecting the program. For these devices,
|
|
63
|
+
`sync` defaults to `false`, and it is recommended to leave the default value.
|
|
64
|
+
You may want to turn sync mode on for testing, for example if you want to see
|
|
65
|
+
log messages immediately after they are written.
|
|
66
|
+
|
|
67
|
+
It is recommended to turn sync mode on for file and Unix socket outputs. This
|
|
68
|
+
ensures that log messages from different threads or processes are written correctly on separate lines.
|
|
69
|
+
|
|
70
|
+
See [#44](https://github.com/dwbutler/logstash-logger/issues/44) for more details.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Customization
|
|
2
|
+
|
|
3
|
+
## Custom Log Fields
|
|
4
|
+
|
|
5
|
+
`LogStashLogger` by default will log a JSON object with the format below.
|
|
6
|
+
|
|
7
|
+
```json
|
|
8
|
+
{
|
|
9
|
+
"@timestamp":"2015-01-29T10:43:32.196-05:00",
|
|
10
|
+
"@version":"1",
|
|
11
|
+
"message":"Some Message",
|
|
12
|
+
"severity":"INFO",
|
|
13
|
+
"host":"hostname"
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Some applications may need to attach additional metadata to each message.
|
|
18
|
+
The `LogStash::Event` can be manipulated directly by specifying a `customize_event` block in the `LogStashLogger` configuration. (Note: this is run after the event has been generated, not before.)
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
config = LogStashLogger.configure do |config|
|
|
22
|
+
config.customize_event do |event|
|
|
23
|
+
event["other_field"] = "some_other_value"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
This configuration would result in the following output.
|
|
29
|
+
|
|
30
|
+
```json
|
|
31
|
+
{
|
|
32
|
+
"@timestamp": "2015-01-29T10:43:32.196-05:00",
|
|
33
|
+
"@version": "1",
|
|
34
|
+
"message": "Some Message",
|
|
35
|
+
"severity": "INFO",
|
|
36
|
+
"host": "hostname",
|
|
37
|
+
"other_field": "some_other_value"
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
This block has full access to the event, so you can remove fields, modify
|
|
42
|
+
existing fields, etc. For example, to remove the default timestamp:
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
config = LogStashLogger.configure do |config|
|
|
46
|
+
config.customize_event do |event|
|
|
47
|
+
event.remove('@timestamp')
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
You can also customize events on a per-logger basis by passing a callable object
|
|
53
|
+
(lambda or proc) to the `customize_event` option when creating a logger:
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
LogStashLogger.new(customize_event: ->(event){ event['other_field'] = 'other_field' })
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Logger Silencing
|
|
60
|
+
|
|
61
|
+
LogStashLogger provides support for Rails-style logger silencing. The
|
|
62
|
+
implementation was extracted from Rails, but has no dependencies, so it can be
|
|
63
|
+
used outside of a Rails app. The interface is the same as in Rails:
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
logger.silence(temporary_level) do
|
|
67
|
+
...
|
|
68
|
+
end
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Custom Logger Class
|
|
72
|
+
|
|
73
|
+
By default, LogStashLogger creates a logger that extends Ruby's built in `Logger` class.
|
|
74
|
+
If you require a different logger implementation, you can use a different class
|
|
75
|
+
by passing in the class with the `logger_class` option.
|
|
76
|
+
|
|
77
|
+
Note that for syslog, the `Syslog::Logger` class is required and cannot be
|
|
78
|
+
changed.
|
|
79
|
+
|
|
80
|
+
## Error Handling
|
|
81
|
+
|
|
82
|
+
If an exception occurs while writing a message to the device, the exception is
|
|
83
|
+
logged using an internal logger. By default, this logs to $stderr. You can
|
|
84
|
+
change the error logger by setting `LogStashLogger.configuration.default_error_logger`, or by passing
|
|
85
|
+
your own logger object in the `:error_logger` configuration key when
|
|
86
|
+
instantiating a LogStashLogger.
|
data/docs/outputs.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Output Configuration
|
|
2
|
+
|
|
3
|
+
## Logstash Listener Configuration
|
|
4
|
+
|
|
5
|
+
In order for logstash to correctly receive and parse the events, you will need to
|
|
6
|
+
configure and run a listener that uses the `json_lines` codec. For example, to receive
|
|
7
|
+
events over UDP on port 5228:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
input {
|
|
11
|
+
udp {
|
|
12
|
+
host => "0.0.0.0"
|
|
13
|
+
port => 5228
|
|
14
|
+
codec => json_lines
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
File and Redis inputs should use the `json` codec instead. For more information
|
|
20
|
+
read the [Logstash docs](https://www.elastic.co/guide/en/logstash/current/plugins-codecs-json_lines.html).
|
|
21
|
+
|
|
22
|
+
See the [samples](https://github.com/dwbutler/logstash-logger/tree/master/samples) directory for more configuration samples.
|
|
23
|
+
|
|
24
|
+
## HTTP
|
|
25
|
+
|
|
26
|
+
Supports rudimentary writes (buffered, non-persistent connections) to the [Logstash HTTP Input](https://www.elastic.co/guide/en/logstash/current/plugins-inputs-http.html):
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
input {
|
|
30
|
+
http {
|
|
31
|
+
port => 8080
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```ruby
|
|
37
|
+
LogStashLogger.new \
|
|
38
|
+
type: :http,
|
|
39
|
+
url: 'http://localhost:8080'
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Note the parameter is `url` and not `uri`. Relies on [Net:HTTP](https://ruby-doc.org/stdlib-2.7.1/libdoc/net/http/rdoc/Net/HTTP.html#class-Net::HTTP-label-HTTPS) to auto-detect SSL usage from the scheme.
|
data/docs/rails.md
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
# Rails Integration
|
|
2
|
+
|
|
3
|
+
Supports Rails 7.2, 8.0, and 8.1.
|
|
4
|
+
|
|
5
|
+
By default, every Rails log message will be written to logstash in `LogStash::Event` JSON format.
|
|
6
|
+
|
|
7
|
+
For minimal, more-structured logstash events, try one of the following gems:
|
|
8
|
+
|
|
9
|
+
* [lograge](https://github.com/roidrage/lograge)
|
|
10
|
+
* [yarder](https://github.com/rurounijones/yarder)
|
|
11
|
+
|
|
12
|
+
Currently these gems output a JSON string, which LogStashLogger then parses.
|
|
13
|
+
Future versions of these gems could potentially have deeper integration with LogStashLogger
|
|
14
|
+
(e.g. by directly writing `LogStash::Event` objects).
|
|
15
|
+
|
|
16
|
+
## Rails Configuration
|
|
17
|
+
|
|
18
|
+
Add the following to your `config/environments/production.rb`:
|
|
19
|
+
|
|
20
|
+
### Common Options
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
# Optional, Rails sets the default to :info
|
|
24
|
+
config.log_level = :debug
|
|
25
|
+
|
|
26
|
+
# Optional, defaults to true in development and false in production
|
|
27
|
+
config.autoflush_log = false
|
|
28
|
+
|
|
29
|
+
# Optional, use a URI to configure
|
|
30
|
+
config.logstash.uri = ENV['LOGSTASH_URI']
|
|
31
|
+
|
|
32
|
+
# Optional. Defaults to :json_lines. If there are multiple outputs,
|
|
33
|
+
# they will all share the same formatter.
|
|
34
|
+
config.logstash.formatter = :json_lines
|
|
35
|
+
|
|
36
|
+
# Optional, the logger to log writing errors to. Defaults to logging to $stderr
|
|
37
|
+
config.logstash.error_logger = Logger.new($stderr)
|
|
38
|
+
|
|
39
|
+
# Optional, max number of items to buffer before flushing. Defaults to 50
|
|
40
|
+
config.logstash.buffer_max_items = 50
|
|
41
|
+
|
|
42
|
+
# Optional, max number of seconds to wait between flushes. Defaults to 5
|
|
43
|
+
config.logstash.buffer_max_interval = 5
|
|
44
|
+
|
|
45
|
+
# Optional, drop message when a connection error occurs. Defaults to false
|
|
46
|
+
config.logstash.drop_messages_on_flush_error = false
|
|
47
|
+
|
|
48
|
+
# Optional, drop messages when the buffer is full. Defaults to true
|
|
49
|
+
config.logstash.drop_messages_on_full_buffer = true
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### UDP
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
# Optional, defaults to '0.0.0.0'
|
|
56
|
+
config.logstash.host = 'localhost'
|
|
57
|
+
|
|
58
|
+
# Optional, defaults to :udp.
|
|
59
|
+
config.logstash.type = :udp
|
|
60
|
+
|
|
61
|
+
# Required, the port to connect to
|
|
62
|
+
config.logstash.port = 5228
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### TCP
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
# Optional, defaults to '0.0.0.0'
|
|
69
|
+
config.logstash.host = 'localhost'
|
|
70
|
+
|
|
71
|
+
# Required, the port to connect to
|
|
72
|
+
config.logstash.port = 5228
|
|
73
|
+
|
|
74
|
+
# Required
|
|
75
|
+
config.logstash.type = :tcp
|
|
76
|
+
|
|
77
|
+
# Optional, enables SSL
|
|
78
|
+
config.logstash.ssl_enable = true
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Unix Socket
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
# Required
|
|
85
|
+
config.logstash.type = :unix
|
|
86
|
+
|
|
87
|
+
# Required
|
|
88
|
+
config.logstash.path = '/tmp/sock'
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Syslog
|
|
92
|
+
|
|
93
|
+
`Syslog::Logger` is built into the standard library for Ruby 3.2+.
|
|
94
|
+
|
|
95
|
+
```ruby
|
|
96
|
+
# Required
|
|
97
|
+
config.logstash.type = :syslog
|
|
98
|
+
|
|
99
|
+
# Optional. Defaults to 'ruby'
|
|
100
|
+
config.logstash.program_name = 'MyApp'
|
|
101
|
+
|
|
102
|
+
# Optional default facility level. Only works in Ruby 2+
|
|
103
|
+
config.logstash.facility = Syslog::LOG_LOCAL0
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Redis
|
|
107
|
+
|
|
108
|
+
Add the redis gem to your Gemfile:
|
|
109
|
+
|
|
110
|
+
gem 'redis'
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
# Required
|
|
114
|
+
config.logstash.type = :redis
|
|
115
|
+
|
|
116
|
+
# Optional, will default to the 'logstash' list
|
|
117
|
+
config.logstash.list = 'logstash'
|
|
118
|
+
|
|
119
|
+
# All other options are passed in to the Redis client
|
|
120
|
+
# Supported options include host, port, path, password, url
|
|
121
|
+
# Example:
|
|
122
|
+
|
|
123
|
+
# Optional, Redis will default to localhost
|
|
124
|
+
config.logstash.host = 'localhost'
|
|
125
|
+
|
|
126
|
+
# Optional, Redis will default to port 6379
|
|
127
|
+
config.logstash.port = 6379
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Kafka
|
|
131
|
+
|
|
132
|
+
Add the ruby-kafka gem to your Gemfile:
|
|
133
|
+
|
|
134
|
+
gem 'ruby-kafka'
|
|
135
|
+
|
|
136
|
+
```ruby
|
|
137
|
+
# Required
|
|
138
|
+
config.logstash.type = :kafka
|
|
139
|
+
|
|
140
|
+
# Required
|
|
141
|
+
config.logstash.topic = 'logstash-topic'
|
|
142
|
+
|
|
143
|
+
# Required, can be in one of two formats:
|
|
144
|
+
# String format (splits on single space):
|
|
145
|
+
config.logstash.brokers = 'localhost:9092 some-other-host.net:9300'
|
|
146
|
+
# Array format
|
|
147
|
+
config.logstash.brokers = %w(localhost:9092 some-other-host.net:9300)
|
|
148
|
+
|
|
149
|
+
# Optional, defaults to 'ruby-kafka'
|
|
150
|
+
config.logstash.client_id = 'logstash-client-alpha'
|
|
151
|
+
|
|
152
|
+
# Optional, transmit over TLS
|
|
153
|
+
# NOTE: either 0 or all 3 ssl_parameters must be provided for a
|
|
154
|
+
# successful connection. An exception will be raised if 1 or 2 params
|
|
155
|
+
# are povided
|
|
156
|
+
config.logstash.ssl_ca_cert: ENV['CLOUDKAFKA_CA']
|
|
157
|
+
config.logstash.ssl_client_cert: ENV['CLOUDKAFKA_CERT']
|
|
158
|
+
config.logstash.ssl_client_cert_key: ENV['CLOUDKAFKA_PRIVATE_KEY']
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### Kinesis
|
|
162
|
+
|
|
163
|
+
Add the aws-sdk gem to your Gemfile:
|
|
164
|
+
|
|
165
|
+
# aws-sdk >= 3.0
|
|
166
|
+
gem 'aws-sdk-kinesis'
|
|
167
|
+
|
|
168
|
+
# aws-sdk < 3.0
|
|
169
|
+
gem 'aws-sdk'
|
|
170
|
+
|
|
171
|
+
```ruby
|
|
172
|
+
# Required
|
|
173
|
+
config.logstash.type = :kinesis
|
|
174
|
+
|
|
175
|
+
# Optional, will default to the 'logstash' stream
|
|
176
|
+
config.logstash.stream = 'my-stream-name'
|
|
177
|
+
|
|
178
|
+
# Optional, will default to 'us-east-1'
|
|
179
|
+
config.logstash.aws_region = 'us-west-2'
|
|
180
|
+
|
|
181
|
+
# Optional, will default to the AWS_ACCESS_KEY_ID environment variable
|
|
182
|
+
config.logstash.aws_access_key_id = 'ASKASKHLD12341'
|
|
183
|
+
|
|
184
|
+
# Optional, will default to the AWS_SECRET_ACCESS_KEY environment variable
|
|
185
|
+
config.logstash.aws_secret_access_key = 'ASKASKHLD1234123412341234'
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Firehose
|
|
189
|
+
|
|
190
|
+
Add the aws-sdk gem to your Gemfile:
|
|
191
|
+
|
|
192
|
+
# aws-sdk >= 3.0
|
|
193
|
+
gem 'aws-sdk-firehose'
|
|
194
|
+
|
|
195
|
+
# aws-sdk < 3.0
|
|
196
|
+
gem 'aws-sdk'
|
|
197
|
+
|
|
198
|
+
```ruby
|
|
199
|
+
# Required
|
|
200
|
+
config.logstash.type = :firehose
|
|
201
|
+
|
|
202
|
+
# Optional, will default to the 'logstash' delivery stream
|
|
203
|
+
config.logstash.stream = 'my-stream-name'
|
|
204
|
+
|
|
205
|
+
# Optional, will default to AWS default region config chain
|
|
206
|
+
config.logstash.aws_region = 'us-west-2'
|
|
207
|
+
|
|
208
|
+
# Optional, will default to AWS default credential provider chain
|
|
209
|
+
config.logstash.aws_access_key_id = 'ASKASKHLD12341'
|
|
210
|
+
|
|
211
|
+
# Optional, will default to AWS default credential provider chain
|
|
212
|
+
config.logstash.aws_secret_access_key = 'ASKASKHLD1234123412341234'
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### File
|
|
216
|
+
|
|
217
|
+
```ruby
|
|
218
|
+
# Required
|
|
219
|
+
config.logstash.type = :file
|
|
220
|
+
|
|
221
|
+
# Optional, defaults to Rails log path
|
|
222
|
+
config.logstash.path = 'log/production.log'
|
|
223
|
+
|
|
224
|
+
# Optional, enable log rotation
|
|
225
|
+
config.logstash.shift_age = 7
|
|
226
|
+
config.logstash.shift_size = 10 * 1024 * 1024
|
|
227
|
+
|
|
228
|
+
# Optional, time-based rotation
|
|
229
|
+
config.logstash.shift_age = 'daily' # or 'weekly'/'monthly'
|
|
230
|
+
config.logstash.shift_period_suffix = '%Y-%m-%d'
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### IO
|
|
234
|
+
|
|
235
|
+
```ruby
|
|
236
|
+
# Required
|
|
237
|
+
config.logstash.type = :io
|
|
238
|
+
|
|
239
|
+
# Required
|
|
240
|
+
config.logstash.io = io
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Multi Delegator
|
|
244
|
+
|
|
245
|
+
```ruby
|
|
246
|
+
# Required
|
|
247
|
+
config.logstash.type = :multi_delegator
|
|
248
|
+
|
|
249
|
+
# Required
|
|
250
|
+
config.logstash.outputs = [
|
|
251
|
+
{
|
|
252
|
+
type: :file,
|
|
253
|
+
path: 'log/production.log'
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
type: :udp,
|
|
257
|
+
port: 5228,
|
|
258
|
+
host: 'localhost'
|
|
259
|
+
}
|
|
260
|
+
]
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Multi Logger
|
|
264
|
+
|
|
265
|
+
```ruby
|
|
266
|
+
# Required
|
|
267
|
+
config.logstash.type = :multi_logger
|
|
268
|
+
|
|
269
|
+
# Required. Each logger may have its own formatter.
|
|
270
|
+
config.logstash.outputs = [
|
|
271
|
+
{
|
|
272
|
+
type: :file,
|
|
273
|
+
path: 'log/production.log',
|
|
274
|
+
formatter: ::Logger::Formatter
|
|
275
|
+
},
|
|
276
|
+
{
|
|
277
|
+
type: :udp,
|
|
278
|
+
port: 5228,
|
|
279
|
+
host: 'localhost'
|
|
280
|
+
}
|
|
281
|
+
]
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## Logging HTTP request data
|
|
285
|
+
|
|
286
|
+
In web applications, you can log data from HTTP requests (such as headers) using the
|
|
287
|
+
[RequestStore](https://github.com/steveklabnik/request_store) middleware. The following
|
|
288
|
+
example assumes Rails.
|
|
289
|
+
|
|
290
|
+
```ruby
|
|
291
|
+
# in Gemfile
|
|
292
|
+
gem 'request_store'
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
```ruby
|
|
296
|
+
# in application.rb
|
|
297
|
+
LogStashLogger.configure do |config|
|
|
298
|
+
config.customize_event do |event|
|
|
299
|
+
event["session_id"] = RequestStore.store[:load_balancer_session_id]
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
```ruby
|
|
305
|
+
# in app/controllers/application_controller.rb
|
|
306
|
+
before_filter :track_load_balancer_session_id
|
|
307
|
+
|
|
308
|
+
def track_load_balancer_session_id
|
|
309
|
+
RequestStore.store[:load_balancer_session_id] = request.headers["X-LOADBALANCER-SESSIONID"]
|
|
310
|
+
end
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
## Cleaning up resources when forking
|
|
314
|
+
|
|
315
|
+
If your application forks (as is common with many web servers) you will need to
|
|
316
|
+
manage cleaning up resources on your LogStashLogger instances. The instance method
|
|
317
|
+
`#reset` is available for this purpose. Here is sample configuration for
|
|
318
|
+
several common web servers used with Rails:
|
|
319
|
+
|
|
320
|
+
### Passenger
|
|
321
|
+
|
|
322
|
+
```ruby
|
|
323
|
+
::PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
|
324
|
+
Rails.logger.reset
|
|
325
|
+
end
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### Puma
|
|
329
|
+
|
|
330
|
+
```ruby
|
|
331
|
+
# In config/puma.rb
|
|
332
|
+
on_worker_boot do
|
|
333
|
+
Rails.logger.reset
|
|
334
|
+
end
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
### Unicorn
|
|
338
|
+
|
|
339
|
+
```ruby
|
|
340
|
+
# In config/unicorn.rb
|
|
341
|
+
after_fork do |server, worker|
|
|
342
|
+
Rails.logger.reset
|
|
343
|
+
end
|
|
344
|
+
```
|
data/docs/ssl.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# SSL Configuration
|
|
2
|
+
|
|
3
|
+
If you are using TCP then there is the option of adding an SSL certificate to the options hash on initialize.
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
LogStashLogger.new(type: :tcp, port: 5228, ssl_certificate: "/path/to/certificate.crt")
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
The SSL certificate and key can be generated using
|
|
10
|
+
|
|
11
|
+
openssl req -x509 -batch -nodes -newkey rsa:2048 -keyout logstash.key -out logstash.crt
|
|
12
|
+
|
|
13
|
+
You can also enable SSL without a certificate:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
LogStashLogger.new(type: :tcp, port: 5228, ssl_enable: true)
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Specify an SSL context to have more control over the behavior. For example,
|
|
20
|
+
set the verify mode:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
ctx = OpenSSL::SSL::SSLContext.new
|
|
24
|
+
ctx.set_params(verify_mode: OpenSSL::SSL::VERIFY_NONE)
|
|
25
|
+
LogStashLogger.new(type: :tcp, port: 5228, ssl_context: ctx)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Logstash Configuration for SSL
|
|
29
|
+
|
|
30
|
+
The following Logstash configuration is required for SSL:
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
input {
|
|
34
|
+
tcp {
|
|
35
|
+
host => "0.0.0.0"
|
|
36
|
+
port => 5228
|
|
37
|
+
codec => json_lines
|
|
38
|
+
ssl_enable => true
|
|
39
|
+
ssl_cert => "/path/to/certificate.crt"
|
|
40
|
+
ssl_key => "/path/to/key.key"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Hostname Verification
|
|
46
|
+
|
|
47
|
+
Hostname verification is enabled by default. Without further configuration,
|
|
48
|
+
the hostname supplied to `:host` will be used to verify the server's certificate
|
|
49
|
+
identity.
|
|
50
|
+
|
|
51
|
+
If you don't pass an `:ssl_context` or pass a falsey value to the
|
|
52
|
+
`:verify_hostname` option, hostname verification will not occur.
|
|
53
|
+
|
|
54
|
+
### Examples
|
|
55
|
+
|
|
56
|
+
**Verify the hostname from the `:host` option**
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
ctx = OpenSSL::SSL::SSLContext.new
|
|
60
|
+
ctx.cert = '/path/to/cert.pem'
|
|
61
|
+
ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
|
62
|
+
|
|
63
|
+
LogStashLogger.new \
|
|
64
|
+
type: :tcp,
|
|
65
|
+
host: 'logstash.example.com'
|
|
66
|
+
port: 5228,
|
|
67
|
+
ssl_context: ctx
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**Verify a hostname different from the `:host` option**
|
|
71
|
+
|
|
72
|
+
```ruby
|
|
73
|
+
LogStashLogger.new \
|
|
74
|
+
type: :tcp,
|
|
75
|
+
host: '1.2.3.4'
|
|
76
|
+
port: 5228,
|
|
77
|
+
ssl_context: ctx,
|
|
78
|
+
verify_hostname: 'server.example.com'
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Explicitly disable hostname verification**
|
|
82
|
+
|
|
83
|
+
```ruby
|
|
84
|
+
LogStashLogger.new \
|
|
85
|
+
type: :tcp,
|
|
86
|
+
host: '1.2.3.4'
|
|
87
|
+
port: 5228,
|
|
88
|
+
ssl_context: ctx,
|
|
89
|
+
verify_hostname: false
|
|
90
|
+
```
|