Class: Hanami::Config::Logger
- Inherits:
-
Object
- Object
- Hanami::Config::Logger
- Includes:
- Dry::Configurable
- Defined in:
- lib/hanami/config/logger.rb
Overview
Hanami logger config
Instance Attribute Summary collapse
- #app_name ⇒ Hanami::SliceName readonly private
- #env ⇒ Symbol readonly private
-
#filters ⇒ Array<String>
Sets or returns an array of attribute names to filter from logs.
-
#formatter ⇒ Symbol, ::Logger::Formatter
Sets or returns the logger’s formatter.
-
#level ⇒ Symbol
Sets or returns the logger’s level.
-
#logger_constructor ⇒ Object
Sets or returns the constructor proc to use for the logger instantiation.
-
#options ⇒ Hash
Sets or returns a hash of options to pass to the #logger_constructor when initializing the logger.
-
#stream ⇒ String, #write
Sets or returns the logger’s stream.
-
#template ⇒ Boolean
Sets or returns log entry string template.
Instance Method Summary collapse
-
#development_logger(_env, app_name, **options) ⇒ Dry::Logger::Dispatcher
private
Build an instance of a development logger.
-
#initialize(env:, app_name:) ⇒ Logger
constructor
private
Returns a new ‘Logger` config.
-
#instance ⇒ logger_class
Returns a new instance of the logger.
-
#production_logger(_env, app_name, **options) ⇒ Dry::Logger::Dispatcher
private
Build an instance of a production logger.
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.
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.
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_name ⇒ Hanami::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.
19 20 21 |
# File 'lib/hanami/config/logger.rb', line 19 def app_name @app_name end |
#env ⇒ Symbol (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.
25 26 27 |
# File 'lib/hanami/config/logger.rb', line 25 def env @env end |
#filters ⇒ Array<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.
89 |
# File 'lib/hanami/config/logger.rb', line 89 setting :filters, default: %w[_csrf password password_confirmation].freeze |
#formatter ⇒ Symbol, ::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.
66 |
# File 'lib/hanami/config/logger.rb', line 66 setting :formatter, default: :string |
#level ⇒ Symbol
Sets or returns the logger’s level.
Defaults to ‘:info` for the production environment and `:debug` for all others.
36 |
# File 'lib/hanami/config/logger.rb', line 36 setting :level |
#logger_constructor ⇒ Object
Sets or returns the constructor proc to use for the logger instantiation.
Defaults to either ‘Config#production_logger` or `Config#development_logger`
98 |
# File 'lib/hanami/config/logger.rb', line 98 setting :logger_constructor |
#options ⇒ Hash
Sets or returns a hash of options to pass to the #logger_constructor when initializing the logger.
Defaults to ‘[]`
110 |
# File 'lib/hanami/config/logger.rb', line 110 setting :options, default: {} |
#stream ⇒ String, #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.
49 |
# File 'lib/hanami/config/logger.rb', line 49 setting :stream, default: $stdout |
#template ⇒ Boolean
Sets or returns log entry string template
Defaults to ‘false`.
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
153 154 155 156 157 158 159 |
# File 'lib/hanami/config/logger.rb', line 153 def development_logger(_env, app_name, **) Dry.Logger(app_name, **) do |setup| setup .add_backend(log_if: -> entry { !entry.tag?(:rack) }) .add_backend(formatter: :rack, log_if: -> entry { entry.tag?(:rack) }) end end |
#instance ⇒ logger_class
Returns a new instance of the logger.
142 143 144 |
# File 'lib/hanami/config/logger.rb', line 142 def instance logger_constructor.call(env, app_name.name, **) 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
168 169 170 |
# File 'lib/hanami/config/logger.rb', line 168 def production_logger(_env, app_name, **) Dry.Logger(app_name, **) end |