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

Add nanoseconds to stdout plugin #1249

Merged
merged 1 commit into from
Sep 28, 2016
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
3 changes: 2 additions & 1 deletion lib/fluent/plugin/out_stdout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class StdoutOutput < Output
helpers :inject, :formatter, :compat_parameters

DEFAULT_FORMAT_TYPE = 'json'
TIME_FORMAT = '%Y-%m-%d %H:%M:%S.%9N %z'

config_section :buffer do
config_set_default :chunk_keys, ['tag']
Expand Down Expand Up @@ -69,7 +70,7 @@ def process(tag, es)

def format(tag, time, record)
record = inject_values_to_record(tag, time, record)
"#{Time.at(time).localtime} #{tag}: #{@formatter.format(tag, time, record).chomp}\n"
"#{Time.at(time).localtime.strftime(TIME_FORMAT)} #{tag}: #{@formatter.format(tag, time, record).chomp}\n"
end

def write(chunk)
Expand Down
21 changes: 11 additions & 10 deletions test/plugin/test_out_stdout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def setup

CONFIG = %[
]
TIME_FORMAT = '%Y-%m-%d %H:%M:%S.%9N %z'

def create_driver(conf = CONFIG)
Fluent::Test::Driver::Output.new(Fluent::Plugin::StdoutOutput).configure(conf)
Expand Down Expand Up @@ -40,7 +41,7 @@ def create_driver(conf = CONFIG)
d.feed(time, {'test' => 'test1'})
end
end
assert_equal "#{Time.at(time).localtime} test: {\"test\":\"test1\"}\n", out
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\":\"test1\"}\n", out
end

data('oj' => 'oj', 'yajl' => 'yajl')
Expand All @@ -52,14 +53,14 @@ def create_driver(conf = CONFIG)
d.feed(time, {'test' => 'test1'})
end
end
assert_equal "#{Time.at(time).localtime} test: {\"test\":\"test1\"}\n", out
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\":\"test1\"}\n", out

if data == 'yajl'
# NOTE: Float::NAN is not jsonable
assert_raise(Yajl::EncodeError) { d.feed('test', time, {'test' => Float::NAN}) }
else
out = capture_log { d.feed('test', time, {'test' => Float::NAN}) }
assert_equal "#{Time.at(time).localtime} test: {\"test\":NaN}\n", out
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\":NaN}\n", out
end
end

Expand All @@ -71,11 +72,11 @@ def create_driver(conf = CONFIG)
d.feed(time, {'test' => 'test2'})
end
end
assert_equal "#{Time.at(time).localtime} test: {\"test\"=>\"test2\"}\n", out
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\"=>\"test2\"}\n", out

# NOTE: Float::NAN is not jsonable, but hash string can output it.
out = capture_log { d.feed('test', time, {'test' => Float::NAN}) }
assert_equal "#{Time.at(time).localtime} test: {\"test\"=>NaN}\n", out
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\"=>NaN}\n", out
end
end

Expand Down Expand Up @@ -113,7 +114,7 @@ def create_driver(conf = CONFIG)
d.feed(time, {'test' => 'test'})
end
end
assert_equal "#{Time.at(time).localtime} test: {\"test\":\"test\"}\n", out
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\":\"test\"}\n", out
end
end

Expand All @@ -128,7 +129,7 @@ def create_driver(conf = CONFIG)
d.feed(time, {'test' => 'test'})
end
end
assert_equal "#{Time.at(time).localtime} test: {\"test\":\"test\"}\n", out
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\":\"test\"}\n", out
end

data('oj' => 'oj', 'yajl' => 'yajl')
Expand All @@ -143,7 +144,7 @@ def create_driver(conf = CONFIG)
end
end

assert_equal "#{Time.at(time).localtime} test: {\"test\":\"test\"}\n", out
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\":\"test\"}\n", out
end
end

Expand All @@ -158,7 +159,7 @@ def create_driver(conf = CONFIG)
end
end

assert_equal "#{Time.at(time).localtime} test: {\"test\"=>\"test\"}\n", out
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\"=>\"test\"}\n", out
end

test '#try_write(asynchronous)' do
Expand All @@ -172,7 +173,7 @@ def create_driver(conf = CONFIG)
end
end

assert_equal "#{Time.at(time).localtime} test: {\"test\"=>\"test\"}\n", out
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\"=>\"test\"}\n", out
end
end
end
Expand Down