Skip to content

Commit

Permalink
log/console_adapter: Reflect log level of wrapped logger
Browse files Browse the repository at this point in the history
Signed-off-by: Takuro Ashie <[email protected]>
  • Loading branch information
ashie committed Jan 5, 2023
1 parent 1d4990c commit a64fc37
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/fluent/log/console_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 14 additions & 1 deletion test/log/test_console_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit a64fc37

Please sign in to comment.