Skip to content
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
13 changes: 10 additions & 3 deletions lib/fluent/plugin/out_stdout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,6 +47,10 @@ def multi_workers_ready?
true
end

def dest_io
@use_logger ? $log : $stdout
end

attr_accessor :formatter

def configure(conf)
Expand All @@ -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)
Expand All @@ -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
27 changes: 25 additions & 2 deletions test/plugin/test_out_stdout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,36 @@ 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
return $log.string
ensure
$log = tmp
end

def capture_stdout
tmp = $stdout
$stdout = StringIO.new
yield
return $stdout.string
ensure
$stdout = tmp
end
end
Loading