rails-flog 1.0.0 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2e219154b03727ea2a4b7f3fb3ae97258085dbe
4
- data.tar.gz: 8e87fe2fc6ce0ed022482168d7776b3e5f8e1c24
3
+ metadata.gz: 57c68ce2bbb1cb74f3bde72f9f9226ecb4ddd8d3
4
+ data.tar.gz: 8981369d077b93a3d63a4fff554e01f7b8869487
5
5
  SHA512:
6
- metadata.gz: e6510bf8585735c1ddf0af141c922ef804f295d88e7efc66975d259f285c9cbb35b0935958889647ac24cf5c107bbdc7a0da8223404cb9232aeba89bf1f38ac0
7
- data.tar.gz: cbab9d2ade5b65ecd88cf05b92ddd6aaf80b52cddb09b58c3caa9be2a17b0f2a121ca28e80b966c166cfca13ffd459d6878e13d7e87caf510f88a7d00f0ec898
6
+ metadata.gz: 026b7737f8ec98e1f1e3749dd00e00eb728728473ba771f836d8cdc2dbb978442ddc2ac691df88be0ec7d5d0749eefe069784b6a85f9761d2777ba5edcce869a
7
+ data.tar.gz: 081eda06508831663a0874bb5b2be931428e55bb95afec2781ee0be8c41e97d10978851ed9016cc2c4d26a60d6e8076a03ad396ed7c97e830ac82dd6c8d50533
data/.travis.yml CHANGED
@@ -3,5 +3,5 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
5
  gemfile:
6
- - gemfiles/activemodel_3_2_x.gemfile
7
- - gemfiles/activemodel_4_0_x.gemfile
6
+ - gemfiles/rails_3_2_x.gemfile
7
+ - gemfiles/rails_4_0_x.gemfile
data/README.md CHANGED
@@ -13,7 +13,7 @@ rails-flog provides feature that formats parameters Hash and SQL in Rails log fi
13
13
  # Parameters
14
14
  Processing by IssuesController#create as HTML
15
15
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"VYCWAsE+aAN+zSZq2H3ONNqaU8rlyfbnXLfbwDY1i10=", "issue"=>{"is_private"=>"0", "tracker_id"=>"1", "subject"=>"test ticket", "description"=>"test ticket description", "status_id"=>"1", "priority_id"=>"2", "assigned_to_id"=>"1", "parent_issue_id"=>"", "start_date"=>"2013-11-28", "due_date"=>"2013-11-29", "estimated_hours"=>"5", "done_ratio"=>"10"}, "commit"=>"Create", "project_id"=>"test"}
16
-
16
+
17
17
  # SQL
18
18
  IssueCustomField Load (0.0ms) SELECT "custom_fields".* FROM "custom_fields" WHERE "custom_fields"."type" IN ('IssueCustomField') AND (is_for_all = 't' OR id IN (SELECT DISTINCT cfp.custom_field_id FROM custom_fields_projects cfp WHERE cfp.project_id = 1)) ORDER BY custom_fields.position ASC
