Class: Hanami::Logger::SQLLogger Private

Inherits:
Object
  • Object
show all
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.

See Also:

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • logger (#tagged, #info)

    a Hanami-compatible logger (typically a Dry::Logger::Dispatcher or a UniversalLogger-wrapped logger)

Since:

  • 0.1.0



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

#levelObject (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.

Since:

  • 0.1.0



17
18
19
# File 'lib/hanami/logger/sql_logger.rb', line 17

def level
  @level
end

#loggerObject (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.

Since:

  • 0.1.0



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.

Parameters:

  • time (Numeric)

    elapsed time in milliseconds

  • name (Symbol)

    database adapter name (e.g. ‘:sqlite`, `:postgres`)

  • query (String)

    the SQL query string

Since:

  • 0.1.0



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.

Parameters:

  • notifications (Dry::Monitor::Notifications)

    the notifications bus

Since:

  • 0.1.0



30
31
32
# File 'lib/hanami/logger/sql_logger.rb', line 30

def subscribe(notifications)
  notifications.subscribe(:sql) { |params| log_query(**params) }
end