rails-flog 1.5.0 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +25 -0
- data/.travis.yml +9 -0
- data/Gemfile +2 -0
- data/README.md +9 -2
- data/Rakefile +7 -5
- data/gemfiles/rails_4_2_x.gemfile +4 -3
- data/gemfiles/rails_5_0_x.gemfile +5 -3
- data/gemfiles/rails_5_1_x.gemfile +5 -3
- data/gemfiles/rails_5_2_x.gemfile +5 -3
- data/lib/flog.rb +7 -5
- data/lib/flog/configuration.rb +27 -10
- data/lib/flog/params_formattable.rb +53 -44
- data/lib/flog/payload_value_shuntable.rb +19 -6
- data/lib/flog/sql_formattable.rb +39 -35
- data/lib/flog/status.rb +37 -32
- data/lib/flog/version.rb +3 -1
- data/rails-flog.gemspec +21 -19
- data/test/test_helper.rb +8 -6
- data/test/unit/params_formattable_test.rb +75 -55
- data/test/unit/payload_value_shuntable_test.rb +19 -17
- data/test/unit/sql_formattable_test.rb +111 -86
- data/test/unit/status_test.rb +41 -36
- metadata +32 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7bba2152bd153f016c66ba66cebcb387fb2e8578e1fa1ff659f885d48eba0e0a
|
4
|
+
data.tar.gz: 1b6784cf98e02d66808f5f087e003dd8af2f69f272845167f2dfc344ab1d0742
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f44d36f195e11be261a0a5d7fe0c763b09f24aaafebc23b924a879da39b1f67b6bb2d68e738206f848e4db891db32089d99847f92f5326fff0471ca8fac5687
|
7
|
+
data.tar.gz: 7e2bc4c9af5f6134f6ef38eb72484fee8305611e25366fa9283933abf3702455b424cb0877073c89aae26ad86aa74a424ab1faa65bf90ddac03cfba719c8bc83
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Current style is easy to confirm SQL.
|
2
|
+
Layout/SpaceBeforeComma:
|
3
|
+
Exclude:
|
4
|
+
- 'test/unit/sql_formattable_test.rb'
|
5
|
+
|
6
|
+
Metrics/AbcSize:
|
7
|
+
Exclude:
|
8
|
+
- 'test/**/*'
|
9
|
+
|
10
|
+
# 80 is too restrict.
|
11
|
+
Metrics/LineLength:
|
12
|
+
Max: 120
|
13
|
+
|
14
|
+
Metrics/MethodLength:
|
15
|
+
Exclude:
|
16
|
+
- 'test/**/*'
|
17
|
+
|
18
|
+
# This is test code not production.
|
19
|
+
Security/Eval:
|
20
|
+
Exclude:
|
21
|
+
- 'test/unit/params_formattable_test.rb'
|
22
|
+
|
23
|
+
# method having '?' suffix should return boolean, double negation is very useful.
|
24
|
+
Style/DoubleNegation:
|
25
|
+
Enabled: false
|
data/.travis.yml
CHANGED
@@ -7,8 +7,17 @@ rvm:
|
|
7
7
|
- 2.3
|
8
8
|
- 2.4
|
9
9
|
- 2.5
|
10
|
+
- 2.6
|
10
11
|
gemfile:
|
11
12
|
- gemfiles/rails_4_2_x.gemfile
|
12
13
|
- gemfiles/rails_5_0_x.gemfile
|
13
14
|
- gemfiles/rails_5_1_x.gemfile
|
14
15
|
- gemfiles/rails_5_2_x.gemfile
|
16
|
+
# FIXME: will remove on dropping Rails 4.2 support
|
17
|
+
matrix:
|
18
|
+
exclude:
|
19
|
+
- gemfile: gemfiles/rails_4_2_x.gemfile
|
20
|
+
rvm: 2.6
|
21
|
+
before_install:
|
22
|
+
- gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
|
23
|
+
- gem install bundler -v '< 2'
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -101,6 +101,10 @@ Flog.configure do |config|
|
|
101
101
|
config.params_key_count_threshold = 2
|
102
102
|
# If this value is true, nested Hash parameter is formatted coercively in any situation
|
103
103
|
config.force_on_nested_params = false
|
104
|
+
# If this value is true, not format query
|
105
|
+
config.ignore_query = true
|
106
|
+
# If this value is true, not format parameters
|
107
|
+
config.ignore_params = true
|
104
108
|
end
|
105
109
|
```
|
106
110
|
|
@@ -112,6 +116,8 @@ end
|
|
112
116
|
|query_duration_threshold |float |0.0 |
|
113
117
|
|params_key_count_threshold |integer |1 |
|
114
118
|
|force_on_nested_params |boolean |true |
|
119
|
+
|ignore_query |boolean |false |
|
120
|
+
|ignore_params |boolean |false |
|
115
121
|
|
116
122
|
## Disable temporary
|
117
123
|
|
@@ -126,7 +132,7 @@ Priority of this feature is higher than configurations.
|
|
126
132
|
|
127
133
|
## Supported versions
|
128
134
|
|
129
|
-
- Ruby: 2.3.x, 2.4.x, 2.5.x
|
135
|
+
- Ruby: 2.3.x, 2.4.x, 2.5.x, 2.6.x
|
130
136
|
- Rails: 4.2.x, 5.0.x, 5.1.x, 5.2.x
|
131
137
|
|
132
138
|
## Contributing
|
@@ -148,4 +154,5 @@ Priority of this feature is higher than configurations.
|
|
148
154
|
- v1.3.2 (2014-04-28 JST): Confirm with Rails 4.1
|
149
155
|
- v1.3.3 (2015-11-16 JST): Confirm with Rails 4.2
|
150
156
|
- v1.4.0 (2017-10-01 JST): Compatible for Rails 5.0 and Rails 5.1, and dropped Ruby 1.9.3 support
|
151
|
-
- v1.5.0 (2018-12-19 JST): Add `
|
157
|
+
- v1.5.0 (2018-12-19 JST): Add `sql_indent` and `sql_in_values_num` options, and dropped eol rails versions.
|
158
|
+
- v1.6.0 (2019-07-26 JST): Add `ignore_query` and `ignore_params` options (Thanks @mothule)
|
data/Rakefile
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
|
2
|
-
require "rake/testtask"
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
task default: :test
|
5
7
|
|
6
8
|
Rake::TestTask.new do |t|
|
7
|
-
t.libs <<
|
8
|
-
t.test_files = Dir.glob(
|
9
|
+
t.libs << 'test'
|
10
|
+
t.test_files = Dir.glob('test/unit/*_test.rb')
|
9
11
|
end
|
data/lib/flog.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'flog/version'
|
4
|
+
require 'flog/status'
|
5
|
+
require 'flog/configuration'
|
6
|
+
require 'flog/sql_formattable'
|
7
|
+
require 'flog/params_formattable'
|
data/lib/flog/configuration.rb
CHANGED
@@ -1,10 +1,15 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'anbt-sql-formatter/rule'
|
4
|
+
|
5
|
+
# Flog is root module of this gem
|
3
6
|
module Flog
|
4
7
|
ONELINE_IN_VALUES_NUM = ::AnbtSql::Rule::ONELINE_IN_VALUES_NUM
|
5
8
|
|
9
|
+
# Configuration of this gem.
|
10
|
+
# Call `configure` to setup.
|
6
11
|
class Configuration
|
7
|
-
attr_writer :ignore_cached_query, :force_on_nested_params
|
12
|
+
attr_writer :ignore_cached_query, :force_on_nested_params, :ignore_query, :ignore_params
|
8
13
|
attr_accessor :query_duration_threshold, :params_key_count_threshold, :sql_indent, :sql_in_values_num
|
9
14
|
|
10
15
|
def initialize
|
@@ -14,24 +19,36 @@ module Flog
|
|
14
19
|
@force_on_nested_params = true
|
15
20
|
@sql_indent = "\t"
|
16
21
|
@sql_in_values_num = 1
|
22
|
+
@ignore_query = false
|
23
|
+
@ignore_params = false
|
17
24
|
end
|
18
25
|
|
19
26
|
def ignore_cached_query?
|
20
|
-
!!@ignore_cached_query
|
27
|
+
!!@ignore_cached_query || @ignore_query
|
21
28
|
end
|
22
29
|
|
23
30
|
def force_on_nested_params?
|
24
31
|
!!@force_on_nested_params
|
25
32
|
end
|
26
|
-
end
|
27
33
|
|
28
|
-
|
29
|
-
|
34
|
+
def ignore_query?
|
35
|
+
!!@ignore_query
|
36
|
+
end
|
37
|
+
|
38
|
+
def ignore_params?
|
39
|
+
!!@ignore_params
|
40
|
+
end
|
30
41
|
end
|
31
42
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
43
|
+
class << self
|
44
|
+
def config
|
45
|
+
@config ||= Flog::Configuration.new
|
46
|
+
end
|
47
|
+
|
48
|
+
def configure
|
49
|
+
cfg = Flog::Configuration.new
|
50
|
+
yield(cfg) if block_given?
|
51
|
+
@config = cfg
|
52
|
+
end
|
36
53
|
end
|
37
54
|
end
|
@@ -1,61 +1,70 @@
|
|
1
|
-
|
2
|
-
require "awesome_print"
|
3
|
-
require "flog/payload_value_shuntable"
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
return super(event) unless formattable?(event)
|
3
|
+
require 'action_controller/log_subscriber'
|
4
|
+
require 'awesome_print'
|
5
|
+
require 'flog/payload_value_shuntable'
|
9
6
|
|
10
|
-
|
7
|
+
module Flog
|
8
|
+
# Overrides `inspect` method for formatting itself
|
9
|
+
module ParamsInspectOverridable
|
10
|
+
def inspect
|
11
|
+
"\n#{ai(plain: !ActionController::LogSubscriber.colorize_logging)}"
|
12
|
+
end
|
13
|
+
end
|
11
14
|
|
12
|
-
|
13
|
-
|
15
|
+
# Overrides `except` method for formatting ecepted params.
|
16
|
+
module ParamsExceptOverridable
|
17
|
+
def except(*keys)
|
18
|
+
excepted = super(*keys)
|
19
|
+
excepted.singleton_class.prepend(ParamsInspectOverridable)
|
20
|
+
excepted
|
14
21
|
end
|
15
22
|
end
|
16
23
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
class << excepted
|
28
|
-
def inspect
|
29
|
-
"\n#{ai(plain: !ActionController::LogSubscriber.colorize_logging)}"
|
30
|
-
end
|
31
|
-
end
|
32
|
-
excepted
|
24
|
+
# ParamsFormattable enables to format request parameters in log.
|
25
|
+
module ParamsFormattable
|
26
|
+
include Flog::PayloadValueShuntable
|
27
|
+
def start_processing(event)
|
28
|
+
return super(event) unless formattable?(event)
|
29
|
+
|
30
|
+
replaced = replace_params(event.payload[:params])
|
31
|
+
|
32
|
+
shunt_payload_value(event.payload, :params, replaced) do
|
33
|
+
super(event)
|
33
34
|
end
|
34
35
|
end
|
35
|
-
replaced
|
36
|
-
end
|
37
36
|
|
38
|
-
|
39
|
-
return false unless Flog::Status.params_formattable?
|
37
|
+
private
|
40
38
|
|
41
|
-
|
39
|
+
def replace_params(params)
|
40
|
+
return params if params.empty? || !params.respond_to?(:ai)
|
42
41
|
|
43
|
-
|
44
|
-
|
42
|
+
replaced = params.dup
|
43
|
+
replaced.singleton_class.prepend(ParamsExceptOverridable)
|
44
|
+
replaced
|
45
|
+
end
|
45
46
|
|
46
|
-
|
47
|
-
|
47
|
+
def formattable?(event)
|
48
|
+
return false if Flog.config.ignore_params?
|
49
|
+
return false unless Flog::Status.params_formattable?
|
48
50
|
|
49
|
-
|
50
|
-
end
|
51
|
+
return true if force_format_by_nested_params?(event)
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
53
|
+
key_count_over?(event)
|
54
|
+
end
|
55
|
+
|
56
|
+
def force_format_by_nested_params?(event)
|
57
|
+
return false unless Flog.config.force_on_nested_params?
|
58
|
+
|
59
|
+
event.payload[:params].values.any? { |value| value.is_a?(Hash) }
|
60
|
+
end
|
61
|
+
|
62
|
+
def key_count_over?(event)
|
63
|
+
threshold = Flog.config.params_key_count_threshold.to_i
|
64
|
+
params = event.payload[:params].except(*ActionController::LogSubscriber::INTERNAL_PARAMS)
|
65
|
+
params.keys.size > threshold
|
66
|
+
end
|
56
67
|
end
|
57
68
|
end
|
58
69
|
|
59
|
-
|
60
|
-
prepend Flog::ParamsFormattable
|
61
|
-
end
|
70
|
+
ActionController::LogSubscriber.prepend(Flog::ParamsFormattable)
|
@@ -1,20 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Flog
|
4
|
+
# PayloadValueShuntable enables to shunt value in payload
|
2
5
|
module PayloadValueShuntable
|
3
6
|
def shunt_payload_value(payload, key, temp_value, &block)
|
4
7
|
return unless block
|
5
8
|
|
6
|
-
|
9
|
+
if payload.key?(key)
|
10
|
+
shunt_if_key_already_exists(payload, key, temp_value, block)
|
11
|
+
else
|
12
|
+
shunt_if_key_not_exist(payload, key, temp_value, block)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def shunt_if_key_already_exists(payload, key, temp_value, block)
|
7
17
|
base_value = payload[key]
|
8
18
|
begin
|
9
19
|
payload[key] = temp_value
|
10
20
|
block.call
|
11
21
|
ensure
|
12
|
-
|
13
|
-
payload[key] = base_value
|
14
|
-
else
|
15
|
-
payload.delete(key)
|
16
|
-
end
|
22
|
+
payload[key] = base_value
|
17
23
|
end
|
18
24
|
end
|
25
|
+
|
26
|
+
def shunt_if_key_not_exist(payload, key, temp_value, block)
|
27
|
+
payload[key] = temp_value
|
28
|
+
block.call
|
29
|
+
ensure
|
30
|
+
payload.delete(key)
|
31
|
+
end
|
19
32
|
end
|
20
33
|
end
|
data/lib/flog/sql_formattable.rb
CHANGED
@@ -1,52 +1,56 @@
|
|
1
|
-
|
2
|
-
require "flog/payload_value_shuntable"
|
1
|
+
# frozen_string_literal: true
|
3
2
|
|
4
|
-
|
5
|
-
|
3
|
+
require 'active_record/log_subscriber'
|
4
|
+
require 'flog/payload_value_shuntable'
|
6
5
|
|
7
|
-
|
8
|
-
|
6
|
+
module Flog
|
7
|
+
# SqlFormattable enables to format SQL log
|
8
|
+
module SqlFormattable
|
9
|
+
include Flog::PayloadValueShuntable
|
9
10
|
|
10
|
-
|
11
|
+
def sql(event)
|
12
|
+
return super(event) unless formattable?(event)
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
+
formatted = format_sql(event.payload[:sql])
|
15
|
+
|
16
|
+
shunt_payload_value(event.payload, :sql, "\n#{Flog.config.sql_indent}#{formatted}") do
|
17
|
+
super(event)
|
18
|
+
end
|
14
19
|
end
|
15
|
-
end
|
16
20
|
|
17
|
-
|
21
|
+
private
|
18
22
|
|
19
|
-
|
20
|
-
|
23
|
+
def format_sql(sql)
|
24
|
+
return sql if sql.blank?
|
21
25
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
require 'anbt-sql-formatter/formatter'
|
27
|
+
rule = AnbtSql::Rule.new
|
28
|
+
rule.keyword = AnbtSql::Rule::KEYWORD_UPPER_CASE
|
29
|
+
rule.indent_string = Flog.config.sql_indent
|
30
|
+
rule.in_values_num = Flog.config.sql_in_values_num
|
31
|
+
%w[count sum].each do |function_name|
|
32
|
+
rule.function_names << function_name
|
33
|
+
end
|
34
|
+
AnbtSql::Formatter.new(rule).format(sql.squeeze(' '))
|
29
35
|
end
|
30
|
-
AnbtSql::Formatter.new(rule).format(sql.squeeze(" "))
|
31
|
-
end
|
32
36
|
|
33
|
-
|
34
|
-
|
37
|
+
def formattable?(event)
|
38
|
+
return false if Flog.config.ignore_query?
|
39
|
+
return false unless Flog::Status.sql_formattable?
|
35
40
|
|
36
|
-
|
41
|
+
return false if ignore_by_cached_query?(event)
|
37
42
|
|
38
|
-
|
39
|
-
|
43
|
+
duration_over?(event)
|
44
|
+
end
|
40
45
|
|
41
|
-
|
42
|
-
|
43
|
-
|
46
|
+
def ignore_by_cached_query?(event)
|
47
|
+
(event.payload[:name] == 'CACHE' || event.payload[:cached]) && Flog.config.ignore_cached_query?
|
48
|
+
end
|
44
49
|
|
45
|
-
|
46
|
-
|
50
|
+
def duration_over?(event)
|
51
|
+
event.duration >= Flog.config.query_duration_threshold.to_f
|
52
|
+
end
|
47
53
|
end
|
48
54
|
end
|
49
55
|
|
50
|
-
|
51
|
-
prepend Flog::SqlFormattable
|
52
|
-
end
|
56
|
+
ActiveRecord::LogSubscriber.prepend(Flog::SqlFormattable)
|