rails-flog 1.3.2 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.gitignore +2 -0
- data/.rubocop.yml +25 -0
- data/.travis.yml +25 -6
- data/Gemfile +2 -0
- data/README.md +13 -2
- data/Rakefile +7 -5
- data/gemfiles/rails_4_2_x.gemfile +8 -0
- data/gemfiles/rails_5_0_x.gemfile +8 -0
- data/gemfiles/rails_5_1_x.gemfile +8 -0
- data/gemfiles/rails_5_2_x.gemfile +8 -0
- data/gemfiles/rails_6_0_x.gemfile +8 -0
- data/lib/flog.rb +6 -6
- data/lib/flog/configuration.rb +33 -9
- data/lib/flog/params_formattable.rb +55 -45
- data/lib/flog/payload_value_shuntable.rb +19 -7
- data/lib/flog/sql_formattable.rb +41 -33
- data/lib/flog/status.rb +40 -24
- data/lib/flog/version.rb +3 -2
- data/rails-flog.gemspec +21 -19
- data/test/test_helper.rb +39 -26
- data/test/unit/params_formattable_test.rb +89 -50
- data/test/unit/payload_value_shuntable_test.rb +19 -18
- data/test/unit/sql_formattable_test.rb +144 -55
- data/test/unit/status_test.rb +96 -82
- metadata +57 -41
- data/gemfiles/rails_3_2_x.gemfile +0 -7
- data/gemfiles/rails_4_0_x.gemfile +0 -7
- data/gemfiles/rails_4_1_x.gemfile +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ce762dd557d5f1894942ea6e1c2ce4a8c368d270881fb6ef83a9565330613bde
|
4
|
+
data.tar.gz: 5df9080511dcc817399c3080fdf7278204986f280c54c3b27945cfa51a8be222
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b92c5ae0ae5aaaf554f5b94bfda53bf90cde1e9ff44f0a31510fdfa08fd289db89b3e531635d62247fe98f2cf6db9f1a0b3ebf2d8c43be5bd085fc1f4f95337b
|
7
|
+
data.tar.gz: 4d22a90f56f4ff337c08bb18bdfda1e5ab0a11f43f0dd82dc7d1a5ffcf973b3cdb82ace1db3d8fe8ca66a21f5dfa3d5f88b21209f8c2834e9a5261608ccc382d
|
data/.gitignore
CHANGED
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
@@ -1,9 +1,28 @@
|
|
1
1
|
language: ruby
|
2
|
+
sudo: false
|
3
|
+
branches:
|
4
|
+
only:
|
5
|
+
- master
|
2
6
|
rvm:
|
3
|
-
-
|
4
|
-
- 2.
|
5
|
-
- 2.
|
7
|
+
- 2.3
|
8
|
+
- 2.4
|
9
|
+
- 2.5
|
10
|
+
- 2.6
|
6
11
|
gemfile:
|
7
|
-
- gemfiles/
|
8
|
-
- gemfiles/
|
9
|
-
- gemfiles/
|
12
|
+
- gemfiles/rails_4_2_x.gemfile
|
13
|
+
- gemfiles/rails_5_0_x.gemfile
|
14
|
+
- gemfiles/rails_5_1_x.gemfile
|
15
|
+
- gemfiles/rails_5_2_x.gemfile
|
16
|
+
- gemfiles/rails_6_0_x.gemfile
|
17
|
+
matrix:
|
18
|
+
exclude:
|
19
|
+
# FIXME: will remove on dropping Rails 4.2 support
|
20
|
+
- gemfile: gemfiles/rails_4_2_x.gemfile
|
21
|
+
rvm: 2.6
|
22
|
+
- gemfile: gemfiles/rails_6_0_x.gemfile
|
23
|
+
rvm: 2.3
|
24
|
+
- gemfile: gemfiles/rails_6_0_x.gemfile
|
25
|
+
rvm: 2.4
|
26
|
+
before_install:
|
27
|
+
- gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
|
28
|
+
- 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,8 +132,8 @@ Priority of this feature is higher than configurations.
|
|
126
132
|
|
127
133
|
## Supported versions
|
128
134
|
|
129
|
-
- Ruby:
|
130
|
-
- Rails:
|
135
|
+
- Ruby: 2.3.x, 2.4.x, 2.5.x, 2.6.x
|
136
|
+
- Rails: 4.2.x, 5.0.x, 5.1.x, 5.2.x, 6.0.x
|
131
137
|
|
132
138
|
## Contributing
|
133
139
|
|
@@ -146,3 +152,8 @@ Priority of this feature is higher than configurations.
|
|
146
152
|
- v1.3.0 (2014-01-26 JST): Add configuration
|
147
153
|
- v1.3.1 (2014-01-27 JST): Refactored
|
148
154
|
- v1.3.2 (2014-04-28 JST): Confirm with Rails 4.1
|
155
|
+
- v1.3.3 (2015-11-16 JST): Confirm with Rails 4.2
|
156
|
+
- v1.4.0 (2017-10-01 JST): Compatible for Rails 5.0 and Rails 5.1, and dropped Ruby 1.9.3 support
|
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)
|
159
|
+
- v1.6.1 (2021-01-13 JST): Use amazing_print instead of awesome_print
|
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,7 +1,7 @@
|
|
1
|
-
#
|
2
|
-
require "flog/version"
|
3
|
-
require "flog/status"
|
4
|
-
require "flog/configuration"
|
5
|
-
require "flog/sql_formattable"
|
6
|
-
require "flog/params_formattable"
|
1
|
+
# frozen_string_literal: true
|
7
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,30 +1,54 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'anbt-sql-formatter/rule'
|
4
|
+
|
5
|
+
# Flog is root module of this gem
|
2
6
|
module Flog
|
7
|
+
ONELINE_IN_VALUES_NUM = ::AnbtSql::Rule::ONELINE_IN_VALUES_NUM
|
8
|
+
|
9
|
+
# Configuration of this gem.
|
10
|
+
# Call `configure` to setup.
|
3
11
|
class Configuration
|
4
|
-
attr_writer :ignore_cached_query, :force_on_nested_params
|
5
|
-
attr_accessor :query_duration_threshold, :params_key_count_threshold
|
12
|
+
attr_writer :ignore_cached_query, :force_on_nested_params, :ignore_query, :ignore_params
|
13
|
+
attr_accessor :query_duration_threshold, :params_key_count_threshold, :sql_indent, :sql_in_values_num
|
6
14
|
|
7
15
|
def initialize
|
8
16
|
@ignore_cached_query = true
|
9
17
|
@query_duration_threshold = 0.0
|
10
18
|
@params_key_count_threshold = 1
|
11
19
|
@force_on_nested_params = true
|
20
|
+
@sql_indent = "\t"
|
21
|
+
@sql_in_values_num = 1
|
22
|
+
@ignore_query = false
|
23
|
+
@ignore_params = false
|
12
24
|
end
|
13
25
|
|
14
26
|
def ignore_cached_query?
|
15
|
-
!!@ignore_cached_query
|
27
|
+
!!@ignore_cached_query || @ignore_query
|
16
28
|
end
|
17
29
|
|
18
30
|
def force_on_nested_params?
|
19
31
|
!!@force_on_nested_params
|
20
32
|
end
|
21
|
-
end
|
22
33
|
|
23
|
-
|
24
|
-
|
34
|
+
def ignore_query?
|
35
|
+
!!@ignore_query
|
36
|
+
end
|
37
|
+
|
38
|
+
def ignore_params?
|
39
|
+
!!@ignore_params
|
40
|
+
end
|
25
41
|
end
|
26
42
|
|
27
|
-
|
28
|
-
|
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
|
29
53
|
end
|
30
54
|
end
|
@@ -1,60 +1,70 @@
|
|
1
|
-
#
|
2
|
-
require "action_controller/log_subscriber"
|
3
|
-
require "awesome_print"
|
4
|
-
require "flog/payload_value_shuntable"
|
1
|
+
# frozen_string_literal: true
|
5
2
|
|
6
|
-
|
7
|
-
|
3
|
+
require 'action_controller/log_subscriber'
|
4
|
+
require 'amazing_print'
|
5
|
+
require 'flog/payload_value_shuntable'
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
13
14
|
|
14
|
-
|
15
|
-
|
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
|
16
21
|
end
|
17
22
|
end
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
excepted = original_except(*keys)
|
30
|
-
class << excepted
|
31
|
-
def inspect
|
32
|
-
"\n#{ai(plain: !ActionController::LogSubscriber.colorize_logging)}"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
excepted
|
23
|
+
|
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)
|
36
34
|
end
|
37
35
|
end
|
38
|
-
replaced
|
39
|
-
end
|
40
36
|
|
41
|
-
|
42
|
-
return false unless Flog::Status.params_formattable?
|
37
|
+
private
|
43
38
|
|
44
|
-
|
39
|
+
def replace_params(params)
|
40
|
+
return params if params.empty? || !params.respond_to?(:ai)
|
45
41
|
|
46
|
-
|
47
|
-
|
42
|
+
replaced = params.dup
|
43
|
+
replaced.singleton_class.prepend(ParamsExceptOverridable)
|
44
|
+
replaced
|
45
|
+
end
|
48
46
|
|
49
|
-
|
50
|
-
|
47
|
+
def formattable?(event)
|
48
|
+
return false if Flog.config.ignore_params?
|
49
|
+
return false unless Flog::Status.params_formattable?
|
51
50
|
|
52
|
-
|
53
|
-
|
51
|
+
return true if force_format_by_nested_params?(event)
|
52
|
+
|
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?
|
54
58
|
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
59
67
|
end
|
60
68
|
end
|
69
|
+
|
70
|
+
ActionController::LogSubscriber.prepend(Flog::ParamsFormattable)
|
@@ -1,21 +1,33 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
2
3
|
module Flog
|
4
|
+
# PayloadValueShuntable enables to shunt value in payload
|
3
5
|
module PayloadValueShuntable
|
4
6
|
def shunt_payload_value(payload, key, temp_value, &block)
|
5
7
|
return unless block
|
6
8
|
|
7
|
-
|
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)
|
8
17
|
base_value = payload[key]
|
9
18
|
begin
|
10
19
|
payload[key] = temp_value
|
11
20
|
block.call
|
12
21
|
ensure
|
13
|
-
|
14
|
-
payload[key] = base_value
|
15
|
-
else
|
16
|
-
payload.delete(key)
|
17
|
-
end
|
22
|
+
payload[key] = base_value
|
18
23
|
end
|
19
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
|
20
32
|
end
|
21
33
|
end
|
data/lib/flog/sql_formattable.rb
CHANGED
@@ -1,48 +1,56 @@
|
|
1
|
-
#
|
2
|
-
require "active_record/log_subscriber"
|
3
|
-
require "flog/payload_value_shuntable"
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
3
|
+
require 'active_record/log_subscriber'
|
4
|
+
require 'flog/payload_value_shuntable'
|
7
5
|
|
8
|
-
|
9
|
-
|
6
|
+
module Flog
|
7
|
+
# SqlFormattable enables to format SQL log
|
8
|
+
module SqlFormattable
|
9
|
+
include Flog::PayloadValueShuntable
|
10
10
|
|
11
|
-
|
11
|
+
def sql(event)
|
12
|
+
return super(event) unless formattable?(event)
|
12
13
|
|
13
|
-
|
14
|
-
|
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
|
15
19
|
end
|
16
|
-
end
|
17
|
-
alias_method_chain :sql, :flog
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
|
21
|
+
private
|
22
|
+
|
23
|
+
def format_sql(sql)
|
24
|
+
return sql if sql.blank?
|
22
25
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
rule.
|
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(' '))
|
28
35
|
end
|
29
|
-
rule.indent_string = "\t"
|
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
|
55
|
+
|
56
|
+
ActiveRecord::LogSubscriber.prepend(Flog::SqlFormattable)
|