Skip to content

Commit

Permalink
Use Newline::Mixin for newline choice handling
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <hatake@clear-code.com>
  • Loading branch information
cosmo0920 committed Oct 22, 2020
1 parent 0943b14 commit fdcfaf4
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 64 deletions.
13 changes: 2 additions & 11 deletions lib/fluent/plugin/formatter_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,11 @@
module Fluent
module Plugin
class HashFormatter < Formatter
include Fluent::Plugin::Newline::Mixin

Plugin.register_formatter('hash', self)

config_param :add_newline, :bool, default: true
config_param :newline, :enum, list: [:lf, :crlf], default: :lf

def configure(conf)
super
@newline = case newline
when :lf
"\n"
when :crlf
"\r\n"
end
end

def format(tag, time, record)
line = record.to_s
Expand Down
10 changes: 2 additions & 8 deletions lib/fluent/plugin/formatter_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
module Fluent
module Plugin
class JSONFormatter < Formatter
include Fluent::Plugin::Newline::Mixin

Plugin.register_formatter('json', self)

config_param :json_parser, :string, default: 'oj'
config_param :add_newline, :bool, default: true
config_param :newline, :enum, list: [:lf, :crlf], default: :lf

def configure(conf)
super
Expand All @@ -42,13 +43,6 @@ def configure(conf)
unless @add_newline
define_singleton_method(:format, method(:format_without_nl))
end

@newline = case newline
when :lf
"\n"
when :crlf
"\r\n"
end
end

def format(tag, time, record)
Expand Down
14 changes: 2 additions & 12 deletions lib/fluent/plugin/formatter_ltsv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,15 @@
module Fluent
module Plugin
class LabeledTSVFormatter < Formatter
include Fluent::Plugin::Newline::Mixin

Plugin.register_formatter('ltsv', self)

# http://ltsv.org/

config_param :delimiter, :string, default: "\t"
config_param :label_delimiter, :string, default: ":"
config_param :add_newline, :bool, default: true
config_param :newline, :enum, list: [:lf, :crlf], default: :lf

def configure(conf)
super

@newline = case newline
when :lf
"\n"
when :crlf
"\r\n"
end
end

# TODO: escaping for \t in values
def format(tag, time, record)
Expand Down
10 changes: 2 additions & 8 deletions lib/fluent/plugin/formatter_out_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
module Fluent
module Plugin
class OutFileFormatter < Formatter
include Fluent::Plugin::Newline::Mixin

Plugin.register_formatter('out_file', self)

config_param :output_time, :bool, default: true
Expand All @@ -32,20 +34,12 @@ class OutFileFormatter < Formatter
else "\t"
end
end
config_param :newline, :enum, list: [:lf, :crlf], default: :lf
config_set_default :time_type, :string
config_set_default :time_format, nil # time_format nil => iso8601

def configure(conf)
super
@timef = time_formatter_create

@newline = case newline
when :lf
"\n"
when :crlf
"\r\n"
end
end

def format(tag, time, record)
Expand Down
14 changes: 2 additions & 12 deletions lib/fluent/plugin/formatter_single_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,12 @@
module Fluent
module Plugin
class SingleValueFormatter < Formatter
include Fluent::Plugin::Newline::Mixin

Plugin.register_formatter('single_value', self)

config_param :message_key, :string, default: 'message'
config_param :add_newline, :bool, default: true
config_param :newline, :enum, list: [:lf, :crlf], default: :lf

def configure(conf)
super

@newline = case newline
when :lf
"\n"
when :crlf
"\r\n"
end
end

def format(tag, time, record)
text = record[@message_key].to_s.dup
Expand Down
15 changes: 2 additions & 13 deletions lib/fluent/plugin/formatter_tsv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
module Fluent
module Plugin
class TSVFormatter < Formatter
include Fluent::Plugin::Newline::Mixin

Plugin.register_formatter('tsv', self)

desc 'Field names included in each lines'
Expand All @@ -27,19 +29,6 @@ class TSVFormatter < Formatter
config_param :delimiter, :string, default: "\t"
desc 'The parameter to enable writing to new lines'
config_param :add_newline, :bool, default: true
desc 'The newline code'
config_param :newline, :enum, list: [:lf, :crlf], default: :lf

def configure(conf)
super

@newline = case newline
when :lf
"\n"
when :crlf
"\r\n"
end
end

def format(tag, time, record)
formatted = @keys.map{|k| record[k].to_s }.join(@delimiter)
Expand Down

0 comments on commit fdcfaf4

Please sign in to comment.