lib/dry/logger/formatters/structured.rb in dry-logger-1.0.0.rc2 vs lib/dry/logger/formatters/structured.rb in dry-logger-1.0.0
- old
+ new
@@ -1,15 +1,19 @@
# frozen_string_literal: true
require "logger"
+
+require "dry/logger/constants"
require "dry/logger/filter"
module Dry
module Logger
module Formatters
# Default structured formatter which receives {Logger::Entry} from the backends.
#
+ # This class can be used as the base class for your custom formatters.
+ #
# @see http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger/Formatter.html
#
# @since 1.0.0
# @api public
class Structured < ::Logger::Formatter
@@ -21,14 +25,10 @@
# @api private
NOOP_FILTER = -> message { message }
# @since 1.0.0
# @api private
- NEW_LINE = $/ # rubocop:disable Style/SpecialGlobalVars
-
- # @since 1.0.0
- # @api private
attr_reader :filter
# @since 1.0.0
# @api private
attr_reader :options
@@ -50,21 +50,32 @@
#
# @since 1.0.0
# @return [String]
# @api public
def call(_severity, _time, _progname, entry)
- format(entry.filter(filter))
+ format(entry.filter(filter)) + NEW_LINE
end
# Format entry into a loggable object
#
# Custom formatters should override this method
#
# @api since 1.0.0
# @return [Entry]
# @api public
def format(entry)
+ format_values(entry)
+ end
+
+ # @since 1.0.0
+ # @api private
+ def format_values(entry)
entry
+ .to_h
+ .map { |key, value|
+ [key, respond_to?(meth = "format_#{key}", true) ? __send__(meth, value) : value]
+ }
+ .to_h
end
end
end
end
end