Class: PG::Connection::GeneralLog::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/pg/connection/general_log/middleware.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Middleware

Returns a new instance of Middleware.



11
12
13
14
15
16
17
18
# File 'lib/pg/connection/general_log/middleware.rb', line 11

def initialize(app, options = {})
  @app = app
  Middleware.enabled = options[:enabled]
  Middleware.path = options[:path] || '/tmp/general_log'
  Middleware.backtrace = options[:backtrace] || false

  GeneralLog.prepend_module if Middleware.enabled
end

Class Attribute Details

.backtraceObject

Returns the value of attribute backtrace.



8
9
10
# File 'lib/pg/connection/general_log/middleware.rb', line 8

def backtrace
  @backtrace
end

.enabledObject

Returns the value of attribute enabled.



8
9
10
# File 'lib/pg/connection/general_log/middleware.rb', line 8

def enabled
  @enabled
end

.pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/pg/connection/general_log/middleware.rb', line 8

def path
  @path
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pg/connection/general_log/middleware.rb', line 20

def call(env)
  if Middleware.enabled
    request = Rack::Request.new(env)
    request_id = extract_request_id(env)
    Thread.current[:request_id] = request_id
  end
  @app.call(env)
ensure
  if Middleware.enabled
    GeneralLog.general_log_with_request_id(request_id)&.writefile(request)
    GeneralLog.delete_general_log(request_id)
    Thread.current[:request_id] = nil
  end
end

#extract_request_id(env) ⇒ Object



35
36
37
# File 'lib/pg/connection/general_log/middleware.rb', line 35

def extract_request_id(env)
  env['action_dispatch.request_id'] || env['HTTP_X_REQUEST_ID'] || SecureRandom.hex(16)
end