You can tweak its configuration in your class, but typically, the defaults should serve you well. Hanami sets up its logger differently depending on the environments:

  • In development environment logger logs to $stdout in :debug mode
  • In test environment logger logs to logs/test.log in :debug mode
  • In production environment logger logs to $stdout in :info mode using :json formatter

The logger configuration is namespaced as config.logger and you can access it in your App class. If you change these settings, they will be set for all environments by default.

You can fine-tune your logger on a per-environment basis using the convenient environment method:

  1. # config/app.rb
  2. require "hanami"
  3. module Bookshelf
  4. class App < Hanami::App
  5. environment(:development) do
  6. end
  7. end
  8. end

In order to avoid having sensitive information leak to your log streams, Hanami configures log filtering to filter out the following keys:

  • _csrf
  • password
  • password_confirmation

If you want colorized log levels in your output, you can do so via colorize option:

  1. # config/app.rb
  2. require "hanami"
  3. module Bookshelf
  4. class App < Hanami::App
  5. end
  6. end
  7. end

You can also customize text log template to use custom colors:

Here’s what you could add to your spec_helper.rb to have any exception that’s being logged while tests are running go to log/exceptions.log file:

  1. # spec/spec_helper.rb
  2. Hanami.logger.add_backend(
  3. stream: Hanami.app.root.join("log").join("exceptions.log"), log_if: :exception?
  4. )
  5. begin
  6. raise "Oh noez"
  7. rescue => e
  8. Hanami.logger.error(e)