Class: Hanami::Logger::SQLFormatter Private
- Inherits:
-
Dry::Logger::Formatters::String
- Object
- Dry::Logger::Formatters::String
- Hanami::Logger::SQLFormatter
- Defined in:
- lib/hanami/logger/sql_formatter.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
SQL query log formatter for Dry Logger.
Formats SQL query log entries with a template that mirrors the structure of the built-in rack log formatter, providing consistent visual formatting across both HTTP request and database query logs.
For example, in development, SQL logs alongside Rack logs:
[my_app] [INFO] [2026-03-04 10:15:32] SQL sqlite 1.234ms SELECT * FROM users
[my_app] [INFO] [2026-03-04 10:15:32] GET 200 1ms 127.0.0.1 /users -
In production, the default JSON formatter handles SQL entries automatically via the structured payload.
Supports colorization via Dry Logger’s template color tags. When ‘colorize: true` is set in the logger options (the default in development), the “SQL” label is colorized, and severity is colorized per-level by the parent formatter (e.g. INFO => magenta, ERROR => red).
When colorization is enabled and the “rouge” gem is available, SQL queries are syntax highlighted using Rouge’s SQL lexer. This is a soft dependency; if Rouge is not installed, queries output as plain, unhighlighted text.
The Rouge theme defaults to Gruvbox and can be customized by setting the ‘HANAMI_SQL_THEME` environment variable to any Rouge theme name (e.g. “github.dark”, “monokai”, “gruvbox.light”). See `Rouge::Theme.registry` for available themes.
Constant Summary collapse
- SQL_TEMPLATE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
<<~TEXT [%<progname>s] [%<severity>s] [%<time>s] SQL %<db>s %<elapsed>s%<elapsed_unit>s %<query>s TEXT
- SQL_TEMPLATE_COLORIZED =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
<<~TEXT [%<progname>s] [%<severity>s] [%<time>s] <blue>SQL</blue> %<db>s %<elapsed>s%<elapsed_unit>s %<query>s TEXT
Instance Method Summary collapse
-
#initialize(**options) ⇒ SQLFormatter
constructor
private
A new instance of SQLFormatter.
Constructor Details
#initialize(**options) ⇒ SQLFormatter
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 instance of SQLFormatter.
45 46 47 48 49 50 51 |
# File 'lib/hanami/logger/sql_formatter.rb', line 45 def initialize(**) super @template = Dry::Logger::Formatters::Template[ colorize? ? SQL_TEMPLATE_COLORIZED : SQL_TEMPLATE ] @sql_colorizer = build_sql_colorizer if colorize? end |