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

Fix plugin log time format if format is JSON #2356

Merged
merged 2 commits into from
Apr 1, 2019
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/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ def initialize(logger)
end

self.format = @logger.format
self.time_format = @logger.time_format
enable_color @logger.enable_color?
end

Expand All @@ -540,23 +541,29 @@ def level=(log_level_str)
end

alias orig_format= format=
alias orig_time_format= time_format=
alias orig_enable_color enable_color

def format=(fmt)
self.orig_format = fmt
@logger.format = fmt
end

def time_format=(fmt)
self.orig_time_format = fmt
@logger.time_format = fmt
end

def enable_color(b = true)
orig_enable_color b
@logger.enable_color b
end

extend Forwardable
def_delegators '@logger', :get_worker_id, :enable_color?, :enable_debug, :enable_event,
:disable_events, :log_event_enabled, :log_event_enabled=, :time_format, :time_format=,
:time_formatter, :time_formatter=, :event, :caller_line, :puts, :write, :<<, :flush,
:reset, :out, :out=, :optional_header, :optional_header=, :optional_attrs, :optional_attrs=
:disable_events, :log_event_enabled, :log_event_enabled=, :event, :caller_line, :puts, :write,
:<<, :flush, :reset, :out, :out=, :optional_header, :optional_header=, :optional_attrs,
:optional_attrs=
end


Expand Down
36 changes: 30 additions & 6 deletions test/test_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -744,12 +744,6 @@ def test_disable_events
@log.disable_events(Thread.current)
end

def test_time_format
assert_equal(@log.time_format, @logger.time_format)
@log.time_format = "time_format"
assert_equal(@log.time_format, @logger.time_format)
end

def test_event
mock(@logger).event(Fluent::Log::LEVEL_TRACE, { key: "value" })
@log.event(Fluent::Log::LEVEL_TRACE, { key: "value" })
Expand Down Expand Up @@ -794,6 +788,36 @@ def test_optional_attrs
assert_equal(@log.optional_attrs, @logger.optional_attrs)
end
end

sub_test_case "partially delegated" do
def setup
super
@log = Fluent::PluginLogger.new(@logger)
end

data(
text: [:text, "2016-04-21 11:58:41 +0900 [info]: yaaay\n"],
json: [:json, %Q({"time":"2016-04-21 11:58:41 +0900","level":"info","message":"yaaay"}\n)],
)
def test_format(data)
fmt, expected_log_line = data
@log.format = fmt
@log.info "yaaay"
assert{ @log_device.logs.include? expected_log_line }
end

data(
text: [:text, "2016 [info]: yaaay\n"],
json: [:json, %Q({"time":"2016","level":"info","message":"yaaay"}\n)],
)
def test_time_format(data)
fmt, expected_log_line = data
@log.format = fmt
@log.time_format = "%Y"
@log.info "yaaay"
assert{ @log_device.logs.include? expected_log_line }
end
end
end

class PluginLoggerMixinTest < Test::Unit::TestCase
Expand Down