Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formatter: Port add newline to v012 #1447

Merged
merged 1 commit into from
Feb 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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