mirror of
https://github.com/meineerde/rackstash.git
synced 2025-10-17 14:01:01 +00:00
Add Logstash encoder
This encoder is very similar to the JSON encoder. It handles a few additional specifics for Logstash to be suotable for Logstash's json input.
This commit is contained in:
parent
2fbdf3c1f5
commit
ab595d031b
29
lib/rackstash/encoders/logstash.rb
Normal file
29
lib/rackstash/encoders/logstash.rb
Normal file
@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
# Copyright 2017 Holger Just
|
||||
#
|
||||
# This software may be modified and distributed under the terms
|
||||
# of the MIT license. See the LICENSE.txt file for details.
|
||||
|
||||
require 'rackstash/encoders/json'
|
||||
|
||||
module Rackstash
|
||||
module Encoders
|
||||
# The JSON encoder formats the log event as a single-line JSON string. The
|
||||
# resulting JSON string contains all data exposed by the buffer. Leading
|
||||
# and trailing whitespace as well as any ANSI color codes in the `"message"`
|
||||
# field will be removed.
|
||||
#
|
||||
# The resulting string is in the JSON format native to Logstash. You can
|
||||
# thus ship your logs directly to Logstash without further processing by
|
||||
# using Logstash's [json codec](https://www.elastic.co/guide/en/logstash/current/plugins-codecs-json.html)
|
||||
# on the input definition.
|
||||
class Logstash < JSON
|
||||
# @param event [Hash] a log event as produced by the {Flow}
|
||||
# @return [String] the event as a single-line JSON string
|
||||
def encode(event)
|
||||
event[FIELD_VERSION] = '1'.freeze if event[FIELD_VERSION].nil?
|
||||
super(event)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
20
spec/rackstash/encoders/logstash_spec.rb
Normal file
20
spec/rackstash/encoders/logstash_spec.rb
Normal file
@ -0,0 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
# Copyright 2017 Holger Just
|
||||
#
|
||||
# This software may be modified and distributed under the terms
|
||||
# of the MIT license. See the LICENSE.txt file for details.
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
require 'rackstash/encoders/logstash'
|
||||
|
||||
describe Rackstash::Encoders::Logstash do
|
||||
let(:encoder) { described_class.new }
|
||||
|
||||
describe '#encode' do
|
||||
it 'formats the passed event hash as a JSON string and includes @version' do
|
||||
event = { 'hello' => 'world', 'message' => 'hello' }
|
||||
expect(encoder.encode(event)).to eql '{"hello":"world","message":"hello","@version":"1"}'
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user