Class: Dbviewer::Logger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/dbviewer/logger.rb

Overview

Logger captures and analyzes SQL queries for debugging and performance monitoring

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



6
7
8
9
# File 'lib/dbviewer/logger.rb', line 6

def initialize
  set_storage
  Rails.logger.info("[DBViewer] Query Logger initialized with #{mode} storage mode")
end

Instance Method Details

#add(event) ⇒ Object

Add a new SQL event query to the logger



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dbviewer/logger.rb', line 12

def add(event)
  # Return early if query logging is disabled
  return unless Dbviewer.configuration.enable_query_logging
  return if QueryParser.should_skip_query?(event)

  current_time = Time.now
  @storage.add({
    sql: event.payload[:sql],
    name: event.payload[:name],
    timestamp: current_time,
    duration_ms: event.duration.round(2),
    binds: QueryParser.format_binds(event.payload[:binds]),
    request_id: event.transaction_id || current_time.to_i,
    thread_id: Thread.current.object_id.to_s,
    caller: event.payload[:caller]
  })
end

#clearObject

Clear all stored queries



31
32
33
# File 'lib/dbviewer/logger.rb', line 31

def clear
  @storage.clear
end

#recent_queries(limit: 100, table_filter: nil, request_id: nil, min_duration: nil) ⇒ Object

Get recent queries, optionally filtered



36
37
38
39
40
41
42
43
# File 'lib/dbviewer/logger.rb', line 36

def recent_queries(limit: 100, table_filter: nil, request_id: nil, min_duration: nil)
  @storage.filter(
    limit: limit,
    table_filter: table_filter,
    request_id: request_id,
    min_duration: min_duration
  )
end

#statsObject

Get stats about all queries



46
47
48
# File 'lib/dbviewer/logger.rb', line 46

def stats
  stats_for_queries(@storage.all)
end

#stats_for_queries(queries) ⇒ Object

Calculate stats for a specific set of queries (can be filtered)



51
52
53
# File 'lib/dbviewer/logger.rb', line 51

def stats_for_queries(queries)
  QueryAnalyzer.generate_stats(queries)
end