It can be configured in config/environment.rb

By default it uses standard output because it’s a that most hosting SaaS companies suggest using.

If you want to use a file, pass stream: 'path/to/file.log' as an option.

Hanami automatically logs the body of non-GET HTTP requests.

When a user submits a form, all the fields and their values will appear in the log:

  1. [bookshelf] [INFO] [2017-08-11 18:17:54 +0200] HTTP/1.1 POST 302 ::1 /signup 5 {"signup"=>{"username"=>"jodosha", "password"=>"secret", "password_confirmation"=>"secret", "bio"=>"lorem"}} 0.00593

To prevent sensitive information from being logged, you can filter it:

  1. # config/environment.rb
  2. # ...
  3. Hanami.configure do
  4. # ...
  5. environment :development do
  6. logger level: :debug, filter: %w[password password_confirmation]
  7. end
  8. end
  1. [bookshelf] [INFO] [2017-08-11 18:17:54 +0200] HTTP/1.1 POST 302 ::1 /signup 5 {"signup"=>{"username"=>"jodosha", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "bio"=>"lorem"}} 0.00593

It also supports fine grained patterns to disambiguate params with the same name. For instance, we have a billing form with street number and credit card number, and we want only to filter the credit card:

  1. # config/environment.rb
  2. # ...
  3. Hanami.configure do
  4. # ...
  5. environment :development do
  6. logger level: :debug, filter: %w[credit_card.number]
  7. end
  8. end

Note that billing => address => number wasn’t filtered while billing => credit_card => number was filtered instead.

If you want to disable logging of the body completely, it can be easily achieved with custom formatter:

  1. class NoParamsFormatter < ::Hanami::Logger::Formatter
  2. def _format(hash)
  3. hash.delete :params
  4. end
  5. end

and then just tell logger to use our new formatter for logging

You can specify , that are compatible with Ruby’s Logger.

Here’s how to setup daily log rotation:

  1. # config/environment.rb
  2. # ...
  3. Hanami.configure do
  4. # ...
  5. environment :production do
  6. logger 'daily', level: :info, formatter: :json, stream: 'log/production.log'
  7. # ...
  8. end
  9. end
  1. # config/environment.rb
  2. # ...
  3. Hanami.configure do
  4. # ...
  5. environment :production do
  6. logger 10, 1_024_000, level: :info, formatter: :json, stream: 'log/production.log'
  7. # ...
  8. end
  9. end

All HTTP requests, SQL queries, and database operations are automatically logged.

When a project is used in development mode, the logging format is human readable:

For production environment, the default format is JSON. JSON is parseable and more machine-oriented. It works great with log aggregators or SaaS logging products.

  1. {"app":"bookshelf","severity":"INFO","time":"2017-02-10T22:31:51Z","http":"HTTP/1.1","verb":"GET","status":"200","ip":"127.0.0.1","path":"/books/1","query":"","length":"451","elapsed":0.000391478}

You can specify a custom logger in cases where you desire different logging behaviour. For example, the Timber logger:

  1. # config/environment.rb
  2. Hanami.configure do
  3. # ...
  4. environment :production do
  5. logger Timber::Logger.new(STDOUT)
  6. # ...
  7. end
  8. end

Use this logger as normal via Hanami.logger. It’s important to note that any logger chosen must conform to the default ::Logger interface.

In order to disable the colorization:

  1. # config/environment.rb
  2. # ...
  3. Hanami.configure do
  4. # ...
  5. environment :development do
  6. logger level: :info, colorizer: false
  7. end
  8. end

Custom colorizer

  1. # config/environment.rb
  2. # ...
  3. require_relative "./logger_colorizer"
  4. Hanami.configure do
  5. # ...
  6. environment :development do
  7. logger level: :info, colorizer: LoggerColorizer.new