-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Adapt Console::Logger to Fluent::Log #3987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7250495
Add a wapper for Console::Serialized::Logger to output Async's log
ashie bd9b69a
Add a test for Fluent::Log::ConsoleAdapter
ashie aa40037
test_console_adapter: Add more tests
ashie 7b52a7e
log/console_adapter: Ensure not to modify original message
ashie 3bf86c3
log/console_adapter: Simplify initialization parameter
ashie 1d4990c
test_console_adapter: Add more tests
ashie a64fc37
log/console_adapter: Reflect log level of wrapped logger
ashie c3dadf7
log/console_adapter: Add missing seek
ashie 63fb3ae
Add a comment for ConsoleAdapter
ashie 853398f
log/console_adapter: Add a comment for `verbose` argument
ashie bedaa89
test_http_server_helper: Address an incompatible logger
ashie ed50732
log/console_adapter: Fix a wrong require path
ashie fb97f56
log/console_adapter: Fix wrong severity to use
ashie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# | ||
# Fluentd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
require 'console/terminal/logger' | ||
|
||
module Fluent | ||
class Log | ||
# Async gem which is used by http_server helper switched logger mechanism to | ||
# Console gem which isn't complatible with Ruby's standard Logger (since | ||
# v1.17). This class adapts it to Fluentd's logger mechanism. | ||
class ConsoleAdapter < Console::Terminal::Logger | ||
def self.wrap(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) | ||
@logger = logger | ||
# When `verbose` is `true`, following items will be added as a prefix or | ||
# suffix of the subject: | ||
# * Severity | ||
# * Object ID | ||
# * PID | ||
# * Time | ||
# Severity and Time are added by Fluentd::Log too so they are redundant. | ||
# PID is the worker's PID so it's also redundant. | ||
# Object ID will be too verbose in usual cases. | ||
# So set it as `false` here to suppress redundant items. | ||
super(StringIO.new, verbose: false) | ||
ashie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
def call(subject = nil, *arguments, name: nil, severity: 'info', **options, &block) | ||
if LEVEL_TEXT.include?(severity.to_s) | ||
level = severity | ||
else | ||
@logger.warn("Unknown severity: #{severity}") | ||
level = 'warn' | ||
end | ||
|
||
@io.seek(0) | ||
@io.truncate(0) | ||
super | ||
@logger.send(level, @io.string.chomp) | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
require_relative '../helper' | ||
|
||
require 'fluent/log' | ||
require 'fluent/log/console_adapter' | ||
|
||
class ConsoleAdapterTest < Test::Unit::TestCase | ||
def setup | ||
@timestamp = Time.parse("2023-01-01 15:32:41 +0000") | ||
ashie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@timestamp_str = @timestamp.strftime("%Y-%m-%d %H:%M:%S %z") | ||
Timecop.freeze(@timestamp) | ||
|
||
@logdev = Fluent::Test::DummyLogDevice.new | ||
@logger = ServerEngine::DaemonLogger.new(@logdev) | ||
@fluent_log = Fluent::Log.new(@logger) | ||
@console_logger = Fluent::Log::ConsoleAdapter.wrap(@fluent_log) | ||
end | ||
|
||
def teardown | ||
Timecop.return | ||
end | ||
|
||
def test_expected_log_levels | ||
assert_equal({debug: 0, info: 1, warn: 2, error: 3, fatal: 4}, | ||
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, | ||
error: :error, | ||
fatal: :fatal) | ||
def test_string_subject(level) | ||
@console_logger.send(level, "subject") | ||
assert_equal(["#{@timestamp_str} [#{level}]: 0.0s: subject\n"], | ||
@logdev.logs) | ||
end | ||
|
||
data(debug: :debug, | ||
info: :info, | ||
warn: :warn, | ||
error: :error, | ||
fatal: :fatal) | ||
def test_args(level) | ||
@console_logger.send(level, "subject", 1, 2, 3) | ||
assert_equal([ | ||
"#{@timestamp_str} [#{level}]: 0.0s: subject\n" + | ||
" | 1\n" + | ||
" | 2\n" + | ||
" | 3\n" | ||
], | ||
@logdev.logs) | ||
end | ||
|
||
data(debug: :debug, | ||
info: :info, | ||
warn: :warn, | ||
error: :error, | ||
fatal: :fatal) | ||
def test_options(level) | ||
@console_logger.send(level, "subject", kwarg1: "opt1", kwarg2: "opt2") | ||
assert_equal([ | ||
"#{@timestamp_str} [#{level}]: 0.0s: subject\n" + | ||
" | {\"kwarg1\":\"opt1\",\"kwarg2\":\"opt2\"}\n" | ||
], | ||
@logdev.logs) | ||
end | ||
|
||
data(debug: :debug, | ||
info: :info, | ||
warn: :warn, | ||
error: :error, | ||
fatal: :fatal) | ||
def test_block(level) | ||
@console_logger.send(level, "subject") { "block message" } | ||
assert_equal([ | ||
"#{@timestamp_str} [#{level}]: 0.0s: subject\n" + | ||
" | block message\n" | ||
], | ||
@logdev.logs) | ||
end | ||
|
||
data(debug: :debug, | ||
info: :info, | ||
warn: :warn, | ||
error: :error, | ||
fatal: :fatal) | ||
def test_multiple_entries(level) | ||
@console_logger.send(level, "subject1") | ||
@console_logger.send(level, "line2") | ||
assert_equal([ | ||
"#{@timestamp_str} [#{level}]: 0.0s: subject1\n", | ||
"#{@timestamp_str} [#{level}]: 0.0s: line2\n" | ||
], | ||
@logdev.logs) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.