lib/hanami/config/logger.rb in hanami-2.0.0.rc1 vs lib/hanami/config/logger.rb in hanami-2.0.0

- old
+ new

@@ -13,13 +13,19 @@ include Dry::Configurable # @return [Hanami::SliceName] # # @api private - # @since 2.0.o + # @since 2.0.0 attr_reader :app_name + # @return [Symbol] + # + # @api private + # @since 2.0.0 + attr_reader :env + # @!attribute [rw] level # Sets or returns the logger level. # # Defaults to `:info` for the production environment and `:debug` for all others. # @@ -38,11 +44,11 @@ # # @return [String, #write] # # @api public # @since 2.0.0 - setting :stream + setting :stream, default: $stdout # @!attribute [rw] formatter # Sets or returns the logger's formatter. # # This may be a name that matches a formatter registered with `Dry::Logger`, which includes @@ -55,22 +61,22 @@ # # @return [Symbol, ::Logger::Formatter] # # @api public # @since 2.0.0 - setting :formatter + setting :formatter, default: :string # @!attribute [rw] template # Sets or returns log entry string template # # Defaults to `false`. # # @return [Boolean] # # @api public # @since 2.0.0 - setting :template, default: "[%<progname>s] [%<severity>s] [%<time>s] %<message>s" + setting :template, default: :details # @!attribute [rw] filters # Sets or returns an array of attribute names to filter from logs. # # Defaults to `["_csrf", "password", "password_confirmation"]`. If you want to preserve @@ -83,15 +89,15 @@ setting :filters, default: %w[_csrf password password_confirmation].freeze # @!attribute [rw] logger_constructor # Sets or returns the constructor proc to use for the logger instantiation. # - # Defaults to `Dry.method(:Logger)`. + # Defaults to either `Config#production_logger` or `Config#development_logger` # # @api public # @since 2.0.0 - setting :logger_constructor, default: Dry.method(:Logger) + setting :logger_constructor # @!attribute [rw] options # Sets or returns a hash of options to pass to the {logger_constructor} when initializing # the logger. # @@ -111,46 +117,63 @@ # @param app_name [Hanami::SliceName] # # @api private def initialize(env:, app_name:) @app_name = app_name + @env = env - config.level = case env - when :production - :info - else - :debug - end - - config.stream = case env - when :test - File.join("log", "#{env}.log") - else - $stdout - end - - config.formatter = case env - when :production - :json - else - :rack - end + case env + when :development, :test + config.level = :debug + config.stream = File.join("log", "#{env}.log") if env == :test + config.logger_constructor = method(:development_logger) + when :production + config.level = :info + config.formatter = :json + config.logger_constructor = method(:production_logger) + end end # Returns a new instance of the logger. # # @return [logger_class] # # @api public # @since 2.0.0 def instance - logger_constructor.call(app_name.name, **logger_constructor_opts) + logger_constructor.call(env, app_name.name, **logger_constructor_options) end + # Build an instance of a development logger + # + # This logger is used in both development and test + # + # @return [Dry::Logger::Dispatcher] + # @since 2.0.0 + # @api private + def development_logger(_env, app_name, **options) + Dry.Logger(app_name, **options) do |setup| + setup + .add_backend(log_if: -> entry { !entry.tag?(:rack) }) + .add_backend(formatter: :rack, log_if: -> entry { entry.tag?(:rack) }) + end + end + + # Build an instance of a production logger + # + # This logger is used in both development and test + # + # @return [Dry::Logger::Dispatcher] + # @since 2.0.0 + # @api private + def production_logger(_env, app_name, **options) + Dry.Logger(app_name, **options) + end + private # @api private - def logger_constructor_opts + def logger_constructor_options {stream: stream, level: level, formatter: formatter, filters: filters, template: template,