Skip to content

Commit

Permalink
Add a wapper for Console::Serialized::Logger to output Async's log
Browse files Browse the repository at this point in the history
Signed-off-by: Takuro Ashie <[email protected]>
  • Loading branch information
ashie committed Dec 14, 2022
1 parent 981decb commit 71ea8fb
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
62 changes: 62 additions & 0 deletions lib/fluent/log/console_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#
# 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/serialized/logger'

module Fluent
class Log
class ConsoleAdapter < Console::Serialized::Logger
def self.wrap(logger)
Console::Logger.new(ConsoleAdapter.new(logger))
end

def initialize(logger)
@logger = logger
super(@logger)
end

def call(subject = nil, *arguments, severity: 'info', **options, &block)
if LEVEL_TEXT.include?(severity)
level = severity
else
@logger.warn "Unknown severity: #{severity}"
level = 'warn'
end

args = arguments.dup
args.unshift("#{subject}") if subject

if block_given?
if block.arity.zero?
args << yield
else
buffer = StringIO.new
yield buffer
args << buffer.string
end
end

exception = find_exception(args)
args.delete(exception) if exception

@logger.send(severity, args)
if exception && @logger.respond_to?("#{level}_backtrace")
@logger.send("#{level}_backtrace", exception)
end
end
end
end
end
3 changes: 2 additions & 1 deletion lib/fluent/plugin_helper/http_server/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
require 'fluent/plugin_helper/http_server/app'
require 'fluent/plugin_helper/http_server/router'
require 'fluent/plugin_helper/http_server/methods'
require 'fluent/log/console_adapter'

module Fluent
module PluginHelper
Expand All @@ -38,7 +39,7 @@ def initialize(addr:, port:, logger:, default_app: nil, tls_context: nil)
scheme = tls_context ? 'https' : 'http'
@uri = URI("#{scheme}://#{@addr}:#{@port}").to_s
@router = Router.new(default_app)
@reactor = Async::Reactor.new(nil, logger: @logger)
@reactor = Async::Reactor.new(nil, logger: Fluent::Log::ConsoleAdapter.wrap(@logger))

opts = if tls_context
{ ssl_context: tls_context }
Expand Down

0 comments on commit 71ea8fb

Please sign in to comment.