mirror of
https://github.com/meineerde/rackstash.git
synced 2026-02-01 01:37:12 +00:00
Add Message encoder to just log the log message
This encoder is useful for local consumption of the raw log stream, e.g. during development where the developer might not care for any additional fields. With this encoder, the log output can mostly resemble a "classic" line-based log feed.
This commit is contained in:
parent
e29c8976bb
commit
7c0c983dc2
23
lib/rackstash/encoders/message.rb
Normal file
23
lib/rackstash/encoders/message.rb
Normal file
@ -0,0 +1,23 @@
|
||||
# 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.
|
||||
|
||||
module Rackstash
|
||||
module Encoders
|
||||
# The Message encoder only returns the message of log event. All other
|
||||
# fields and tags are ignored.
|
||||
#
|
||||
# This encoder is useful in environments where the added fields are not
|
||||
# required, mostly during development where debug logs are directly consumed
|
||||
# by humans
|
||||
class Message
|
||||
# @param event [Hash] a log event as produced by the {Flow}
|
||||
# @return [String] the `"message"` field of the event. Trailing whitespace
|
||||
# will be removed.
|
||||
def encode(event)
|
||||
event[FIELD_MESSAGE].rstrip
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
24
spec/rackstash/encoders/message_spec.rb
Normal file
24
spec/rackstash/encoders/message_spec.rb
Normal file
@ -0,0 +1,24 @@
|
||||
# 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/message'
|
||||
|
||||
describe Rackstash::Encoders::Message do
|
||||
let(:encoder) { Rackstash::Encoders::Message.new }
|
||||
|
||||
describe '#encode' do
|
||||
it 'gets the message from the event hash' do
|
||||
event = { 'hello' => 'world', 'message' => 'hello' }
|
||||
expect(encoder.encode(event)).to eql 'hello'
|
||||
end
|
||||
|
||||
it 'rstrips the message' do
|
||||
event = { 'message' => "\n\t \nline1\nline2\n \n\t\n" }
|
||||
expect(encoder.encode(event)).to eql "\n\t \nline1\nline2"
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user