logstash-logger 0.26.1 → 1.1.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.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/tests.yml +53 -0
  3. data/.gitignore +1 -0
  4. data/.rubocop.yml +85 -105
  5. data/Appraisals +6 -20
  6. data/CHANGELOG.md +61 -0
  7. data/Gemfile +2 -2
  8. data/README.md +12 -833
  9. data/docs/buffering.md +86 -0
  10. data/docs/customization.md +86 -0
  11. data/docs/outputs.md +65 -0
  12. data/docs/rails.md +349 -0
  13. data/docs/ssl.md +102 -0
  14. data/docs/troubleshooting.md +106 -0
  15. data/docs/usage.md +148 -0
  16. data/gemfiles/rails_8.0.gemfile +10 -0
  17. data/gemfiles/rails_8.1.gemfile +9 -0
  18. data/lib/logstash-logger/buffer.rb +15 -5
  19. data/lib/logstash-logger/configuration.rb +1 -2
  20. data/lib/logstash-logger/device/aws_stream.rb +96 -17
  21. data/lib/logstash-logger/device/base.rb +2 -2
  22. data/lib/logstash-logger/device/connectable.rb +30 -12
  23. data/lib/logstash-logger/device/file.rb +21 -4
  24. data/lib/logstash-logger/device/firehose.rb +6 -0
  25. data/lib/logstash-logger/device/http.rb +104 -0
  26. data/lib/logstash-logger/device/kafka.rb +182 -38
  27. data/lib/logstash-logger/device/kinesis.rb +10 -1
  28. data/lib/logstash-logger/device/redis.rb +8 -1
  29. data/lib/logstash-logger/device/tcp.rb +54 -12
  30. data/lib/logstash-logger/device.rb +24 -2
  31. data/lib/logstash-logger/formatter/base.rb +97 -11
  32. data/lib/logstash-logger/formatter/cee_syslog.rb +8 -11
  33. data/lib/logstash-logger/formatter/json.rb +13 -0
  34. data/lib/logstash-logger/formatter/json_lines.rb +13 -0
  35. data/lib/logstash-logger/formatter.rb +14 -6
  36. data/lib/logstash-logger/logger.rb +6 -19
  37. data/lib/logstash-logger/multi_logger.rb +2 -1
  38. data/lib/logstash-logger/railtie.rb +1 -1
  39. data/lib/logstash-logger/tagged_logging.rb +3 -1
  40. data/lib/logstash-logger/version.rb +1 -1
  41. data/logstash-logger.gemspec +8 -4
  42. data/spec/device/aws_stream_limits_spec.rb +219 -0
  43. data/spec/device/connectable_spec.rb +51 -0
  44. data/spec/device/file_spec.rb +65 -0
  45. data/spec/device/firehose_spec.rb +4 -1
  46. data/spec/device/http_spec.rb +176 -0
  47. data/spec/device/kafka_spec.rb +429 -14
  48. data/spec/device/kinesis_spec.rb +4 -1
  49. data/spec/device/tcp_spec.rb +94 -5
  50. data/spec/device/tcp_tls_integration_spec.rb +91 -0
  51. data/spec/device_spec.rb +13 -0
  52. data/spec/formatter/base_spec.rb +82 -1
  53. data/spec/formatter/cee_syslog_spec.rb +71 -19
  54. data/spec/formatter/framing_spec.rb +58 -0
  55. data/spec/formatter/json_lines_spec.rb +41 -0
  56. data/spec/formatter/json_spec.rb +49 -0
  57. data/spec/formatter_spec.rb +19 -2
  58. data/spec/logger_spec.rb +5 -5
  59. data/spec/multi_logger_spec.rb +16 -0
  60. data/spec/spec_helper.rb +7 -5
  61. data/spec/support/tls_helper.rb +30 -0
  62. data/spec/support/unserializable_value.rb +10 -0
  63. data/spec/tagged_logging_spec.rb +15 -0
  64. metadata +101 -16
  65. data/.travis.yml +0 -26
  66. data/gemfiles/rails_3.2.gemfile +0 -9
  67. data/gemfiles/rails_4.0.gemfile +0 -9
  68. data/gemfiles/rails_4.1.gemfile +0 -9
  69. data/gemfiles/rails_4.2.gemfile +0 -9
  70. data/gemfiles/rails_5.0.gemfile +0 -9
  71. data/gemfiles/rails_5.1.gemfile +0 -9
