Skip to content

Commit

Permalink
Merge pull request #2356 from ods94065/ods94065/fix-plugin-log-time-f…
Browse files Browse the repository at this point in the history
…ormat

Fix plugin log time format if format is JSON
  • Loading branch information
repeatedly authored Apr 1, 2019
2 parents 3f37357 + 1d0a1ef commit 0e37e68
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
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

0 comments on commit 0e37e68

Please sign in to comment.