lograge-sql 2.1.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: '0794c5325c6cc647ab9e133b0ac1ebfee558311f12b26861c0776866a6cc27fb'
4
- data.tar.gz: b2bae9007ad19926f3ba0fed85bb6a965b942e70a537ed50b4230f071834af8f
3
+ metadata.gz: 8eeabf2a3a00c53b0d7483d6dd84659f2434eb90f0741183370a1559bf9eab1d
4
+ data.tar.gz: da0adc7fa61626a386b563d622234c02971da52f310e687f313b95cf074a6434
5
5
  SHA512:
6
- metadata.gz: 72222b28288cc38a14533c3a3babe408aa2d834dc19ba7f323ac9f7aeca167d55ad7ba2fc3af777e35fa643f7850901378282ee6865bbe4246edc410dff68ad2
7
- data.tar.gz: f4a956079f4562b3f24737adc851fb78ca41d70b6e8555e3eda42c436b0c88af28a109d3db4e54c2b36c1d0d21ef67a8c344bcb5a5b22b7884d2b9268fbca0cd
6
+ metadata.gz: fcc84f05b613c82c0b00d9747d6a07976c324e1acef7a6b7b7e98987adb1b319ffcbfe92752682b579ed0eaeb00651431ec9046119dccac8df66a0750a050924
7
+ data.tar.gz: 333ff533242871fcbb4ea2130da71fbdf232625c32b206634fca0f374b7e96e1b2d0b66c0dcccb7ec2799b00b015bcfcde953e54732e5d42d84f4cec6cbb12b4
data/README.md CHANGED
@@ -29,7 +29,23 @@ By default, Lograge::Sql disables default logging on ActiveRecord. To preserve d
29
29
  config.lograge_sql.keep_default_active_record_log = true
30
30
  ```
31
31
 
32
- ## Customization
32
+ ## Configuration
33
+
34
+ ### Minimum query duration threshold
35
+
36
+ By default, `lograge-sql` stores all queries, but you can set a `min_duration_ms` config.
37
+ When you do so, only queries that run for AT LEAST `min_duration_ms` milliseconds will be logged, and all others will be ignored.
38
+ This can be really helpful if you want to detect `Slow SQL queries`.
39
+
40
+ ```ruby
41
+ # config/initializers/lograge.rb
42
+ Rails.application.configure do
43
+ # Defaults is zero
44
+ config.lograge_sql.min_duration_ms = 5000 # milliseconds
45
+ end
46
+ ```
47
+
48
+ ### Output Customization
33
49
 
34
50
  By default, the format is a string concatenation of the query name, the query duration and the query itself joined by `\n` newline:
35
51
 
@@ -66,19 +82,16 @@ Rails.application.configure do
66
82
  end
67
83
  ```
68
84
 
69
- `Lograge-sql` only stores any events by `min_duration_ms` condition.
70
- It very helpful if you want to detect `Slow SQL queries`
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:
71
87
 
72
88
  ```ruby
73
- # config/initializers/lograge.rb
74
89
  Rails.application.configure do
75
- # Set limitted of SQL duration if you want to filter (unit: milliseconds)
76
- # Defaults is zero
77
- config.lograge_sql.min_duration_ms = 5000
90
+ config.lograge_sql.query_filter = ->(query) { query.gsub(/custom_regexp/, "[FILTERED]".freeze) }
78
91
  end
79
92
  ```
80
93
 
81
- #### Thread-safety
94
+ ### Thread-safety
82
95
 
83
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`.
84
97
 
@@ -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.1.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.1.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-02-25 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
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '5'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '7.1'
22
+ version: '7.2'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '5'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '7.1'
32
+ version: '7.2'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: lograge
35
35
  requirement: !ruby/object:Gem::Requirement