data/docs/buffering.md ADDED
@@ -0,0 +1,86 @@
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
+ * `:max_record_bytes` - Drop any single formatted record larger than this many bytes, with a warning that gives its size. Off by default.
22
+
23
+ You can turn buffering off by setting `sync = true`.
24
+
25
+ ## Caveats
26
+
27
+ Please be aware of the following caveats to this behavior:
28
+
29
+ * It's possible for duplicate log messages to be sent when retrying. For outputs like Redis and
30
+ Kafka that write in batches, the whole batch could get re-sent. If this is a problem, you
31
+ can add a UUID field to each event to uniquely identify it. You can either do this
32
+ in a `customize_event` block, or by using logstash's
33
+ [UUID filter](https://www.elastic.co/guide/en/logstash/current/plugins-filters-uuid.html).
34
+ * It's still possible to lose log messages. Ruby won't detect a TCP/UDP connection problem
35
+ immediately. In my testing, it took Ruby about 4 seconds to notice the receiving end was down
36
+ and start raising exceptions. Since logstash listeners over TCP/UDP do not acknowledge received
37
+ messages, it's not possible to know which log messages to re-send.
38
+ * When `sync` is turned off, Ruby may buffer data internally before writing to
39
+ the IO device. This is why you may not see messages written immediately to a
40
+ UDP or TCP socket, even though LogStashLogger's buffer is periodically flushed.
41
+
42
+ ## Full Buffer
43
+
44
+ By default, messages are discarded when the buffer gets full. This can happen
45
+ if the output source is down for too long or log messages are being received
46
+ too quickly. If your application suddenly terminates (for example, by SIGKILL or a power outage),
47
+ the whole buffer will be lost.
48
+
49
+ You can make message loss less likely by increasing `buffer_max_items`
50
+ (so that more events can be held in the buffer), and decreasing `buffer_max_interval` (to wait
51
+ less time between flushes). This will increase memory pressure on your application as log messages
52
+ accumulate in the buffer, so make sure you have allocated enough memory to your process.
53
+
54
+ If you don't want to lose messages when the buffer gets full, you can set
55
+ `drop_messages_on_full_buffer = false`. Note that if the buffer gets full, any
56
+ incoming log message will block, which could be undesirable.
57
+
58
+ ## Records that can never be delivered
59
+
60
+ Some outputs can reject a record for a reason that will not go away on retry, such as its size.
61
+ Instead of retrying the whole batch until the buffer fills, these outputs drop only the rejected
62
+ records, and log their size and the reason, never their content:
63
+
64
+ * Kafka: records the broker or client rejects with `MessageSizeTooLarge`, `RecordListTooLarge`
65
+ or `BufferOverflow`. The rest of the batch is sent one record at a time.
66
+ * Kinesis and Firehose: records over the service's per-record limit (Kinesis 1 MiB by default,
67
+ Firehose 1,000 KiB). Batches are split to fit each service's request limits. If your Kinesis
68
+ stream is configured for larger records, raise the limit with `aws_max_record_bytes`, for
69
+ example `aws_max_record_bytes: 10 * 1024 * 1024`.
70
+ * HTTP: records the collector rejects with 400, 413, 415 or 422. The rest of the batch is sent
71
+ one record at a time. Other error statuses, such as 401, 429 or 5xx, are retried.
72
+
73
+ ## Sync Mode
74
+
75
+ All logger outputs support a `sync` setting. This is analogous to the "sync mode" setting on Ruby IO
76
+ objects. When set to `true`, output is immediately flushed and is not buffered internally. Normally,
77
+ for devices that connect to a remote service, buffering is a good thing because
78
+ it improves performance and reduces the likelihood of errors affecting the program. For these devices,
79
+ `sync` defaults to `false`, and it is recommended to leave the default value.
80
+ You may want to turn sync mode on for testing, for example if you want to see
81
+ log messages immediately after they are written.
82
+
83
+ It is recommended to turn sync mode on for file and Unix socket outputs. This
84
+ ensures that log messages from different threads or processes are written correctly on separate lines.
85
+
86
+ 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,65 @@
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
+ A batch that gets an error status is retried on the next flush. If the collector rejects the content
43
+ (400, 413, 415 or 422), the records are sent one at a time and only the rejected ones are dropped.
44
+ See [Records that can never be delivered](buffering.md#records-that-can-never-be-delivered).
45
+
46
+ 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.
47
+
48
+ By default, each batch is sent as a JSON array, which is what the Logstash HTTP Input expects. For endpoints that expect newline-delimited JSON (NDJSON), with one event per line, set `format: :ndjson`:
49
+
50
+ ```ruby
51
+ LogStashLogger.new \
52
+ type: :http,
53
+ url: 'https://example.com:8071/?token=TOKEN',
54
+ format: :ndjson
55
+ ```
56
+
57
+ The `format` option accepts `:json_array` (the default) or `:ndjson`. With `:ndjson` the request is sent with `Content-Type: application/x-ndjson`; with `:json_array` it uses `application/json`. If your endpoint expects a different header, set it explicitly with `content_type`:
58
+
59
+ ```ruby
60
+ LogStashLogger.new \
61
+ type: :http,
62
+ url: 'https://example.com:8071/?token=TOKEN',
63
+ format: :ndjson,
64
+ content_type: 'application/json'
65
+ ```
data/docs/rails.md ADDED
@@ -0,0 +1,349 @@
1
+ # Rails Integration
2
+
3
+ Supports Rails 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 and verifies the server certificate against the system trust store
78
+ config.logstash.ssl_enable = true
79
+
80
+ # Optional, trusts this certificate (for a self-signed or private-CA server) and enables SSL
81
+ # config.logstash.ssl_certificate = '/path/to/certificate.crt'
82
+ ```
83
+
84
+ See [SSL Configuration](ssl.md) for other options.
85
+
86
+ ### Unix Socket
87
+
88
+ ```ruby
89
+ # Required
90
+ config.logstash.type = :unix
91
+
92
+ # Required
93
+ config.logstash.path = '/tmp/sock'
94
+ ```
95
+
96
+ ### Syslog
97
+
98
+ `Syslog::Logger` is built into the standard library for Ruby 3.2+.
99
+
100
+ ```ruby
101
+ # Required
102
+ config.logstash.type = :syslog
103
+
104
+ # Optional. Defaults to 'ruby'
105
+ config.logstash.program_name = 'MyApp'
106
+
107
+ # Optional default facility level. Only works in Ruby 2+
108
+ config.logstash.facility = Syslog::LOG_LOCAL0
109
+ ```
110
+
111
+ ### Redis
112
+
113
+ Add the redis gem to your Gemfile:
114
+
115
+ gem 'redis'
116
+
117
+ ```ruby
118
+ # Required
119
+ config.logstash.type = :redis
120
+
121
+ # Optional, will default to the 'logstash' list
122
+ config.logstash.list = 'logstash'
123
+
124
+ # All other options are passed in to the Redis client
125
+ # Supported options include host, port, path, password, url
126
+ # Example:
127
+
128
+ # Optional, Redis will default to localhost
129
+ config.logstash.host = 'localhost'
130
+
131
+ # Optional, Redis will default to port 6379
132
+ config.logstash.port = 6379
133
+ ```
134
+
135
+ ### Kafka
136
+
137
+ Add the ruby-kafka gem to your Gemfile:
138
+
139
+ gem 'ruby-kafka'
140
+
141
+ ```ruby
142
+ # Required
143
+ config.logstash.type = :kafka
144
+
145
+ # Required
146
+ config.logstash.topic = 'logstash-topic'
147
+
148
+ # Required, can be in one of two formats:
149
+ # String format (splits on single space):
150
+ config.logstash.brokers = 'localhost:9092 some-other-host.net:9300'
151
+ # Array format
152
+ config.logstash.brokers = %w(localhost:9092 some-other-host.net:9300)
153
+
154
+ # Optional, defaults to 'ruby-kafka'
155
+ config.logstash.client_id = 'logstash-client-alpha'
156
+
157
+ # Optional, transmit over TLS
158
+ # NOTE: either 0 or all 3 ssl_parameters must be provided for a
159
+ # successful connection. An exception will be raised if 1 or 2 params
160
+ # are povided
161
+ config.logstash.ssl_ca_cert: ENV['CLOUDKAFKA_CA']
162
+ config.logstash.ssl_client_cert: ENV['CLOUDKAFKA_CERT']
163
+ config.logstash.ssl_client_cert_key: ENV['CLOUDKAFKA_PRIVATE_KEY']
164
+ ```
165
+
166
+ ### Kinesis
167
+
168
+ Add the aws-sdk gem to your Gemfile:
169
+
170
+ # aws-sdk >= 3.0
171
+ gem 'aws-sdk-kinesis'
172
+
173
+ # aws-sdk < 3.0
174
+ gem 'aws-sdk'
175
+
176
+ ```ruby
177
+ # Required
178
+ config.logstash.type = :kinesis
179
+
180
+ # Optional, will default to the 'logstash' stream
181
+ config.logstash.stream = 'my-stream-name'
182
+
183
+ # Optional, will default to 'us-east-1'
184
+ config.logstash.aws_region = 'us-west-2'
185
+
186
+ # Optional, will default to the AWS_ACCESS_KEY_ID environment variable
187
+ config.logstash.aws_access_key_id = 'ASKASKHLD12341'
188
+
189
+ # Optional, will default to the AWS_SECRET_ACCESS_KEY environment variable
190
+ config.logstash.aws_secret_access_key = 'ASKASKHLD1234123412341234'
191
+ ```
192
+
193
+ ### Firehose
194
+
195
+ Add the aws-sdk gem to your Gemfile:
196
+
197
+ # aws-sdk >= 3.0
198
+ gem 'aws-sdk-firehose'
199
+
200
+ # aws-sdk < 3.0
201
+ gem 'aws-sdk'
202
+
203
+ ```ruby
204
+ # Required
205
+ config.logstash.type = :firehose
206
+
207
+ # Optional, will default to the 'logstash' delivery stream
208
+ config.logstash.stream = 'my-stream-name'
209
+
210
+ # Optional, will default to AWS default region config chain
211
+ config.logstash.aws_region = 'us-west-2'
212
+
213
+ # Optional, will default to AWS default credential provider chain
214
+ config.logstash.aws_access_key_id = 'ASKASKHLD12341'
215
+
216
+ # Optional, will default to AWS default credential provider chain
217
+ config.logstash.aws_secret_access_key = 'ASKASKHLD1234123412341234'
218
+ ```
219
+
220
+ ### File
221
+
222
+ ```ruby
223
+ # Required
224
+ config.logstash.type = :file
225
+
226
+ # Optional, defaults to Rails log path
227
+ config.logstash.path = 'log/production.log'
228
+
229
+ # Optional, enable log rotation
230
+ config.logstash.shift_age = 7
231
+ config.logstash.shift_size = 10 * 1024 * 1024
232
+
233
+ # Optional, time-based rotation
234
+ config.logstash.shift_age = 'daily' # or 'weekly'/'monthly'
235
+ config.logstash.shift_period_suffix = '%Y-%m-%d'
236
+ ```
237
+
238
+ ### IO
239
+
240
+ ```ruby
241
+ # Required
242
+ config.logstash.type = :io
243
+
244
+ # Required
245
+ config.logstash.io = io
246
+ ```
247
+
248
+ ### Multi Delegator
249
+
250
+ ```ruby
251
+ # Required
252
+ config.logstash.type = :multi_delegator
253
+
254
+ # Required
255
+ config.logstash.outputs = [
256
+ {
257
+ type: :file,
258
+ path: 'log/production.log'
259
+ },
260
+ {
261
+ type: :udp,
262
+ port: 5228,
263
+ host: 'localhost'
264
+ }
265
+ ]
266
+ ```
267
+
268
+ ### Multi Logger
269
+
270
+ ```ruby
271
+ # Required
272
+ config.logstash.type = :multi_logger
273
+
274
+ # Required. Each logger may have its own formatter.
275
+ config.logstash.outputs = [
276
+ {
277
+ type: :file,
278
+ path: 'log/production.log',
279
+ formatter: ::Logger::Formatter
280
+ },
281
+ {
282
+ type: :udp,
283
+ port: 5228,
284
+ host: 'localhost'
285
+ }
286
+ ]
287
+ ```
288
+
289
+ ## Logging HTTP request data
290
+
291
+ In web applications, you can log data from HTTP requests (such as headers) using the
292
+ [RequestStore](https://github.com/steveklabnik/request_store) middleware. The following
293
+ example assumes Rails.
294
+
295
+ ```ruby
296
+ # in Gemfile
297
+ gem 'request_store'
298
+ ```
299
+
300
+ ```ruby
301
+ # in application.rb
302
+ LogStashLogger.configure do |config|
303
+ config.customize_event do |event|
304
+ event["session_id"] = RequestStore.store[:load_balancer_session_id]
305
+ end
306
+ end
307
+ ```
308
+
309
+ ```ruby
310
+ # in app/controllers/application_controller.rb
311
+ before_filter :track_load_balancer_session_id
312
+
313
+ def track_load_balancer_session_id
314
+ RequestStore.store[:load_balancer_session_id] = request.headers["X-LOADBALANCER-SESSIONID"]
315
+ end
316
+ ```
317
+
318
+ ## Cleaning up resources when forking
319
+
320
+ If your application forks (as is common with many web servers) you will need to
321
+ manage cleaning up resources on your LogStashLogger instances. The instance method
322
+ `#reset` is available for this purpose. Here is sample configuration for
323
+ several common web servers used with Rails:
324
+
325
+ ### Passenger
326
+
327
+ ```ruby
328
+ ::PhusionPassenger.on_event(:starting_worker_process) do |forked|
329
+ Rails.logger.reset
330
+ end
331
+ ```
332
+
333
+ ### Puma
334
+
335
+ ```ruby
336
+ # In config/puma.rb
337
+ on_worker_boot do
338
+ Rails.logger.reset
339
+ end
340
+ ```
341
+
342
+ ### Unicorn
343
+
344
+ ```ruby
345
+ # In config/unicorn.rb
346
+ after_fork do |server, worker|
347
+ Rails.logger.reset
348
+ end
349
+ ```
data/docs/ssl.md ADDED
@@ -0,0 +1,102 @@
1
+ # SSL Configuration
2
+
3
+ The TCP output can connect to Logstash over TLS. The server certificate is always
4
+ verified, and so is the hostname, unless you opt out explicitly.
5
+
6
+ Enable TLS and verify the server against the system trust store:
7
+
8
+ ```ruby
9
+ LogStashLogger.new(type: :tcp, host: 'logstash.example.com', port: 5228, ssl_enable: true)
10
+ ```
11
+
12
+ If your Logstash server uses a self-signed certificate or a private CA, pass that
13
+ certificate (or CA bundle) as `ssl_certificate`. It is trusted in addition to the
14
+ system trust store, and TLS is enabled implicitly:
15
+
16
+ ```ruby
17
+ LogStashLogger.new(type: :tcp, host: 'logstash.example.com', port: 5228, ssl_certificate: "/path/to/certificate.crt")
18
+ ```
19
+
20
+ The file is checked when the logger is created, and an `ArgumentError` is raised
21
+ if it is missing or is not a certificate.
22
+
23
+ A self-signed certificate and key for the Logstash server can be generated with
24
+
25
+ openssl req -x509 -batch -nodes -newkey rsa:2048 -subj "/CN=logstash.example.com" \
26
+ -addext "subjectAltName=DNS:logstash.example.com" -keyout logstash.key -out logstash.crt
27
+
28
+ Use the name your clients connect to in the certificate.
29
+
30
+ Specify an SSL context for full control over the connection. For example, to
31
+ trust a private CA:
32
+
33
+ ```ruby
34
+ ctx = OpenSSL::SSL::SSLContext.new
35
+ ctx.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER, ca_file: '/path/to/ca.pem')
36
+ LogStashLogger.new(type: :tcp, host: 'logstash.example.com', port: 5228, ssl_context: ctx)
37
+ ```
38
+
39
+ ### Turning verification off
40
+
41
+ Verification can only be turned off with an explicit context. This exposes the
42
+ log stream to anyone who can intercept the connection, so use it only for local
43
+ testing:
44
+
45
+ ```ruby
46
+ ctx = OpenSSL::SSL::SSLContext.new
47
+ ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
48
+ LogStashLogger.new(type: :tcp, host: 'localhost', port: 5228, ssl_context: ctx, verify_hostname: false)
49
+ ```
50
+
51
+ Before 1.1.0, `ssl_enable: true` without an `ssl_context` did not verify the
52
+ server certificate. If you relied on that, either pass `ssl_certificate` with the
53
+ server's certificate, or use the explicit context above.
54
+
55
+ ## Logstash Configuration for SSL
56
+
57
+ The following Logstash configuration is required for SSL:
58
+
59
+ ```ruby
60
+ input {
61
+ tcp {
62
+ host => "0.0.0.0"
63
+ port => 5228
64
+ codec => json_lines
65
+ ssl_enable => true
66
+ ssl_cert => "/path/to/certificate.crt"
67
+ ssl_key => "/path/to/key.key"
68
+ }
69
+ }
70
+ ```
71
+
72
+ ## Hostname Verification
73
+
74
+ Hostname verification is enabled by default whenever TLS is used. The hostname
75
+ supplied to `:host` is checked against the server's certificate, and it is also
76
+ sent as the TLS server name (SNI) unless it is an IP address.
77
+
78
+ **Verify a hostname different from the `:host` option**
79
+
80
+ This is useful when connecting by IP address:
81
+
82
+ ```ruby
83
+ LogStashLogger.new \
84
+ type: :tcp,
85
+ host: '1.2.3.4',
86
+ port: 5228,
87
+ ssl_certificate: '/path/to/certificate.crt',
88
+ verify_hostname: 'logstash.example.com'
89
+ ```
90
+
91
+ **Disable hostname verification**
92
+
93
+ The certificate chain is still verified:
94
+
95
+ ```ruby
96
+ LogStashLogger.new \
97
+ type: :tcp,
98
+ host: '1.2.3.4',
99
+ port: 5228,
100
+ ssl_certificate: '/path/to/certificate.crt',
101
+ verify_hostname: false
102
+ ```