From a64fc37ed0af1c5ce6f7681187df9f97c1ac9874 Mon Sep 17 00:00:00 2001 From: Takuro Ashie Date: Mon, 2 Jan 2023 16:13:30 +0900 Subject: [PATCH] log/console_adapter: Reflect log level of wrapped logger Signed-off-by: Takuro Ashie --- lib/fluent/log/console_adapter.rb | 9 ++++++++- test/log/test_console_adapter.rb | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/fluent/log/console_adapter.rb b/lib/fluent/log/console_adapter.rb index 2a489440c2..53de0df05d 100644 --- a/lib/fluent/log/console_adapter.rb +++ b/lib/fluent/log/console_adapter.rb @@ -20,7 +20,14 @@ module Fluent class Log class ConsoleAdapter < Console::Terminal::Logger def self.wrap(logger) - Console::Logger.new(ConsoleAdapter.new(logger)) + _, level = Console::Logger::LEVELS.find { |key, value| + if logger.level <= 0 + key == :debug + else + value == logger.level - 1 + end + } + Console::Logger.new(ConsoleAdapter.new(logger), level: level) end def initialize(logger) diff --git a/test/log/test_console_adapter.rb b/test/log/test_console_adapter.rb index 7199fea57c..0d8cbea50b 100644 --- a/test/log/test_console_adapter.rb +++ b/test/log/test_console_adapter.rb @@ -13,7 +13,6 @@ def setup @logger = ServerEngine::DaemonLogger.new(@logdev) @fluent_log = Fluent::Log.new(@logger) @console_logger = Fluent::Log::ConsoleAdapter.wrap(@fluent_log) - @console_logger.level = :debug end def teardown @@ -25,6 +24,20 @@ def test_expected_log_levels Console::Logger::LEVELS) end + data(trace: [Fluent::Log::LEVEL_TRACE, :debug], + debug: [Fluent::Log::LEVEL_DEBUG, :debug], + info: [Fluent::Log::LEVEL_INFO, :info], + warn: [Fluent::Log::LEVEL_WARN, :warn], + error: [Fluent::Log::LEVEL_ERROR, :error], + fatal: [Fluent::Log::LEVEL_FATAL, :fatal]) + def test_reflect_log_level(data) + level, expected = data + @fluent_log.level = level + console_logger = Fluent::Log::ConsoleAdapter.wrap(@fluent_log) + assert_equal(Console::Logger::LEVELS[expected], + console_logger.level) + end + data(debug: :debug, info: :info, warn: :warn,