Class: Timber::Events::SQLQuery

Inherits:
Timber::Event
  • Object
show all
Defined in:
lib/timber/events/sql_query.rb

Overview

Note:

This event should be installed automatically through integrations, such as the Integrations::ActiveRecord::LogSubscriber integration.

The SQL query event tracks sql queries to your database.

Constant Summary collapse

MESSAGE_MAX_BYTES =
8192.freeze
SQL_MAX_BYTES =
4096.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ SQLQuery

Returns a new instance of SQLQuery.



15
16
17
18
19
20
# File 'lib/timber/events/sql_query.rb', line 15

def initialize(attributes)
  normalizer = Util::AttributeNormalizer.new(attributes)
  @message = normalizer.fetch!(:message, :string, :limit => MESSAGE_MAX_BYTES)
  @sql = normalizer.fetch!(:sql, :string, :limit => SQL_MAX_BYTES)
  @time_ms = normalizer.fetch!(:time_ms, :float, :precision => 6)
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



13
14
15
# File 'lib/timber/events/sql_query.rb', line 13

def message
  @message
end

#sqlObject (readonly)

Returns the value of attribute sql.



13
14
15
# File 'lib/timber/events/sql_query.rb', line 13

def sql
  @sql
end

#time_msObject (readonly)

Returns the value of attribute time_ms.



13
14
15
# File 'lib/timber/events/sql_query.rb', line 13

def time_ms
  @time_ms
end

Instance Method Details

#as_json(_options = {}) ⇒ Object

Builds a hash representation containing simple objects, suitable for serialization (JSON).



31
32
33
# File 'lib/timber/events/sql_query.rb', line 31

def as_json(_options = {})
  {:sql_query => to_hash}
end

#to_hashObject Also known as: to_h



22
23
24
25
26
27
# File 'lib/timber/events/sql_query.rb', line 22

def to_hash
  @to_hash ||= Util::NonNilHashBuilder.build do |h|
    h.add(:sql, sql)
    h.add(:time_ms, time_ms)
  end
end