Skip to content

Commit

Permalink
Merge pull request #342 from fluent/add-newline-option-to-single-value
Browse files Browse the repository at this point in the history
Add add_newline option to SingleValueFormatter
  • Loading branch information
repeatedly committed Jun 6, 2014
2 parents e1b81f3 + e1b0c37 commit a8ec325
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/fluent/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ class SingleValueFormatter
include Configurable

config_param :message_key, :string, :default => 'message'
config_param :add_newline, :bool, :default => true

def format(tag, time, record)
record[@message_key]
text = record[@message_key].to_s
text << "\n" if @add_newline
text
end
end

Expand Down
24 changes: 24 additions & 0 deletions test/plugin/test_out_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ def test_write_with_format_json
check_gzipped_result(path, %[#{Yajl.dump({"a" => 1, 'time' => time})}\n] + %[#{Yajl.dump({"a" => 2, 'time' => time})}\n])
end

def test_write_with_format_ltsv
d = create_driver [CONFIG, 'format ltsv', 'include_time_key true'].join("\n")

time = Time.parse("2011-01-02 13:14:15 UTC").to_i
d.emit({"a"=>1}, time)
d.emit({"a"=>2}, time)

# FileOutput#write returns path
path = d.run
check_gzipped_result(path, %[a:1\ttime:2011-01-02T13:14:15Z\n] + %[a:2\ttime:2011-01-02T13:14:15Z\n])
end

def test_write_with_format_single_value
d = create_driver [CONFIG, 'format single_value', 'message_key a'].join("\n")

time = Time.parse("2011-01-02 13:14:15 UTC").to_i
d.emit({"a"=>1}, time)
d.emit({"a"=>2}, time)

# FileOutput#write returns path
path = d.run
check_gzipped_result(path, %[1\n] + %[2\n])
end

def test_write_path_increment
d = create_driver

Expand Down
11 changes: 9 additions & 2 deletions test/test_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,22 @@ def test_config_params
def test_format
formatter = TextFormatter::TEMPLATE_REGISTRY.lookup('single_value').call
formatted = formatter.format('tag', Engine.now, {'message' => 'awesome'})
assert_equal('awesome', formatted)
assert_equal("awesome\n", formatted)
end

def test_format_without_newline
formatter = TextFormatter::TEMPLATE_REGISTRY.lookup('single_value').call
formatter.configure('add_newline' => 'false')
formatted = formatter.format('tag', Engine.now, {'message' => 'awesome'})
assert_equal("awesome", formatted)
end

def test_format_with_message_key
formatter = TextFormatter::SingleValueFormatter.new
formatter.configure('message_key' => 'foobar')
formatted = formatter.format('tag', Engine.now, {'foobar' => 'foo'})

assert_equal('foo', formatted)
assert_equal("foo\n", formatted)
end
end

Expand Down

0 comments on commit a8ec325

Please sign in to comment.