-
Notifications
You must be signed in to change notification settings - Fork 13
/
line.rb
52 lines (43 loc) · 1.54 KB
/
line.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# encoding: utf-8
require "logstash/codecs/base"
require "logstash/util/charset"
# Line-oriented text data.
#
# Decoding behavior: Only whole line events will be emitted.
#
# Encoding behavior: Each event will be emitted with a trailing newline.
class LogStash::Codecs::Line < LogStash::Codecs::Base
config_name "line"
# Set the desired text format for encoding.
config :format, :validate => :string
# The character encoding used in this input. Examples include `UTF-8`
# and `cp1252`
#
# This setting is useful if your log files are in `Latin-1` (aka `cp1252`)
# or in another character set other than `UTF-8`.
#
# This only affects "plain" format logs since json is `UTF-8` already.
config :charset, :validate => ::Encoding.name_list, :default => "UTF-8"
# Change the delimiter that separates lines
config :delimiter, :validate => :string, :default => "\n"
MESSAGE_FIELD = "message".freeze
def register
require "logstash/util/buftok"
@buffer = FileWatch::BufferedTokenizer.new(@delimiter)
@converter = LogStash::Util::Charset.new(@charset)
@converter.logger = @logger
end
def decode(data)
@buffer.extract(data).each { |line| yield LogStash::Event.new(MESSAGE_FIELD => @converter.convert(line)) }
end
def flush(&block)
remainder = @buffer.flush
if !remainder.empty?
block.call(LogStash::Event.new(MESSAGE_FIELD => @converter.convert(remainder)))
end
end
def encode(event)
encoded = @format ? event.sprintf(@format) : event.to_s
@on_event.call(event, encoded + @delimiter)
end
end