Skip to content

Commit

Permalink
Add time_format parameter to <log>. fix #1640
Browse files Browse the repository at this point in the history
  • Loading branch information
repeatedly committed Jul 26, 2017
1 parent d6296d3 commit 283d55c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/fluent/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ def reopen!

def apply_options(opts)
$log.format = opts[:format] if opts[:format]
$log.time_format = opts[:time_format] if opts[:time_format]
end

def level=(level)
Expand Down Expand Up @@ -443,7 +444,7 @@ def run_supervisor
show_plugin_config if @show_plugin_config
read_config
set_system_config
@log.apply_options(format: @system_config.log.format)
@log.apply_options(format: @system_config.log.format, time_format: @system_config.log.time_format)

$log.info :supervisor, "parsing config file is succeeded", path: @config_path

Expand Down Expand Up @@ -495,7 +496,7 @@ def run_worker
show_plugin_config if @show_plugin_config
read_config
set_system_config
@log.apply_options(format: @system_config.log.format)
@log.apply_options(format: @system_config.log.format, time_format: @system_config.log.time_format)

Process.setproctitle("worker:#{@process_name}") if @process_name

Expand Down
1 change: 1 addition & 0 deletions lib/fluent/system_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class SystemConfig
end
config_section :log, required: false, init: true, multi: false do
config_param :format, :enum, list: [:text, :json], default: :text
config_param :time_format, :string, default: '%Y-%m-%d %H:%M:%S %z'
end

def self.create(conf)
Expand Down
3 changes: 3 additions & 0 deletions test/config/test_system_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def parse_text(text)
assert_nil(sc.suppress_config_dump)
assert_nil(sc.without_source)
assert_equal(:text, sc.log.format)
assert_equal('%Y-%m-%d %H:%M:%S %z', sc.log.time_format)
assert_equal(1, s.instance_variable_get(:@workers))
assert_nil(s.instance_variable_get(:@root_dir))
assert_equal(Fluent::Log::LEVEL_INFO, s.instance_variable_get(:@log_level))
Expand Down Expand Up @@ -97,13 +98,15 @@ def parse_text(text)
<system>
<log>
format json
time_format %Y
</log>
</system>
EOS
s = FakeSupervisor.new
sc = Fluent::SystemConfig.new(conf)
sc.apply(s)
assert_equal(:json, sc.log.format)
assert_equal('%Y', sc.log.time_format)
end

data(
Expand Down
24 changes: 24 additions & 0 deletions test/test_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,30 @@ def test_format_json
})
end

def test_time_format
logdev = @log_device
logger = ServerEngine::DaemonLogger.new(logdev)
log = Fluent::Log.new(logger)
log.time_format = "%Y"
log.level = Fluent::Log::LEVEL_TRACE
log.trace "trace log"
log.debug "debug log"
log.info "info log"
log.warn "warn log"
log.error "error log"
log.fatal "fatal log"
timestamp_str = @timestamp.strftime("%Y")
expected = [
"#{timestamp_str} [trace]: trace log\n",
"#{timestamp_str} [debug]: debug log\n",
"#{timestamp_str} [info]: info log\n",
"#{timestamp_str} [warn]: warn log\n",
"#{timestamp_str} [error]: error log\n",
"#{timestamp_str} [fatal]: fatal log\n"
]
assert_equal(expected, log.out.logs)
end

def test_disable_events
dl_opts = {}
dl_opts[:log_level] = ServerEngine::DaemonLogger::TRACE
Expand Down
2 changes: 2 additions & 0 deletions test/test_supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def test_system_config
root_dir #{TMP_ROOT_DIR}
<log>
format json
time_format %Y
</log>
</system>
EOC
Expand All @@ -149,6 +150,7 @@ def test_system_config
assert_equal 2, sys_conf.log_level
assert_equal TMP_ROOT_DIR, sys_conf.root_dir
assert_equal :json, sys_conf.log.format
assert_equal '%Y', sys_conf.log.time_format
end

def test_main_process_signal_handlers
Expand Down

0 comments on commit 283d55c

Please sign in to comment.