Skip to content

Commit

Permalink
Port #1420 to v0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
repeatedly committed Feb 2, 2017
1 parent c66ef65 commit 13f7313
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/fluent/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -177,14 +193,15 @@ 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)
formatted = record.inject('') { |result, pair|
result << @delimiter if result.length.nonzero?
result << "#{pair.first}#{@label_delimiter}#{pair.last}"
}
formatted << "\n"
formatted << "\n".freeze if @add_newline
formatted
end
end
Expand All @@ -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
Expand All @@ -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

Expand Down
24 changes: 24 additions & 0 deletions test/test_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 13f7313

Please sign in to comment.