Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ Rails.application.configure do
end
```


### Filtering out sensitive info in SQL logs
By default, `lograge-sql` will log full query but if you have sensitive data that need to be filtered out, you can set `query_filter` config:

Expand Down
37 changes: 26 additions & 11 deletions lib/lograge/active_record_log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@ module Lograge
# Log subscriber to replace ActiveRecord's default one
class ActiveRecordLogSubscriber < ActiveSupport::LogSubscriber
# Every time there's an SQL query, stores it into the Thread.
# They'll later be access from the RequestLogSubscriber.
# They'll later be accessed from the RequestLogSubscriber.
def sql(event)
increase_runtime_duration(event)
return if event.payload[:name] == 'SCHEMA'
return unless valid?(event)

# Only store SQL events if `event.duration` is greater than the configured +min_duration+
# No need to check if +min_duration+ is present before as it defaults to 0
return if event.duration.to_f.round(2) < Lograge::Sql.min_duration_ms.to_f

# Filtering out sensitive info in SQL query if custom query_filter is provided
event.payload[:sql] = Lograge::Sql.query_filter.call(event.payload[:sql]) if Lograge::Sql.query_filter

Lograge::Sql.store[:lograge_sql_queries] ||= []
Lograge::Sql.store[:lograge_sql_queries] << Lograge::Sql.extract_event.call(event)
filter_query(event)
store(event)
end

private
Expand All @@ -30,5 +23,27 @@ def increase_runtime_duration(event)

ActiveRecord::LogSubscriber.runtime += event.duration
end

def filter_query(event)
return unless Lograge::Sql.query_filter

# Filtering out sensitive info in SQL query if custom query_filter is provided
event.payload[:sql] = Lograge::Sql.query_filter.call(event.payload[:sql])
end

def valid?(event)
return false if event.payload[:name] == 'SCHEMA'

# Only store SQL events if `event.duration` is greater than the configured +min_duration+
# No need to check if +min_duration+ is present before as it defaults to 0
return false if event.duration.to_f.round(2) < Lograge::Sql.min_duration_ms.to_f

true
end

def store(event)
Lograge::Sql.store[:lograge_sql_queries] ||= []
Lograge::Sql.store[:lograge_sql_queries] << Lograge::Sql.extract_event.call(event)
end
end
end
4 changes: 2 additions & 2 deletions spec/lograge/active_record_log_subscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

before do
allow(Rails).to receive_message_chain('application.config.lograge_sql.keep_default_active_record_log') { true }
allow(event).to receive(:payload).and_return({ sql: "foo" })
allow(event).to receive(:payload).and_return({ sql: 'foo' })
Lograge::Sql.query_filter = query_filter
end

Expand All @@ -72,7 +72,7 @@

it 'apply filter to sql payload' do
log_subscriber.sql(event)
expect(query_filter).to have_received(:call).with("foo")
expect(query_filter).to have_received(:call).with('foo')
end
end
end
Expand Down