diff --git a/lib/fluent/plugin/out_stdout.rb b/lib/fluent/plugin/out_stdout.rb index b43fac2212..83ee6a8ffc 100644 --- a/lib/fluent/plugin/out_stdout.rb +++ b/lib/fluent/plugin/out_stdout.rb @@ -25,6 +25,9 @@ class StdoutOutput < Output DEFAULT_LINE_FORMAT_TYPE = 'stdout' DEFAULT_FORMAT_TYPE = 'json' + desc "If Fluentd logger outputs logs to a file (with -o option), this plugin outputs events to the file as well." + config_param :use_logger, :bool, default: true + config_section :buffer do config_set_default :chunk_keys, ['tag'] config_set_default :flush_at_shutdown, true @@ -44,6 +47,10 @@ def multi_workers_ready? true end + def dest_io + @use_logger ? $log : $stdout + end + attr_accessor :formatter def configure(conf) @@ -57,9 +64,9 @@ def configure(conf) def process(tag, es) es = inject_values_to_event_stream(tag, es) es.each {|time,record| - $log.write(format(tag, time, record)) + dest_io.write(format(tag, time, record)) } - $log.flush + dest_io.flush end def format(tag, time, record) @@ -68,7 +75,7 @@ def format(tag, time, record) end def write(chunk) - chunk.write_to($log) + chunk.write_to(dest_io) end end end diff --git a/test/plugin/test_out_stdout.rb b/test/plugin/test_out_stdout.rb index b8f8eb2bed..0420f35a49 100644 --- a/test/plugin/test_out_stdout.rb +++ b/test/plugin/test_out_stdout.rb @@ -193,8 +193,22 @@ def create_driver(conf = CONFIG) end end - # Capture the log output of the block given - def capture_log(&block) + test 'use_logger false' do + d = create_driver(<<~EOC) + use_logger false + EOC + time = event_time + + out = capture_stdout do + d.run(default_tag: 'test', flush: true) do + d.feed(time, {'test' => 'test'}) + end + end + + assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\":\"test\"}\n", out + end + + def capture_log tmp = $log $log = StringIO.new yield @@ -202,4 +216,13 @@ def capture_log(&block) ensure $log = tmp end + + def capture_stdout + tmp = $stdout + $stdout = StringIO.new + yield + return $stdout.string + ensure + $stdout = tmp + end end