with one click
hanami-logging
// Expert guidance on configuring and using Hanami logger for structured logging
// Expert guidance on configuring and using Hanami logger for structured logging
[HINT] Download the complete skill directory including SKILL.md and all related files
| name | hanami-logging |
| description | Expert guidance on configuring and using Hanami logger for structured logging |
This skill provides expert guidance on configuring, using, and customizing Hanami's built-in logger (v2.x). It covers default behavior per environment, log formatters, structured logging with payloads, log filtering, colorized output, custom backends, and exception logging.
Understand per-environment defaults
Hanami configures the logger differently depending on the environment:
| Environment | Output | Level | Formatter |
|---|---|---|---|
| Development | $stdout | :debug | text |
| Test | logs/test.log | :debug | text |
| Production | $stdout | :info | :json |
The defaults typically serve well without changes.
Set global logger configuration
Access via config.logger in your App class. Settings apply to all environments by default:
# config/app.rb
require "hanami"
module Bookshelf
class App < Hanami::App
# Change formatter for all environments to JSON
config.logger.formatter = :json
end
end
Fine-tune per-environment
Use the environment method for environment-specific configuration:
# config/app.rb
require "hanami"
module Bookshelf
class App < Hanami::App
environment(:development) do
config.logger.stream = root.join("log").join("development.log")
end
end
end
Enable colorized log levels
# config/app.rb
require "hanami"
module Bookshelf
class App < Hanami::App
environment(:development) do
config.logger.options[:colorize] = true
end
end
end
Customize colorized text template
# config/app.rb
require "hanami"
module Bookshelf
class App < Hanami::App
environment(:development) do
config.logger.options[:colorize] = true
config.logger.template = <<~TMPL
[<blue>%<progname>s</blue>] [%<severity>s] [<green>%<time>s</green>] %<message>s %<payload>s
TMPL
end
end
end
Configure sensitive data filtering
Hanami filters these keys by default to prevent sensitive information leaks:
_csrfpasswordpassword_confirmationAdd custom filter keys:
# config/app.rb
require "hanami"
module Bookshelf
class App < Hanami::App
config.logger.filters = config.logger.filters + ["token"]
end
end
Log plain text entries
Use logger methods corresponding to log levels:
# Console / REPL
app["logger"].info "Hello World"
# [bookshelf] [INFO] [2022-11-20 13:47:13 +0100] Hello World
app["logger"].error "Something's wrong"
# [bookshelf] [ERROR] [2022-11-20 13:48:05 +0100] Something's wrong
Available log levels:
debuginfowarnerrorfatalLog structured data with payloads
Hanami supports structured logging by default. Pass data as a payload:
# With text message and payload
app["logger"].info "Hello World", component: "admin"
# [bookshelf] [INFO] [2022-11-20 13:50:43 +0100] Hello World component="admin"
# Payload only (text message is optional)
app["logger"].info text: "Hello World", component: "admin"
# [bookshelf] [INFO] [2022-11-20 13:51:40 +0100] text="Hello World" component="admin"
In production with JSON formatter, payloads become structured JSON fields.
Log exceptions out of the box
Pass rescued exceptions directly to the logger:
begin
raise "OH NOEZ!"
rescue => e
app["logger"].error(e)
end
# [bookshelf] [ERROR] [2022-11-20 13:54:55 +0100]
# OH NOEZ! (RuntimeError)
# (pry):7:in `__pry__'
# ...
Log exceptions with additional payload data
begin
raise "OH NOEZ!"
rescue => e
app["logger"].error(e, component: "admin")
end
# [bookshelf] [ERROR] [2022-11-20 13:56:36 +0100] component="admin"
# OH NOEZ! (RuntimeError)
# ...
Add dedicated logging backends
Route specific log entries to separate files:
# spec/spec_helper.rb
Hanami.logger.add_backend(
stream: Hanami.app.root.join("log").join("exceptions.log"), log_if: :exception?
)
begin
raise "Oh noez"
rescue => e
Hanami.logger.error(e)
end
When assisting with Hanami Logging tasks, follow this workflow:
Identify the logging requirement
Configure the logger appropriately
config.logger settings for cross-environment changesenvironment blocks for per-environment customizationformatter to :json for production structured output:colorize for development readabilityGuide structured logging usage
Address exception logging needs
Configure data filtering
config.logger.filtersReview and refine
<color> tagslog_if conditionsWhen detailed information is needed about specific topics, consult the Hanami documentation:
config/app.rb within the App classenvironment blocks for per-environment settings (global config affects all environments)config.logger.filters to prevent data leakslog_if conditions carefully to avoid duplicating log entriesHanami.logger or app["logger"] to access the logger:json formatter in production for structured log processingenvironment blocks to override defaults per environment:colorize in development for easier log reading<color> tags in custom templates (e.g., <blue>, <green>, <red>)component, user_id, request_id)config.logger.filters for all sensitive fieldslog_if conditions to route specific severity levels