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:
[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:
# config/environment.rb
# ...
Hanami.configure do
# ...
environment :development do
logger level: :debug, filter: %w[password password_confirmation]
end
end
[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:
# config/environment.rb
# ...
Hanami.configure do
# ...
environment :development do
logger level: :debug, filter: %w[credit_card.number]
end
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:
class NoParamsFormatter < ::Hanami::Logger::Formatter
def _format(hash)
hash.delete :params
end
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:
# config/environment.rb
# ...
Hanami.configure do
# ...
environment :production do
logger 'daily', level: :info, formatter: :json, stream: 'log/production.log'
# ...
end
end
# config/environment.rb
# ...
Hanami.configure do
# ...
environment :production do
logger 10, 1_024_000, level: :info, formatter: :json, stream: 'log/production.log'
# ...
end
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.
{"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:
# config/environment.rb
Hanami.configure do
# ...
environment :production do
logger Timber::Logger.new(STDOUT)
# ...
end
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:
# config/environment.rb
# ...
Hanami.configure do
# ...
environment :development do
logger level: :info, colorizer: false
end
end
Custom colorizer
# config/environment.rb
# ...
require_relative "./logger_colorizer"
Hanami.configure do
# ...
environment :development do
logger level: :info, colorizer: LoggerColorizer.new