rails-flog 1.5.0 → 1.6.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/.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
data/lib/flog/status.rb
CHANGED
@@ -1,44 +1,49 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails'
|
2
4
|
|
3
5
|
module Flog
|
6
|
+
# Status returns checke result of switch files
|
4
7
|
class Status
|
5
|
-
SWITCH_FILE_NAME =
|
6
|
-
SQL_SWITCH_FILE_NAME =
|
7
|
-
PARAMS_SWITCH_FILE_NAME =
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
SWITCH_FILE_NAME = 'no-flog.txt'
|
9
|
+
SQL_SWITCH_FILE_NAME = 'no-flog-sql.txt'
|
10
|
+
PARAMS_SWITCH_FILE_NAME = 'no-flog-params.txt'
|
11
|
+
class << self
|
12
|
+
def enabled?
|
13
|
+
!switch_file_exists?(SWITCH_FILE_NAME)
|
14
|
+
rescue StandardError
|
15
|
+
true
|
16
|
+
end
|
14
17
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
def sql_formattable?
|
19
|
+
enabled? && !switch_file_exists?(SQL_SWITCH_FILE_NAME)
|
20
|
+
rescue StandardError
|
21
|
+
true
|
22
|
+
end
|
20
23
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
def params_formattable?
|
25
|
+
enabled? && !switch_file_exists?(PARAMS_SWITCH_FILE_NAME)
|
26
|
+
rescue StandardError
|
27
|
+
true
|
28
|
+
end
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
def switch_file_base_path
|
31
|
+
if Rails.root&.exist?
|
32
|
+
Rails.root
|
33
|
+
else
|
34
|
+
Pathname.new(File.expand_path(File.dirname(__FILE__) + '../../'))
|
35
|
+
end
|
32
36
|
end
|
33
|
-
end
|
34
37
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
+
def switch_file_dir_path
|
39
|
+
switch_file_base_path.join('tmp')
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
38
43
|
|
39
|
-
|
40
|
-
|
41
|
-
|
44
|
+
def switch_file_exists?(file_name)
|
45
|
+
switch_file_dir_path.join(file_name).exist?
|
46
|
+
end
|
42
47
|
end
|
43
48
|
end
|
44
49
|
end
|
data/lib/flog/version.rb
CHANGED
data/rails-flog.gemspec
CHANGED
@@ -1,30 +1,32 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
require 'flog/version'
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
8
|
+
spec.name = 'rails-flog'
|
8
9
|
spec.version = Flog::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.description =
|
12
|
-
spec.summary =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
10
|
+
spec.authors = ['pinzolo']
|
11
|
+
spec.email = ['pinzolo@gmail.com']
|
12
|
+
spec.description = 'This formats parameters and sql in rails log.'
|
13
|
+
spec.summary = 'Rails log formatter for parameters and sql'
|
14
|
+
spec.homepage = 'https://github.com/pinzolo/rails-flog'
|
15
|
+
spec.license = 'MIT'
|
15
16
|
|
16
|
-
spec.files = `git ls-files`.split(
|
17
|
+
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
17
18
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = [
|
20
|
+
spec.require_paths = ['lib']
|
20
21
|
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.add_development_dependency
|
25
|
-
spec.add_development_dependency
|
22
|
+
spec.add_development_dependency 'bundler'
|
23
|
+
spec.add_development_dependency 'coveralls'
|
24
|
+
spec.add_development_dependency 'minitest'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
spec.add_development_dependency 'rubocop'
|
27
|
+
spec.add_development_dependency 'sqlite3', '~> 1.3.0'
|
26
28
|
|
27
|
-
spec.add_dependency
|
28
|
-
spec.add_dependency
|
29
|
-
spec.add_dependency
|
29
|
+
spec.add_dependency 'anbt-sql-formatter', '>=0.0.7'
|
30
|
+
spec.add_dependency 'awesome_print'
|
31
|
+
spec.add_dependency 'rails', '>=4.2.0'
|
30
32
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'coveralls'
|
4
|
+
require 'simplecov'
|
3
5
|
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
4
6
|
SimpleCov.start do
|
5
7
|
add_filter '/test/'
|
6
8
|
add_filter '/bundle/'
|
7
9
|
end
|
8
10
|
|
9
|
-
require
|
10
|
-
require
|
11
|
+
require 'flog'
|
12
|
+
require 'minitest/autorun'
|
11
13
|
|
12
14
|
unless defined?(TestLogger)
|
13
15
|
class TestLogger
|
@@ -42,10 +44,10 @@ unless defined?(TestLogger)
|
|
42
44
|
end
|
43
45
|
|
44
46
|
unless defined?(COLOR_SEQ_REGEX)
|
45
|
-
COLOR_SEQ_REGEX = /\e\[(\d+;)*\d+m
|
47
|
+
COLOR_SEQ_REGEX = /\e\[(\d+;)*\d+m/.freeze
|
46
48
|
|
47
49
|
def remove_color_seq(log)
|
48
|
-
log.gsub(COLOR_SEQ_REGEX,
|
50
|
+
log.gsub(COLOR_SEQ_REGEX, '')
|
49
51
|
end
|
50
52
|
|
51
53
|
def match_color_seq(log)
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'action_controller'
|
4
|
+
require 'test_helper'
|
3
5
|
|
4
6
|
class TestController < ActionController::Base
|
5
7
|
def initialize(routes)
|
@@ -15,7 +17,7 @@ class TestController < ActionController::Base
|
|
15
17
|
end
|
16
18
|
end
|
17
19
|
|
18
|
-
|
20
|
+
module ParamsFormattableTestHelper
|
19
21
|
def setup
|
20
22
|
# default configuration
|
21
23
|
Flog.configure do |config|
|
@@ -25,11 +27,7 @@ class ParamsFormattableTest < ActionController::TestCase
|
|
25
27
|
|
26
28
|
@old_logger = ActionController::Base.logger
|
27
29
|
ActiveSupport::LogSubscriber.colorize_logging = false
|
28
|
-
|
29
|
-
@routes.draw do
|
30
|
-
get "test/show", to: "test#show"
|
31
|
-
end
|
32
|
-
@controller = TestController.new(@routes)
|
30
|
+
setup_routes
|
33
31
|
super
|
34
32
|
ActionController::Base.logger = TestLogger.new
|
35
33
|
end
|
@@ -39,52 +37,89 @@ class ParamsFormattableTest < ActionController::TestCase
|
|
39
37
|
ActionController::Base.logger = @old_logger
|
40
38
|
end
|
41
39
|
|
40
|
+
def setup_routes
|
41
|
+
@routes = ActionDispatch::Routing::RouteSet.new
|
42
|
+
@routes.draw do
|
43
|
+
get 'test/show', to: 'test#show'
|
44
|
+
end
|
45
|
+
@controller = TestController.new(@routes)
|
46
|
+
end
|
47
|
+
|
48
|
+
def assert_logger(&block)
|
49
|
+
raise ActionController::Base.logger.errors.first if ActionController::Base.logger.errors.present?
|
50
|
+
|
51
|
+
block.call(ActionController::Base.logger)
|
52
|
+
end
|
53
|
+
|
54
|
+
def assert_include(log, *expected_includees)
|
55
|
+
expected_includees.each do |e|
|
56
|
+
assert log.include?(e)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def assert_hash(hash)
|
61
|
+
assert_equal hash['foo'], 'foo_value'
|
62
|
+
assert_equal hash['bar']['prop'], 'prop_value'
|
63
|
+
assert_equal hash['bar']['attr'], 'attr_value'
|
64
|
+
end
|
65
|
+
|
66
|
+
def get_show(params)
|
67
|
+
if Gem::Version.new(Rails.version) >= Gem::Version.new('5.0.0')
|
68
|
+
get :show, params: params
|
69
|
+
else
|
70
|
+
get :show, params
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def hash_from_logs(logs, start, finish)
|
75
|
+
eval(start.upto(finish).reduce('') { |s, n| s + logs[n] })
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class ParamsFormattableTest < ActionController::TestCase
|
80
|
+
include ParamsFormattableTestHelper
|
81
|
+
|
42
82
|
def test_parameters_log_is_formatted
|
43
|
-
get_show foo:
|
83
|
+
get_show foo: 'foo_value', bar: { prop: 'prop_value', attr: 'attr_value' }
|
44
84
|
assert_logger do |logger|
|
45
85
|
logs = logger.infos.map { |log| remove_color_seq(log) }
|
46
|
-
assert_equal
|
47
|
-
hash = hash_from_logs(logs, 2,
|
48
|
-
|
49
|
-
assert_equal hash["bar"], "bar_value"
|
86
|
+
assert_equal ' Parameters: ', logs[1]
|
87
|
+
hash = hash_from_logs(logs, 2, 8)
|
88
|
+
assert_hash hash
|
50
89
|
end
|
51
90
|
end
|
52
91
|
|
53
92
|
def test_colorized_on_colorize_loggin_is_true
|
54
93
|
ActiveSupport::LogSubscriber.colorize_logging = true
|
55
|
-
get_show foo:
|
94
|
+
get_show foo: 'foo_value', bar: 'bar_value'
|
56
95
|
assert_logger do |logger|
|
57
|
-
assert match_color_seq(logger.infos.join
|
96
|
+
assert match_color_seq(logger.infos.join)
|
58
97
|
end
|
59
98
|
end
|
60
99
|
|
61
100
|
def test_not_colorized_on_colorize_loggin_is_false
|
62
101
|
Flog::Status.stub(:enabled?, true) do
|
63
|
-
get_show foo:
|
102
|
+
get_show foo: 'foo_value', bar: 'bar_value'
|
64
103
|
assert_logger do |logger|
|
65
|
-
assert_nil match_color_seq(logger.infos.join
|
104
|
+
assert_nil match_color_seq(logger.infos.join)
|
66
105
|
end
|
67
106
|
end
|
68
107
|
end
|
69
108
|
|
70
109
|
def test_parameters_log_is_not_formatted_when_enabled_is_false
|
71
110
|
Flog::Status.stub(:enabled?, false) do
|
72
|
-
get_show foo:
|
111
|
+
get_show foo: 'foo_value', bar: 'bar_value'
|
73
112
|
assert_logger do |logger|
|
74
|
-
|
75
|
-
assert logger.infos[1].include?(%("foo"=>"foo_value"))
|
76
|
-
assert logger.infos[1].include?(%("bar"=>"bar_value"))
|
113
|
+
assert_include logger.infos[1], 'Parameters: {', %("foo"=>"foo_value"), %("bar"=>"bar_value")
|
77
114
|
end
|
78
115
|
end
|
79
116
|
end
|
80
117
|
|
81
118
|
def test_parameters_log_is_not_formatted_when_params_formattable_is_false
|
82
119
|
Flog::Status.stub(:params_formattable?, false) do
|
83
|
-
get_show foo:
|
120
|
+
get_show foo: 'foo_value', bar: 'bar_value'
|
84
121
|
assert_logger do |logger|
|
85
|
-
|
86
|
-
assert logger.infos[1].include?(%("foo"=>"foo_value"))
|
87
|
-
assert logger.infos[1].include?(%("bar"=>"bar_value"))
|
122
|
+
assert_include logger.infos[1], 'Parameters: {', %("foo"=>"foo_value"), %("bar"=>"bar_value")
|
88
123
|
end
|
89
124
|
end
|
90
125
|
end
|
@@ -93,11 +128,9 @@ class ParamsFormattableTest < ActionController::TestCase
|
|
93
128
|
Flog.configure do |config|
|
94
129
|
config.params_key_count_threshold = 2
|
95
130
|
end
|
96
|
-
get_show foo:
|
131
|
+
get_show foo: 'foo_value', bar: 'bar_value'
|
97
132
|
assert_logger do |logger|
|
98
|
-
|
99
|
-
assert logger.infos[1].include?(%("foo"=>"foo_value"))
|
100
|
-
assert logger.infos[1].include?(%("bar"=>"bar_value"))
|
133
|
+
assert_include logger.infos[1], 'Parameters: {', %("foo"=>"foo_value"), %("bar"=>"bar_value")
|
101
134
|
end
|
102
135
|
end
|
103
136
|
|
@@ -105,47 +138,34 @@ class ParamsFormattableTest < ActionController::TestCase
|
|
105
138
|
Flog.configure do |config|
|
106
139
|
config.params_key_count_threshold = 3
|
107
140
|
end
|
108
|
-
get_show foo:
|
141
|
+
get_show foo: 'foo_value', bar: 'bar_value'
|
109
142
|
assert_logger do |logger|
|
110
|
-
|
111
|
-
assert logger.infos[1].include?(%("foo"=>"foo_value"))
|
112
|
-
assert logger.infos[1].include?(%("bar"=>"bar_value"))
|
143
|
+
assert_include logger.infos[1], 'Parameters: {', %("foo"=>"foo_value"), %("bar"=>"bar_value")
|
113
144
|
end
|
114
145
|
end
|
115
146
|
|
147
|
+
# rubocop:disable Metrics/LineLength
|
116
148
|
def test_parameters_log_is_formatted_when_key_of_parameters_count_is_under_configured_threshold_but_force_on_nested_params_configuration_is_true
|
117
149
|
Flog.configure do |config|
|
118
150
|
config.params_key_count_threshold = 3
|
119
151
|
end
|
120
|
-
get_show foo:
|
152
|
+
get_show foo: 'foo_value', bar: { prop: 'prop_value', attr: 'attr_value' }
|
121
153
|
assert_logger do |logger|
|
122
154
|
logs = logger.infos.map { |log| remove_color_seq(log) }
|
123
|
-
assert_equal
|
155
|
+
assert_equal ' Parameters: ', logs[1]
|
124
156
|
hash = hash_from_logs(logs, 2, 8)
|
125
|
-
|
126
|
-
assert_equal hash["bar"]["prop"], "prop_value"
|
127
|
-
assert_equal hash["bar"]["attr"], "attr_value"
|
157
|
+
assert_hash hash
|
128
158
|
end
|
129
159
|
end
|
160
|
+
# rubocop:enable Metrics/LineLength
|
130
161
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
fail ActionController::Base.logger.errors.first
|
135
|
-
else
|
136
|
-
block.call(ActionController::Base.logger)
|
162
|
+
def test_parameters_log_is_not_formatted_when_ignore_params_configuration_is_true
|
163
|
+
Flog.configure do |config|
|
164
|
+
config.ignore_params = true
|
137
165
|
end
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
if Gem::Version.new(Rails.version) >= Gem::Version.new("5.0.0")
|
142
|
-
get :show, params: params
|
143
|
-
else
|
144
|
-
get :show, params
|
166
|
+
get_show foo: 'foo_value', bar: 'bar_value'
|
167
|
+
assert_logger do |logger|
|
168
|
+
assert_include logger.infos[1], 'Parameters: {', %("foo"=>"foo_value"), %("bar"=>"bar_value")
|
145
169
|
end
|
146
170
|
end
|
147
|
-
|
148
|
-
def hash_from_logs(logs, start, finish)
|
149
|
-
eval(start.upto(finish).reduce("") { |s, n| s + logs[n] })
|
150
|
-
end
|
151
171
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_helper'
|
2
4
|
|
3
5
|
class TestClass
|
4
6
|
include Flog::PayloadValueShuntable
|
@@ -7,49 +9,49 @@ end
|
|
7
9
|
class PayloadValueShuntableTest < ActiveSupport::TestCase
|
8
10
|
def setup
|
9
11
|
@tester = TestClass.new
|
10
|
-
@payload = { foo:
|
12
|
+
@payload = { foo: 'foo_value', bar: 'bar_value' }
|
11
13
|
end
|
12
14
|
|
13
15
|
def test_shunt_payload_value
|
14
|
-
@tester.shunt_payload_value(@payload, :foo,
|
15
|
-
assert_equal
|
16
|
+
@tester.shunt_payload_value(@payload, :foo, 'new_value') do
|
17
|
+
assert_equal 'new_value', @payload[:foo]
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
21
|
def test_value_of_other_key_is_not_changed
|
20
|
-
@tester.shunt_payload_value(@payload, :foo,
|
21
|
-
assert_equal
|
22
|
+
@tester.shunt_payload_value(@payload, :foo, 'new_value') do
|
23
|
+
assert_equal 'bar_value', @payload[:bar]
|
22
24
|
end
|
23
25
|
end
|
24
26
|
|
25
27
|
def test_restoration
|
26
|
-
@tester.shunt_payload_value(@payload, :foo,
|
27
|
-
assert_equal
|
28
|
+
@tester.shunt_payload_value(@payload, :foo, 'new_value') do
|
29
|
+
assert_equal 'new_value', @payload[:foo]
|
28
30
|
end
|
29
|
-
assert_equal
|
31
|
+
assert_equal 'foo_value', @payload[:foo]
|
30
32
|
end
|
31
33
|
|
32
34
|
def test_ensure_restoration_on_error_raised_in_block
|
33
35
|
error_raised = false
|
34
36
|
begin
|
35
|
-
@tester.shunt_payload_value(@payload, :foo,
|
36
|
-
raise
|
37
|
+
@tester.shunt_payload_value(@payload, :foo, 'new_value') do
|
38
|
+
raise 'error'
|
37
39
|
end
|
38
|
-
rescue
|
40
|
+
rescue StandardError
|
39
41
|
error_raised = true
|
40
42
|
end
|
41
43
|
assert error_raised
|
42
|
-
assert_equal
|
44
|
+
assert_equal 'foo_value', @payload[:foo]
|
43
45
|
end
|
44
46
|
|
45
47
|
def test_without_block
|
46
|
-
@tester.shunt_payload_value(@payload, :foo,
|
47
|
-
assert_equal
|
48
|
+
@tester.shunt_payload_value(@payload, :foo, 'new_value')
|
49
|
+
assert_equal 'foo_value', @payload[:foo]
|
48
50
|
end
|
49
51
|
|
50
52
|
def test_key_removed_when_give_not_exist_key
|
51
|
-
@tester.shunt_payload_value(@payload, :baz,
|
52
|
-
assert_equal
|
53
|
+
@tester.shunt_payload_value(@payload, :baz, 'new_value') do
|
54
|
+
assert_equal 'new_value', @payload[:baz]
|
53
55
|
end
|
54
56
|
assert_equal false, @payload.key?(:baz)
|
55
57
|
end
|