Class: Hanami::Logger::SQLLogger Private
- Inherits:
-
Object
- Object
- Hanami::Logger::SQLLogger
- Defined in:
- lib/hanami/logger/sql_logger.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 logger that integrates with the Hanami logger using structured, tagged logging.
Subscribes to ‘:sql` notification events (emitted by ROM via the Hanami app’s ‘“notifications”` component) and logs each query as a structured payload. The log entries are tagged as `:sql`, which allows Dry Logger backends to route them to a dedicated formatter (see SQLFormatter), in the same way that rack log entries are routed using the `:rack` tag.
Instance Attribute Summary collapse
- #level ⇒ Object readonly private
- #logger ⇒ Object readonly private
Instance Method Summary collapse
-
#initialize(logger, level: :debug) ⇒ SQLLogger
constructor
private
A new instance of SQLLogger.
-
#log_query(time:, name:, query:) ⇒ Object
private
Log a SQL query with structured data.
-
#subscribe(notifications) ⇒ void
private
Subscribes to ‘:sql` notification events.
Constructor Details
#initialize(logger, level: :debug) ⇒ SQLLogger
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 SQLLogger.
21 22 23 24 |
# File 'lib/hanami/logger/sql_logger.rb', line 21 def initialize(logger, level: :debug) @logger = logger @level = level end |
Instance Attribute Details
#level ⇒ Object (readonly)
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.
17 18 19 |
# File 'lib/hanami/logger/sql_logger.rb', line 17 def level @level end |
#logger ⇒ Object (readonly)
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.
17 18 19 |
# File 'lib/hanami/logger/sql_logger.rb', line 17 def logger @logger end |
Instance Method Details
#log_query(time:, name:, query:) ⇒ Object
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.
Log a SQL query with structured data.
39 40 41 42 43 44 45 |
# File 'lib/hanami/logger/sql_logger.rb', line 39 def log_query(time:, name:, query:) logger.tagged(:sql) do logger.public_send(@level) do {query:, db: name, elapsed: time.round(3), elapsed_unit: "ms"} end end end |
#subscribe(notifications) ⇒ void
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.
This method returns an undefined value.
Subscribes to ‘:sql` notification events.
30 31 32 |
# File 'lib/hanami/logger/sql_logger.rb', line 30 def subscribe(notifications) notifications.subscribe(:sql) { |params| log_query(**params) } end |