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



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

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

  case env
  when :development
    config.level = :debug
    config.options = {colorize: true}
    config.logger_constructor = method(:development_logger)
  when :test
    config.level = :debug
    config.stream = File.join("log", "#{env}.log")
    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



193
194
195
196
197
198
199
# File 'lib/hanami/config/logger.rb', line 193

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



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

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



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

setting :formatter, default: :string

#levelSymbol

Sets or returns the logger’s level.

Defaults to ‘:info` for the production environment and `:debug` for all others. Can be set via the `HANAMI_LOG_LEVEL` environment variable.

Returns:

  • (Symbol)

Since:

  • 2.0.0



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

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



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

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



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

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



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

setting :stream, default: $stdout

#templateBoolean

Sets or returns log entry string template

Defaults to ‘false`.

Returns:

  • (Boolean)

Since:

  • 2.0.0



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

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



158
159
160
161
162
163
164
165
# File 'lib/hanami/config/logger.rb', line 158

def development_logger(_env, app_name, **options)
  Dry.Logger(app_name, **options) do |setup|
    setup
      .add_backend(log_if: -> entry { !entry.tag?(:rack) && !entry.tag?(:sql) })
      .add_backend(log_if: -> entry { entry.tag?(:rack) }, formatter: Hanami::Logger::RackFormatter)
      .add_backend(log_if: -> entry { entry.tag?(:sql) }, formatter: Hanami::Logger::SQLFormatter)
  end
end

#instancelogger_class

Returns a new instance of the logger.

Returns:

  • (logger_class)

Since:

  • 2.0.0



147
148
149
# File 'lib/hanami/config/logger.rb', line 147

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



174
175
176
# File 'lib/hanami/config/logger.rb', line 174

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