Skip to content

Commit 203627e

Browse files
committed
out_stdout: support output to $stdout independently of logger
Signed-off-by: Daijiro Fukuda <fukuda@clear-code.com>
1 parent cefbc62 commit 203627e

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

lib/fluent/plugin/out_stdout.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ class StdoutOutput < Output
2525
DEFAULT_LINE_FORMAT_TYPE = 'stdout'
2626
DEFAULT_FORMAT_TYPE = 'json'
2727

28+
desc "If Fluentd logger outputs logs to a file, this plugin outputs events to the file as well."
29+
config_param :use_logger, :bool, default: true
30+
2831
config_section :buffer do
2932
config_set_default :chunk_keys, ['tag']
3033
config_set_default :flush_at_shutdown, true
@@ -44,6 +47,10 @@ def multi_workers_ready?
4447
true
4548
end
4649

50+
def dest_io
51+
@use_logger ? $log : $stdout
52+
end
53+
4754
attr_accessor :formatter
4855

4956
def configure(conf)
@@ -57,9 +64,9 @@ def configure(conf)
5764
def process(tag, es)
5865
es = inject_values_to_event_stream(tag, es)
5966
es.each {|time,record|
60-
$log.write(format(tag, time, record))
67+
dest_io.write(format(tag, time, record))
6168
}
62-
$log.flush
69+
dest_io.flush
6370
end
6471

6572
def format(tag, time, record)
@@ -68,7 +75,7 @@ def format(tag, time, record)
6875
end
6976

7077
def write(chunk)
71-
chunk.write_to($log)
78+
chunk.write_to(dest_io)
7279
end
7380
end
7481
end

test/plugin/test_out_stdout.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,36 @@ def create_driver(conf = CONFIG)
193193
end
194194
end
195195

196-
# Capture the log output of the block given
197-
def capture_log(&block)
196+
test 'use_logger false' do
197+
d = create_driver(<<~EOC)
198+
use_logger false
199+
EOC
200+
time = event_time
201+
202+
out = capture_stdout do
203+
d.run(default_tag: 'test', flush: true) do
204+
d.feed(time, {'test' => 'test'})
205+
end
206+
end
207+
208+
assert_equal "#{Time.at(time).localtime.strftime(TIME_FORMAT)} test: {\"test\":\"test\"}\n", out
209+
end
210+
211+
def capture_log
198212
tmp = $log
199213
$log = StringIO.new
200214
yield
201215
return $log.string
202216
ensure
203217
$log = tmp
204218
end
219+
220+
def capture_stdout
221+
tmp = $stdout
222+
$stdout = StringIO.new
223+
yield
224+
return $stdout.string
225+
ensure
226+
$stdout = tmp
227+
end
205228
end

0 commit comments

Comments
 (0)