19
19
  ```
@@ -46,7 +46,7 @@ Processing by IssuesController#create as HTML
46
46
  }
47
47
 
48
48
  # SQL
49
- IssueCustomField Load (0.0ms)
49
+ IssueCustomField Load (0.0ms)
50
50
  SELECT
51
51
  "custom_fields" . *
52
52
  FROM
@@ -87,6 +87,10 @@ Or install it yourself as:
87
87
 
88
88
  Just install.
89
89
 
90
+ ## Disable temporary
91
+
92
+ If you put a file named `no-flog.txt` to `<rails_app>/tmp` direcotry, `rails-flog` will disable format.
93
+
90
94
  ## Supported versions
91
95
 
92
96
  - Ruby: 1.9.3, 2.0.0
@@ -100,8 +104,7 @@ Just install.
100
104
  4. Push to the branch (`git push origin my-new-feature`)
101
105
  5. Create new Pull Request
102
106
 
103
- ## Thanks
107
+ ## Changelog
104
108
 
105
- - [awesome_print](https://github.com/michaeldv/awesome_print) : Use format Hash
106
- - [anbt-sql-formatter](https://github.com/sonota/anbt-sql-formatter) : Use format SQL
107
- - [yuroyoro](http://yuroyoro.hatenablog.com/entry/2013/04/12/141648) : Inspired
109
+ - v1.0.0 (2013-11-28 JST): First release
110
+ - v1.1.0 (2013-12-02 JST): Add feature that disables format by no-flog.txt
data/lib/flog.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # coding: utf-8
2
2
  require "flog/version"
3
+ require "flog/status"
3
4
  require "flog/sql_formattable"
4
5
  require "flog/params_formattable"
5
6
 
@@ -2,13 +2,16 @@
2
2
  require "action_controller/log_subscriber"
3
3
  require "awesome_print"
4
4
  require "flog/payload_value_shuntable"
5
+ require "flog/status"
5
6
 
6
7
  class ActionController::LogSubscriber
7
- include PayloadValueShuntable
8
+ include Flog::PayloadValueShuntable
8
9
 
9
10
  alias :original_start_processing :start_processing
10
11
 
11
12
  def start_processing(event)
13
+ return original_start_processing(event) unless Flog::Status.enabled?
14
+
12
15
  replaced = replace_params(event.payload[:params])
13
16
 
14
17
  shunt_payload_value(event.payload, :params, replaced) do
@@ -1,18 +1,20 @@
1
1
  # coding: utf-8
2
- module PayloadValueShuntable
3
- def shunt_payload_value(payload, key, temp_value, &block)
4
- return unless block
2
+ module Flog
3
+ module PayloadValueShuntable
4
+ def shunt_payload_value(payload, key, temp_value, &block)
5
+ return unless block
5
6
 
6
- key_exists = payload.key?(key)
7
- base_value = payload[key]
8
- begin
9
- payload[key] = temp_value
10
- block.call
11
- ensure
12
- if key_exists
13
- payload[key] = base_value
14
- else
15
- payload.delete(key)
7
+ key_exists = payload.key?(key)
8
+ base_value = payload[key]
9
+ begin
10
+ payload[key] = temp_value
11
+ block.call
12
+ ensure
13
+ if key_exists
14
+ payload[key] = base_value
15
+ else
16
+ payload.delete(key)
17
+ end
16
18
  end
17
19
  end
18
20
  end
@@ -3,11 +3,13 @@ require "active_record/log_subscriber"
3
3
  require "flog/payload_value_shuntable"
4
4
 
5
5
  class ActiveRecord::LogSubscriber
6
- include PayloadValueShuntable
6
+ include Flog::PayloadValueShuntable
7
7
 
8
8
  alias :original_sql :sql
9
9
 
10
10
  def sql(event)
11
+ return original_sql(event) unless Flog::Status.enabled?
12
+
11
13
  formatted = format_sql(event.payload[:sql])
12
14
 
13
15
  shunt_payload_value(event.payload, :sql, "\n\t#{formatted}") do
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+ require "rails"
3
+
4
+ module Flog
5
+ class Status
6
+ SWITCH_FILE_NAME = "no-flog.txt"
7
+
8
+ def self.enabled?
9
+ !File.exist?(Rails.root.join("tmp", SWITCH_FILE_NAME).to_s)
10
+ rescue
11
+ true
12
+ end
13
+ end
14
+ end
data/lib/flog/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # coding: utf-8
2
2
  module Flog
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
data/rails-flog.gemspec CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "sqlite3"
24
24
  spec.add_development_dependency "coveralls"
25
+ spec.add_development_dependency "mocha"
25
26
 
26
27
  spec.add_dependency "rails", ">=3.2.0"
27
28
  spec.add_dependency "anbt-sql-formatter"
data/test/test_helper.rb CHANGED
@@ -10,6 +10,7 @@ end
10
10
  Bundler.require
11
11
  require "flog"
12
12
  require "test/unit"
13
+ require "mocha/setup"
13
14
 
14
15
  class TestLogger
15
16
  attr_accessor :debugs, :infos, :errors
@@ -61,6 +61,14 @@ class ParamsFormattableTest < ActionController::TestCase
61
61
  end
62
62
  end
63
63
 
64
+ def test_parameters_log_is_not_formatted_when_enabled_is_false
65
+ Flog::Status.expects(:enabled?).returns(false)
66
+ get :show, foo: "foo_value", bar: "bar_value"
67
+ assert_logger do |logger|
68
+ assert logger.infos[1].include?(%(Parameters: {"foo"=>"foo_value", "bar"=>"bar_value"}))
69
+ end
70
+ end
71
+
64
72
  private
65
73
  def assert_logger(&block)
66
74
  if ActionController::Base.logger.errors.present?
@@ -2,7 +2,7 @@
2
2
  require "test_helper"
3
3
 
4
4
  class TestClass
5
- include PayloadValueShuntable
5
+ include Flog::PayloadValueShuntable
6
6
  end
7
7
 
8
8
  class PayloadValueShuntableTest < ActiveSupport::TestCase
@@ -52,6 +52,14 @@ class SqlFormattableTest < ActiveSupport::TestCase
52
52
  end
53
53
  end
54
54
 
55
+ def test_sql_is_not_formatted_when_enabled_is_false
56
+ Flog::Status.expects(:enabled?).returns(false)
57
+ Book.where(category: "comics").to_a
58
+ assert_logger do |logger|
59
+ assert logger.debugs.first.include?(%(SELECT "books".* FROM "books" WHERE "books"."category" = 'comics'))
60
+ end
61
+ end
62
+
55
63
  private
56
64
  def assert_logger(&block)
57
65
  if ActiveRecord::Base.logger.errors.present?
@@ -0,0 +1,41 @@
1
+ # coding: utf-8
2
+ require "rails"
3
+
4
+ class StatusTest < ActiveSupport::TestCase
5
+ def setup
6
+ @test_root = Pathname.new(File.expand_path(File.dirname(__FILE__) + "../../"))
7
+ @switch_file_path = @test_root.join("tmp", Flog::Status::SWITCH_FILE_NAME)
8
+ end
9
+
10
+ def teardown
11
+ delete_switch_file
12
+ end
13
+
14
+ def test_enabled_is_true_when_switch_file_does_not_exist
15
+ delete_switch_file
16
+ assert Flog::Status.enabled?
17
+ end
18
+
19
+ def test_enabled_is_false_when_switch_file_exists
20
+ Rails.expects(:root).returns(@test_root)
21
+ create_switch_file
22
+ assert_equal false, Flog::Status.enabled?
23
+ end
24
+
25
+ def test_enabled_is_true_when_error_is_raised_in_process
26
+ Rails.expects(:root).returns(nil) # For raise NoMethodError
27
+ create_switch_file
28
+ assert Flog::Status.enabled?
29
+ end
30
+
31
+ private
32
+ def create_switch_file
33
+ File.open(@switch_file_path, "w").close
34
+ end
35
+
36
+ def delete_switch_file
37
+ if File.exist?(@switch_file_path)
38
+ File.delete(@switch_file_path)
39
+ end
40
+ end
41
+ end
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.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pinzolo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-28 00:00:00.000000000 Z
11
+ date: 2013-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rails
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -122,18 +136,21 @@ files:
122
136
  - LICENSE.txt
123
137
  - README.md
124
138
  - Rakefile
125
- - gemfiles/activemodel_3_2_x.gemfile
126
- - gemfiles/activemodel_4_0_x.gemfile
139
+ - gemfiles/rails_3_2_x.gemfile
140
+ - gemfiles/rails_4_0_x.gemfile
127
141
  - lib/flog.rb
128
142
  - lib/flog/params_formattable.rb
129
143
  - lib/flog/payload_value_shuntable.rb
130
144
  - lib/flog/sql_formattable.rb
145
+ - lib/flog/status.rb
131
146
  - lib/flog/version.rb
132
147
  - rails-flog.gemspec
133
148
  - test/test_helper.rb
149
+ - test/tmp/.gitkeep
134
150
  - test/unit/params_formattable_test.rb
135
151
  - test/unit/payload_value_shuntable_test.rb
136
152
  - test/unit/sql_formattable_test.rb
153
+ - test/unit/status_test.rb
137
154
  homepage: https://github.com/pinzolo/rails-flog
138
155
  licenses:
139
156
  - MIT
@@ -160,6 +177,8 @@ specification_version: 4
160
177
  summary: Rails log formatter for parameters and sql
161
178
  test_files:
162
179
  - test/test_helper.rb
180
+ - test/tmp/.gitkeep
163
181
  - test/unit/params_formattable_test.rb
164
182
  - test/unit/payload_value_shuntable_test.rb
165
183
  - test/unit/sql_formattable_test.rb
184
+ - test/unit/status_test.rb