rails-flog 1.2.0 → 1.3.0
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 +4 -4
- data/.travis.yml +1 -1
- data/README.md +29 -1
- data/lib/flog.rb +1 -0
- data/lib/flog/configuration.rb +30 -0
- data/lib/flog/params_formattable.rb +14 -2
- data/lib/flog/sql_formattable.rb +11 -1
- data/lib/flog/version.rb +1 -1
- data/test/unit/params_formattable_test.rb +44 -0
- data/test/unit/sql_formattable_test.rb +58 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7573566571c52550bd3a1fdaf222159101caabb3
|
4
|
+
data.tar.gz: 3e19ae5c1197155f93264ac7e28614bbd22596b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1a196d239d1af127143e19eefb1e8526de7d32684baab1b301cbd4d16aacde126ef35235103cb0546b5fe6a17d5b3c4d207234c6de0d25e467032a964564e206
|
7
|
+
data.tar.gz: b0b4ac38af5542776ea16fe73aa28efa774e2bee3b21618044c91c25de4d54ab13541099722039f4411f20dab9417874f7a22f92c36a49502c1b60ec661f116e
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -87,6 +87,32 @@ Or install it yourself as:
|
|
87
87
|
|
88
88
|
Just install.
|
89
89
|
|
90
|
+
## Configuration
|
91
|
+
|
92
|
+
Configure in your environment file (ex. `config/environments/development.rb`)
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
Flog.configure do |config|
|
96
|
+
# If this value is true, not format on cached query
|
97
|
+
config.ignore_cached_query = false
|
98
|
+
# If query duration is under this value, not format
|
99
|
+
config.query_duration_threshold = 2.0
|
100
|
+
# If key count of parameters is under this value, not format
|
101
|
+
config.params_key_count_threshold = 2
|
102
|
+
# If this value is true, force format in any situation
|
103
|
+
config.force_on_nested_params = false
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
### Default value
|
108
|
+
|
109
|
+
|Attribute |Data type |Default value |
|
110
|
+
|:--------------------------|:---------|:-------------|
|
111
|
+
|ignore_cached_query |boolean |true |
|
112
|
+
|query_duration_threshold |float |0.0 |
|
113
|
+
|params_key_count_threshold |integer |1 |
|
114
|
+
|force_on_nested_params |boolean |true |
|
115
|
+
|
90
116
|
## Disable temporary
|
91
117
|
|
92
118
|
If you put a file to `<rails_app>/tmp` direcotry, `rails-flog` will disable format.
|
@@ -99,7 +125,7 @@ If you put a file to `<rails_app>/tmp` direcotry, `rails-flog` will disable form
|
|
99
125
|
|
100
126
|
## Supported versions
|
101
127
|
|
102
|
-
- Ruby: 1.9.3, 2.0.0
|
128
|
+
- Ruby: 1.9.3, 2.0.0, 2.1.0
|
103
129
|
- Rails: 3.2.x, 4.0.x
|
104
130
|
|
105
131
|
## Contributing
|
@@ -116,3 +142,5 @@ If you put a file to `<rails_app>/tmp` direcotry, `rails-flog` will disable form
|
|
116
142
|
- v1.1.0 (2013-12-02 JST): Add feature that disables format by no-flog.txt
|
117
143
|
- v1.1.1 (2013-12-06 JST): Change to alias_method_change from alias for method aliasing
|
118
144
|
- v1.2.0 (2014-01-14 JST): Add feature that disables format partially by no-flog-sql.txt and no-flog-params.txt
|
145
|
+
- v1.3.0 (2014-01-26 JST): Add configuration
|
146
|
+
|
data/lib/flog.rb
CHANGED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module Flog
|
3
|
+
class Configuration
|
4
|
+
attr_writer :ignore_cached_query, :force_on_nested_params
|
5
|
+
attr_accessor :query_duration_threshold, :params_key_count_threshold
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@ignore_cached_query = true
|
9
|
+
@query_duration_threshold = 0.0
|
10
|
+
@params_key_count_threshold = 1
|
11
|
+
@force_on_nested_params = true
|
12
|
+
end
|
13
|
+
|
14
|
+
def ignore_cached_query?
|
15
|
+
!!@ignore_cached_query
|
16
|
+
end
|
17
|
+
|
18
|
+
def force_on_nested_params?
|
19
|
+
!!@force_on_nested_params
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.config
|
24
|
+
@@config ||= Flog::Configuration.new
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.configure
|
28
|
+
yield(config) if block_given?
|
29
|
+
end
|
30
|
+
end
|
@@ -2,13 +2,12 @@
|
|
2
2
|
require "action_controller/log_subscriber"
|
3
3
|
require "awesome_print"
|
4
4
|
require "flog/payload_value_shuntable"
|
5
|
-
require "flog/status"
|
6
5
|
|
7
6
|
class ActionController::LogSubscriber
|
8
7
|
include Flog::PayloadValueShuntable
|
9
8
|
|
10
9
|
def start_processing_with_flog(event)
|
11
|
-
return start_processing_without_flog(event) unless
|
10
|
+
return start_processing_without_flog(event) unless formattable?(event)
|
12
11
|
|
13
12
|
replaced = replace_params(event.payload[:params])
|
14
13
|
|
@@ -38,4 +37,17 @@ class ActionController::LogSubscriber
|
|
38
37
|
end
|
39
38
|
replaced
|
40
39
|
end
|
40
|
+
|
41
|
+
def formattable?(event)
|
42
|
+
return false unless Flog::Status.params_formattable?
|
43
|
+
|
44
|
+
if Flog.config.force_on_nested_params?
|
45
|
+
return true if event.payload[:params].values.any? { |value| value.is_a?(Hash) }
|
46
|
+
end
|
47
|
+
|
48
|
+
threshold = Flog.config.params_key_count_threshold.to_i
|
49
|
+
threshold += 1 if event.payload[:params].keys.include?("controller")
|
50
|
+
threshold += 1 if event.payload[:params].keys.include?("action")
|
51
|
+
event.payload[:params].keys.size > threshold
|
52
|
+
end
|
41
53
|
end
|
data/lib/flog/sql_formattable.rb
CHANGED
@@ -6,7 +6,7 @@ class ActiveRecord::LogSubscriber
|
|
6
6
|
include Flog::PayloadValueShuntable
|
7
7
|
|
8
8
|
def sql_with_flog(event)
|
9
|
-
return sql_without_flog(event) unless
|
9
|
+
return sql_without_flog(event) unless formattable?(event)
|
10
10
|
|
11
11
|
formatted = format_sql(event.payload[:sql])
|
12
12
|
|
@@ -29,4 +29,14 @@ class ActiveRecord::LogSubscriber
|
|
29
29
|
rule.indent_string = "\t"
|
30
30
|
AnbtSql::Formatter.new(rule).format(sql.squeeze(" "))
|
31
31
|
end
|
32
|
+
|
33
|
+
def formattable?(event)
|
34
|
+
return false unless Flog::Status.sql_formattable?
|
35
|
+
|
36
|
+
if event.payload[:name] == "CACHE" && Flog.config.ignore_cached_query?
|
37
|
+
return false
|
38
|
+
end
|
39
|
+
|
40
|
+
event.duration >= Flog.config.query_duration_threshold.to_f
|
41
|
+
end
|
32
42
|
end
|
data/lib/flog/version.rb
CHANGED
@@ -18,6 +18,12 @@ end
|
|
18
18
|
|
19
19
|
class ParamsFormattableTest < ActionController::TestCase
|
20
20
|
def setup
|
21
|
+
# default configuration
|
22
|
+
Flog.configure do |config|
|
23
|
+
config.params_key_count_threshold = 1
|
24
|
+
config.force_on_nested_params = true
|
25
|
+
end
|
26
|
+
|
21
27
|
@old_logger = ActionController::Base.logger
|
22
28
|
ActiveSupport::LogSubscriber.colorize_logging = false
|
23
29
|
@routes = ActionDispatch::Routing::RouteSet.new
|
@@ -77,6 +83,44 @@ class ParamsFormattableTest < ActionController::TestCase
|
|
77
83
|
end
|
78
84
|
end
|
79
85
|
|
86
|
+
def test_parameters_log_is_not_formatted_when_key_of_parameters_count_equals_to_configured_threshold
|
87
|
+
Flog.configure do |config|
|
88
|
+
config.params_key_count_threshold = 2
|
89
|
+
end
|
90
|
+
get :show, foo: "foo_value", bar: "bar_value"
|
91
|
+
assert_logger do |logger|
|
92
|
+
assert logger.infos[1].include?(%(Parameters: {"foo"=>"foo_value", "bar"=>"bar_value"}))
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_parameters_log_is_not_formatted_when_key_of_parameters_count_is_under_configured_threshold
|
97
|
+
Flog.configure do |config|
|
98
|
+
config.params_key_count_threshold = 3
|
99
|
+
end
|
100
|
+
get :show, foo: "foo_value", bar: "bar_value"
|
101
|
+
assert_logger do |logger|
|
102
|
+
assert logger.infos[1].include?(%(Parameters: {"foo"=>"foo_value", "bar"=>"bar_value"}))
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_parameters_log_is_formatted_when_key_of_parameters_count_is_under_configured_threshold_but_force_on_nested_params_configuration_is_true
|
107
|
+
Flog.configure do |config|
|
108
|
+
config.params_key_count_threshold = 3
|
109
|
+
end
|
110
|
+
get :show, foo: "foo_value", bar: { prop: "prop_value", attr: "attr_value" }
|
111
|
+
assert_logger do |logger|
|
112
|
+
logs = logger.infos.map { |log| log.gsub(/\e\[(\d+;)*\d+m/, "") }
|
113
|
+
assert_equal %( Parameters: ) , logs[1]
|
114
|
+
assert_equal %({) , logs[2]
|
115
|
+
assert_equal %( "foo" => "foo_value",) , logs[3]
|
116
|
+
assert_equal %( "bar" => {) , logs[4]
|
117
|
+
assert_equal %( "prop" => "prop_value",), logs[5]
|
118
|
+
assert_equal %( "attr" => "attr_value") , logs[6]
|
119
|
+
assert_equal %( }) , logs[7]
|
120
|
+
assert_equal %(}) , logs[8]
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
80
124
|
private
|
81
125
|
def assert_logger(&block)
|
82
126
|
if ActionController::Base.logger.errors.present?
|
@@ -15,6 +15,12 @@ class Book < ActiveRecord::Base; end
|
|
15
15
|
|
16
16
|
class SqlFormattableTest < ActiveSupport::TestCase
|
17
17
|
def setup
|
18
|
+
# default configuration
|
19
|
+
Flog.configure do |config|
|
20
|
+
config.ignore_cached_query = true
|
21
|
+
config.query_duration_threshold = 0.0
|
22
|
+
end
|
23
|
+
|
18
24
|
@old_logger = ActiveRecord::Base.logger
|
19
25
|
ActiveSupport::LogSubscriber.colorize_logging = false
|
20
26
|
ActiveRecord::Base.logger = TestLogger.new
|
@@ -68,6 +74,58 @@ class SqlFormattableTest < ActiveSupport::TestCase
|
|
68
74
|
end
|
69
75
|
end
|
70
76
|
|
77
|
+
def test_sql_is_not_formatted_on_cached_query
|
78
|
+
Book.cache do
|
79
|
+
Book.where(category: "comics").to_a
|
80
|
+
Book.where(category: "comics").to_a
|
81
|
+
end
|
82
|
+
assert_logger do |logger|
|
83
|
+
logs = logger.debugs.map { |log| log.gsub("\t", " ") }
|
84
|
+
assert_equal %{ SELECT} , logs[1]
|
85
|
+
assert_equal %{ "books" . *} , logs[2]
|
86
|
+
assert_equal %{ FROM} , logs[3]
|
87
|
+
assert_equal %{ "books"} , logs[4]
|
88
|
+
assert_equal %{ WHERE} , logs[5]
|
89
|
+
assert_equal %{ "books" . "category" = 'comics'}, logs[6]
|
90
|
+
assert logs[7].include?(%(SELECT "books".* FROM "books" WHERE "books"."category" = 'comics'))
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_sql_is_formatted_on_cached_query_when_ignore_cached_query_configration_is_false
|
95
|
+
Flog.configure do |config|
|
96
|
+
config.ignore_cached_query = false
|
97
|
+
end
|
98
|
+
Book.cache do
|
99
|
+
Book.where(category: "comics").to_a
|
100
|
+
Book.where(category: "comics").to_a
|
101
|
+
end
|
102
|
+
assert_logger do |logger|
|
103
|
+
logs = logger.debugs.map { |log| log.gsub("\t", " ") }
|
104
|
+
assert_equal %{ SELECT} , logs[1]
|
105
|
+
assert_equal %{ "books" . *} , logs[2]
|
106
|
+
assert_equal %{ FROM} , logs[3]
|
107
|
+
assert_equal %{ "books"} , logs[4]
|
108
|
+
assert_equal %{ WHERE} , logs[5]
|
109
|
+
assert_equal %{ "books" . "category" = 'comics'}, logs[6]
|
110
|
+
assert_equal %{ SELECT} , logs[8]
|
111
|
+
assert_equal %{ "books" . *} , logs[9]
|
112
|
+
assert_equal %{ FROM} , logs[10]
|
113
|
+
assert_equal %{ "books"} , logs[11]
|
114
|
+
assert_equal %{ WHERE} , logs[12]
|
115
|
+
assert_equal %{ "books" . "category" = 'comics'}, logs[13]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_sql_is_not_formatted_when_duration_is_under_threshold
|
120
|
+
Flog.configure do |config|
|
121
|
+
config.query_duration_threshold = 100.0
|
122
|
+
end
|
123
|
+
Book.where(category: "comics").to_a
|
124
|
+
assert_logger do |logger|
|
125
|
+
assert logger.debugs.first.include?(%(SELECT "books".* FROM "books" WHERE "books"."category" = 'comics'))
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
71
129
|
private
|
72
130
|
def assert_logger(&block)
|
73
131
|
if ActiveRecord::Base.logger.errors.present?
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-flog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pinzolo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -140,6 +140,7 @@ files:
|
|
140
140
|
- gemfiles/rails_4_0_x.gemfile
|
141
141
|
- gemfiles/rails_4_1_x.gemfile
|
142
142
|
- lib/flog.rb
|
143
|
+
- lib/flog/configuration.rb
|
143
144
|
- lib/flog/params_formattable.rb
|
144
145
|
- lib/flog/payload_value_shuntable.rb
|
145
146
|
- lib/flog/sql_formattable.rb
|