diff --git a/lib/fluent/formatter.rb b/lib/fluent/formatter.rb index 2fb8b70694..1fa6e5fc4f 100644 --- a/lib/fluent/formatter.rb +++ b/lib/fluent/formatter.rb @@ -135,6 +135,7 @@ class JSONFormatter < Formatter include StructuredFormatMixin config_param :json_parser, :string, default: 'oj' + config_param :add_newline, :bool, default: true def configure(conf) super @@ -147,19 +148,34 @@ def configure(conf) rescue LoadError @dump_proc = Yajl.method(:dump) end + + # format json is used on various highload environment, so re-define method to skip if check + unless @add_newline + define_singleton_method(:format, method(:format_without_nl)) + end end def format_record(record) "#{@dump_proc.call(record)}\n" end + + def format_without_nl(tag, time, record) + @dump_proc.call(record) + end end class HashFormatter < Formatter include HandleTagAndTimeMixin include StructuredFormatMixin + config_param :add_newline, :bool, default: true + def format_record(record) - "#{record.to_s}\n" + if @add_newline + "#{record.to_s}\n" + else + record.to_s + end end end @@ -177,6 +193,7 @@ class LabeledTSVFormatter < Formatter config_param :delimiter, :string, default: "\t" config_param :label_delimiter, :string, default: ":" + config_param :add_newline, :bool, default: true def format(tag, time, record) filter_record(tag, time, record) @@ -184,7 +201,7 @@ def format(tag, time, record) result << @delimiter if result.length.nonzero? result << "#{pair.first}#{@label_delimiter}#{pair.last}" } - formatted << "\n" + formatted << "\n".freeze if @add_newline formatted end end @@ -197,6 +214,7 @@ class CsvFormatter < Formatter end config_param :force_quotes, :bool, default: true config_param :fields, :array, value_type: :string + config_param :add_newline, :bool, default: true def initialize super @@ -217,7 +235,9 @@ def format(tag, time, record) memo << record[key] memo end - CSV.generate_line(row, @generate_opts) + line = CSV.generate_line(row, @generate_opts) + line.chomp! unless @add_newline + line end end diff --git a/test/test_formatter.rb b/test/test_formatter.rb index 8d50b34f60..a92d463f17 100644 --- a/test/test_formatter.rb +++ b/test/test_formatter.rb @@ -142,6 +142,14 @@ def test_format(data) assert_equal("#{Yajl.dump(record)}\n", formatted) end + data('oj' => 'oj', 'yajl' => 'yajl') + def test_format_without_nl(data) + @formatter.configure('json_parser' => data, 'add_newline' => false) + formatted = @formatter.format(tag, @time, record) + + assert_equal(Yajl.dump(record), formatted) + end + data('oj' => 'oj', 'yajl' => 'yajl') def test_format_with_symbolic_record(data) @formatter.configure('json_parser' => data) @@ -252,6 +260,13 @@ def test_format assert_equal("message:awesome\n", formatted) end + def test_format_without_nl + @formatter.configure('add_newline' => false) + formatted = @formatter.format(tag, @time, record) + + assert_equal("message:awesome", formatted) + end + def test_format_with_tag @formatter.configure('include_tag_key' => 'true') formatted = @formatter.format(tag, @time, record) @@ -319,6 +334,15 @@ def test_format assert_equal("\"awesome\",\"awesome2\"\n", formatted) end + def test_format_without_nl + @formatter.configure('fields' => 'message,message2', 'add_newline' => false) + formatted = @formatter.format(tag, @time, { + 'message' => 'awesome', + 'message2' => 'awesome2' + }) + assert_equal("\"awesome\",\"awesome2\"", formatted) + end + def test_format_with_tag @formatter.configure( 'fields' => 'tag,message,message2',