Class: Hanami::Config::Logger

Inherits:
Object
  • Object
show all
Includes:
Dry::Configurable
Defined in:
lib/hanami/config/logger.rb

Overview

Hanami logger config

Since:

  • 2.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env:, app_name:) ⇒ Logger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new ‘Logger` config.

You should not need to initialize this directly, instead use Hanami::Config#logger.

Parameters:

Since:

  • 2.0.0



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hanami/config/logger.rb', line 120

def initialize(env:, app_name:)
  @app_name = app_name
  @env = env

  case env
  when :development, :test
    config.level = :debug
    config.stream = File.join("log", "#{env}.log") if env == :test
    config.logger_constructor = method(:development_logger)
  else
    config.level = :info
    config.formatter = :json
    config.logger_constructor = method(:production_logger)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



185
186
187
188
189
190
191
# File 'lib/hanami/config/logger.rb', line 185

def method_missing(name, *args, &block)
  if config.respond_to?(name)
    config.public_send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#app_nameHanami::SliceName (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

Since:

  • 2.0.0



19
20
21
# File 'lib/hanami/config/logger.rb', line 19

def app_name
  @app_name
end

#envSymbol (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Symbol)

Since:

  • 2.0.0



25
26
27
# File 'lib/hanami/config/logger.rb', line 25

def env
  @env
end

#filtersArray<String>

Sets or returns an array of attribute names to filter from logs.

Defaults to ‘[“_csrf”, “password”, “password_confirmation”]`. If you want to preserve these defaults, append to this array rather than reassigning it.

Returns:

  • (Array<String>)

Since:

  • 2.0.0



89
# File 'lib/hanami/config/logger.rb', line 89

setting :filters, default: %w[_csrf password password_confirmation].freeze

#formatterSymbol, ::Logger::Formatter

Sets or returns the logger’s formatter.

This may be a name that matches a formatter registered with ‘Dry::Logger`, which includes `:string`, `:rack` and `:json`.

This may also be an instance of Ruby’s built-in ‘::Logger::Formatter` or any compatible object.

Defaults to ‘:json` for the production environment, and `:rack` for all others.

Returns:

  • (Symbol, ::Logger::Formatter)

Since:

  • 2.0.0



66
# File 'lib/hanami/config/logger.rb', line 66

setting :formatter, default: :string

#levelSymbol

Sets or returns the logger level.

Defaults to ‘:info` for the production environment and `:debug` for all others.

Returns:

  • (Symbol)

Since:

  • 2.0.0



36
# File 'lib/hanami/config/logger.rb', line 36

setting :level

#logger_constructorObject

Sets or returns the constructor proc to use for the logger instantiation.

Defaults to either ‘Config#production_logger` or `Config#development_logger`

Since:

  • 2.0.0



98
# File 'lib/hanami/config/logger.rb', line 98

setting :logger_constructor

#optionsHash

Sets or returns a hash of options to pass to the #logger_constructor when initializing the logger.

Defaults to ‘[]`

Returns:

  • (Hash)

Since:

  • 2.0.0



110
# File 'lib/hanami/config/logger.rb', line 110

setting :options, default: {}

#streamString, #write

Sets or returns the logger’s stream.

This can be a file path or an ‘IO`-like object for the logger to write to.

Defaults to ‘“log/test.log”` for the test environment and `$stdout` for all others.

Returns:

  • (String, #write)

Since:

  • 2.0.0



49
# File 'lib/hanami/config/logger.rb', line 49

setting :stream, default: $stdout

#templateBoolean

Sets or returns log entry string template

Defaults to ‘false`.

Returns:

  • (Boolean)

Since:

  • 2.0.0



77
# File 'lib/hanami/config/logger.rb', line 77

setting :template, default: :details

Instance Method Details

#development_logger(_env, app_name, **options) ⇒ Dry::Logger::Dispatcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build an instance of a development logger

This logger is used in both development and test

Returns:

  • (Dry::Logger::Dispatcher)

Since:

  • 2.0.0



153
154
155
156
157
158
159
# File 'lib/hanami/config/logger.rb', line 153

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

#instancelogger_class

Returns a new instance of the logger.

Returns:

  • (logger_class)

Since:

  • 2.0.0



142
143
144
# File 'lib/hanami/config/logger.rb', line 142

def instance
  logger_constructor.call(env, app_name.name, **logger_constructor_options)
end

#production_logger(_env, app_name, **options) ⇒ Dry::Logger::Dispatcher

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build an instance of a production logger

This logger is used in both development and test

Returns:

  • (Dry::Logger::Dispatcher)

Since:

  • 2.0.0



168
169
170
# File 'lib/hanami/config/logger.rb', line 168

def production_logger(_env, app_name, **options)
  Dry.Logger(app_name, **options)
end