From dd0f87261dc8c49a30c9d2539e7f6005646eb1f5 Mon Sep 17 00:00:00 2001 From: Daijiro Fukuda Date: Mon, 20 Feb 2023 10:44:13 +0900 Subject: [PATCH] Fix option log_level Signed-off-by: Daijiro Fukuda --- lib/fluent/command/fluentd.rb | 12 +++---- test/command/test_fluentd.rb | 61 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/lib/fluent/command/fluentd.rb b/lib/fluent/command/fluentd.rb index dfe64e4cf8..33f19073e6 100644 --- a/lib/fluent/command/fluentd.rb +++ b/lib/fluent/command/fluentd.rb @@ -158,15 +158,15 @@ } op.on('-v', '--verbose', "increase verbose level (-v: debug, -vv: trace)", TrueClass) {|b| - if b - cmd_opts[:log_level] = [default_opts[:log_level] - 1, Fluent::Log::LEVEL_TRACE].max - end + return unless b + cur_level = cmd_opts.fetch(:log_level, default_opts[:log_level]) + cmd_opts[:log_level] = [cur_level - 1, Fluent::Log::LEVEL_TRACE].max } op.on('-q', '--quiet', "decrease verbose level (-q: warn, -qq: error)", TrueClass) {|b| - if b - cmd_opts[:log_level] = [default_opts[:log_level] + 1, Fluent::Log::LEVEL_ERROR].min - end + return unless b + cur_level = cmd_opts.fetch(:log_level, default_opts[:log_level]) + cmd_opts[:log_level] = [cur_level + 1, Fluent::Log::LEVEL_TRACE].max } op.on('--suppress-config-dump', "suppress config dumping when fluentd starts", TrueClass) {|b| diff --git a/test/command/test_fluentd.rb b/test/command/test_fluentd.rb index 5719055521..a905dcc9b1 100644 --- a/test/command/test_fluentd.rb +++ b/test/command/test_fluentd.rb @@ -1160,4 +1160,65 @@ def multi_workers_ready?; true; end "shared socket for multiple workers is disabled",) end end + + sub_test_case 'log_level by command line option' do + test 'info' do + conf = "" + conf_path = create_conf_file('empty.conf', conf) + assert File.exist?(conf_path) + assert_log_matches(create_cmdline(conf_path), + "[info]", + patterns_not_match: ["[debug]"]) + end + + test 'debug' do + conf = "" + conf_path = create_conf_file('empty.conf', conf) + assert File.exist?(conf_path) + assert_log_matches(create_cmdline(conf_path, "-v"), + "[debug]", + patterns_not_match: ["[trace]"]) + end + + test 'trace' do + conf = < + @type sample + tag test + +CONF + conf_path = create_conf_file('sample.conf', conf) + assert File.exist?(conf_path) + assert_log_matches(create_cmdline(conf_path, "-vv"), + "[trace]",) + end + + test 'warn' do + conf = < + @type sample + tag test + +CONF + conf_path = create_conf_file('sample.conf', conf) + assert File.exist?(conf_path) + assert_log_matches(create_cmdline(conf_path, "-q"), + "[warn]", + patterns_not_match: ["[info]"]) + end + + test 'error' do + conf = < + @type plugin_not_found + tag test + +CONF + conf_path = create_conf_file('plugin_not_found.conf', conf) + assert File.exist?(conf_path) + assert_log_matches(create_cmdline(conf_path, "-qq"), + "[error]", + patterns_not_match: ["[warn]"]) + end + end end