lograge-sql 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6df7e9d756f02019ce93cbac6c0b19a275035a9d0fe74c631aa699eecf149f6f
4
- data.tar.gz: d34996be2888e4d234d914de95ee092e2b2380181dd70f4b70b2fe84fdc7cc97
3
+ metadata.gz: 8eeabf2a3a00c53b0d7483d6dd84659f2434eb90f0741183370a1559bf9eab1d
4
+ data.tar.gz: da0adc7fa61626a386b563d622234c02971da52f310e687f313b95cf074a6434
5
5
  SHA512:
6
- metadata.gz: 9ef329876869d54eab7e71bf64345917185dfc2ad06d33cb3649689ddaa51ea7b257a425f5b1fb2c39da57b6b26e051d2a13a2d1687b51e99b8813f39be7e12f
7
- data.tar.gz: c15d0baabaaede38e6d949fe64d1abf1f14229ac2160b3d8b6a3b70a2ea117e49f0b4c36693f467622d42af862a1dd27d89e3066d44938a4d83bf35633cc170c
6
+ metadata.gz: fcc84f05b613c82c0b00d9747d6a07976c324e1acef7a6b7b7e98987adb1b319ffcbfe92752682b579ed0eaeb00651431ec9046119dccac8df66a0750a050924
7
+ data.tar.gz: 333ff533242871fcbb4ea2130da71fbdf232625c32b206634fca0f374b7e96e1b2d0b66c0dcccb7ec2799b00b015bcfcde953e54732e5d42d84f4cec6cbb12b4
data/README.md CHANGED
@@ -82,6 +82,15 @@ Rails.application.configure do
82
82
  end
83
83
  ```
84
84
 
85
+ ### Filtering out sensitive info in SQL logs
86
+ 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:
87
+
88
+ ```ruby
89
+ Rails.application.configure do
90
+ config.lograge_sql.query_filter = ->(query) { query.gsub(/custom_regexp/, "[FILTERED]".freeze) }
91
+ end
92
+ ```
93
+
85
94
  ### Thread-safety
86
95
 
87
96
  [Depending on the web server in your project](https://github.com/steveklabnik/request_store#the-problem) you might benefit from improved thread-safety by adding [`request_store`](https://github.com/steveklabnik/request_store) to your Gemfile. It will be automatically picked up by `lograge-sql`.
@@ -4,17 +4,13 @@ module Lograge
4
4
  # Log subscriber to replace ActiveRecord's default one
5
5
  class ActiveRecordLogSubscriber < ActiveSupport::LogSubscriber
6
6
  # Every time there's an SQL query, stores it into the Thread.
7
- # They'll later be access from the RequestLogSubscriber.
7
+ # They'll later be accessed from the RequestLogSubscriber.
8
8
  def sql(event)
9
9
  increase_runtime_duration(event)
10
- return if event.payload[:name] == 'SCHEMA'
10
+ return unless valid?(event)
11
11
 
12
- # Only store SQL events if `event.duration` is greater than the configured +min_duration+
13
- # No need to check if +min_duration+ is present before as it defaults to 0
14
- return if event.duration.to_f.round(2) < Lograge::Sql.min_duration_ms.to_f
15
-
16
- Lograge::Sql.store[:lograge_sql_queries] ||= []
17
- Lograge::Sql.store[:lograge_sql_queries] << Lograge::Sql.extract_event.call(event)
12
+ filter_query(event)
13
+ store(event)
18
14
  end
19
15
 
20
16
  private
@@ -27,5 +23,27 @@ module Lograge
27
23
 
28
24
  ActiveRecord::LogSubscriber.runtime += event.duration
29
25
  end
26
+
27
+ def filter_query(event)
28
+ return unless Lograge::Sql.query_filter
29
+
30
+ # Filtering out sensitive info in SQL query if custom query_filter is provided
31
+ event.payload[:sql] = Lograge::Sql.query_filter.call(event.payload[:sql])
32
+ end
33
+
34
+ def valid?(event)
35
+ return false if event.payload[:name] == 'SCHEMA'
36
+
37
+ # Only store SQL events if `event.duration` is greater than the configured +min_duration+
38
+ # No need to check if +min_duration+ is present before as it defaults to 0
39
+ return false if event.duration.to_f.round(2) < Lograge::Sql.min_duration_ms.to_f
40
+
41
+ true
42
+ end
43
+
44
+ def store(event)
45
+ Lograge::Sql.store[:lograge_sql_queries] ||= []
46
+ Lograge::Sql.store[:lograge_sql_queries] << Lograge::Sql.extract_event.call(event)
47
+ end
30
48
  end
31
49
  end
@@ -3,6 +3,6 @@
3
3
  module Lograge
4
4
  module Sql
5
5
  # Gem version
6
- VERSION = '2.2.0'
6
+ VERSION = '2.3.0'
7
7
  end
8
8
  end
data/lib/lograge/sql.rb CHANGED
@@ -13,12 +13,15 @@ module Lograge
13
13
  attr_accessor :extract_event
14
14
  # Filter SQL events by duration
15
15
  attr_accessor :min_duration_ms
16
+ # Filter SQL query
17
+ attr_accessor :query_filter
16
18
 
17
19
  # Initialise configuration with fallback to default values
18
20
  def setup(config)
19
21
  Lograge::Sql.formatter = config.formatter || default_formatter
20
22
  Lograge::Sql.extract_event = config.extract_event || default_extract_event
21
23
  Lograge::Sql.min_duration_ms = config.min_duration_ms || 0
24
+ Lograge::Sql.query_filter = config.query_filter
22
25
 
23
26
  # Disable existing ActiveRecord logging
24
27
  unsubscribe_log_subscribers unless config.keep_default_active_record_log
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lograge-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Giuffrida
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-10 00:00:00.000000000 Z
11
+ date: 2023-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord