lib/hanami/web/rack_logger.rb in hanami-2.1.0.beta2.1 vs lib/hanami/web/rack_logger.rb in hanami-2.1.0.rc1
- old
+ new
@@ -1,7 +1,10 @@
# frozen_string_literal: true
+require "delegate"
+require "json"
+
module Hanami
# @api private
module Web
# Rack logger for Hanami apps
#
@@ -51,14 +54,79 @@
payload[:elapsed] = elapsed > 1000 ? "#{elapsed / 1000}ms" : "#{elapsed}#{MICROSECOND}"
payload
end
end
+ # @since 2.1.0
# @api private
+ class UniversalLogger
+ class << self
+ # @since 2.1.0
+ # @api private
+ def call(logger)
+ return logger if compatible_logger?(logger)
+
+ new(logger)
+ end
+
+ # @since 2.1.0
+ # @api private
+ alias_method :[], :call
+
+ private
+
+ def compatible_logger?(logger)
+ logger.respond_to?(:tagged) && accepts_entry_payload?(logger)
+ end
+
+ def accepts_entry_payload?(logger)
+ logger.method(:info).parameters.last.then { |type, _| type == :keyrest }
+ end
+ end
+
+ # @since 2.1.0
+ # @api private
+ attr_reader :logger
+
+ # @since 2.1.0
+ # @api private
+ def initialize(logger)
+ @logger = logger
+ end
+
+ # @since 2.1.0
+ # @api private
+ def tagged(*, &blk)
+ blk.call
+ end
+
+ # Logs the entry as JSON.
+ #
+ # This ensures a reasonable (and parseable) representation of our log payload structures for
+ # loggers that are configured to wholly replace Hanami's default logger.
+ #
+ # @since 2.1.0
+ # @api private
+ def info(message = nil, **payload)
+ payload[:message] = message if message
+ logger.info(JSON.fast_generate(payload))
+ end
+
+ # @see info
+ #
+ # @since 2.1.0
+ # @api private
+ def error(message = nil, **payload)
+ payload[:message] = message if message
+ logger.info(JSON.fast_generate(payload))
+ end
+ end
+
+ # @api private
# @since 2.0.0
def initialize(logger, env: :development)
- @logger = logger
+ @logger = UniversalLogger[logger]
extend(Development) if %i[development test].include?(env)
end
# @api private
# @since 2.0.0
@@ -75,10 +143,10 @@
# @api private
# @since 2.0.0
def log_request(env, status, elapsed)
logger.tagged(:rack) do
- logger.info(data(env, status: status, elapsed: elapsed))
+ logger.info(**data(env, status: status, elapsed: elapsed))
end
end
# @api private
# @since 2.0.0