active_record-mod_sql_log_subscriber 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cab5693d0baabd7697dba145864541d835fc0a82c7f89b5e078ff321b6af8147
4
+ data.tar.gz: 327dc2c516057842f48b4a26189705898f84e918c15101c44072d67252f4d861
5
+ SHA512:
6
+ metadata.gz: 6913d322729c0f5d503475489c0ee47e3e908262f3998f1347364f2198f59a5cfd8c0e015031328eb12fdc1140b0444ad57de7477f9c775f57955dd25f85e47f
7
+ data.tar.gz: 12de01f66bdc5496be9fb46a3d1c994b95ea371464cf851daf1336a7bdf47a1393bfef0794ea999f7e4bcaada01afaf6cf1897c3047d178099ca8185d9df8697
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
11
+ .ruby-version
12
+ .rspec
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at dev.ryu39@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in active_record-mod_sql_log_subscriber.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 ryu39
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,166 @@
1
+ # ActiveRecord::ModSqlLogSubscriber
2
+
3
+ An ActiveRecord::LogSubscriber which records only mod-SQL logs.
4
+
5
+ The mod-SQL is shown as follows.
6
+
7
+ * INSERT
8
+ * UPDATE
9
+ * DELETE
10
+ * TRUNCATE
11
+ * BEGIN
12
+ * COMMIT
13
+ * ROLLBACK
14
+ * SAVEPOINT
15
+ * RELEASE SAVEPOINT
16
+ * ROLLBACK TO SAVEPOINT
17
+
18
+ ## Motivation
19
+
20
+ * SQL logs are useful for trouble shooging.
21
+ * Especialy, mod-SQL(INSERT, UPDATE, DELETE, ...) logs are very important to know what occurred in app.
22
+ * Rails provides SQL logging feature only by settings `Rails.logger` log level to `:debug`.
23
+ * But this settings writes a lot of `SELECT` statement SQL logs.
24
+ These logs are very noisy and use many disk space.
25
+ * I want to write logs only mod-SQL(INSERT, UPDATE, DELETE, ...) and transaction SQL(BEGIN, COMMIT, ROLLBACK, ...).
26
+ So I create this gem.
27
+
28
+ ## Installation
29
+
30
+ Add this line to your application's Gemfile:
31
+
32
+ ```ruby
33
+ gem 'active_record-mod_sql_log_subscriber'
34
+ ```
35
+
36
+ And then execute:
37
+
38
+ $ bundle install
39
+
40
+ Or install it yourself as:
41
+
42
+ $ gem install active_record-mod_sql_log_subscriber
43
+
44
+ ## Usage
45
+
46
+ Create a file under`config/initializer`, configure log subscriber,
47
+ and attach to :active_record namespace.
48
+
49
+ ```
50
+ require 'active_record/mod_sql_log_subscriber'
51
+
52
+ ::ActiveRecord::ModSqlLogSubscriber.configure do |config|
53
+ config.disable = ::ActiveRecord::Base.logger.debug? # Recommended
54
+ end
55
+
56
+ ::ActiveRecord::ModSqlLogSubscriber.attach_to(:active_record)
57
+ ```
58
+
59
+ Next restart your app, then your app starts to log mod-SQLs.
60
+
61
+ Here is rails console examples.
62
+
63
+ ```
64
+ $ rails c
65
+
66
+ > ActiveRecord::Base.logger = Logger.new(STDOUT)
67
+
68
+ > user = User.find(1); nil # Do not logging
69
+
70
+ > user.update_attributes(name: 'New name'); nil # Do logging
71
+ # => "UPDATE users SET name = 'New name' WHERE id = $1 {:id=>1}"
72
+ ```
73
+
74
+ ## Configuration
75
+
76
+ You can configure this subscriber before attaching to :active_record namespace
77
+
78
+ ```
79
+ ::ActiveRecord::ModSqlLogSubscriber.configure do |config|
80
+ config.disable = true
81
+ config.log_level = :warn
82
+ config.log_format = :json
83
+ # config.log_format = ->(sql, binds) { 'your code here' }
84
+ config.target_statements << 'merge'
85
+ end
86
+
87
+ # You must attach subscriber after configuraton.
88
+ ::ActiveRecord::ModSqlLogSubscriber.attach_to(:active_record)
89
+ ```
90
+
91
+ ### Enable/Disable
92
+
93
+ You can make log subscriber enbaled/disabled via config. Default is enabled.
94
+ This is useful in development/test env because duplicated sql logs are written in log in this env.
95
+
96
+ ```
97
+ ::ActiveRecord::ModSqlLogSubscriber.configure do |config|
98
+ config.disable = false # enabled
99
+ config.disable = true # disabled
100
+
101
+ config.disable = ::ActiveRecord::Base.logger.debug? # Recommended
102
+ end
103
+ ```
104
+
105
+ ### Log level
106
+
107
+ You can change log level. Default is :info.
108
+
109
+ ```
110
+ ::ActiveRecord::ModSqlLogSubscriber.configure do |config|
111
+ config.log_level = :warn
112
+ # config.log_level = :error
113
+ # config.log_level = :fatal
114
+ end
115
+ ```
116
+
117
+ ### Log format
118
+
119
+ You can select log format as follows, or use a custom Proc object.
120
+
121
+ * `:text` (Default)
122
+ * `:json`
123
+ * `:hash`
124
+ * Proc object
125
+
126
+ ### Target statement
127
+
128
+ You can add/change/remove target SQL statements.
129
+
130
+ ```
131
+ ::ActiveRecord::ModSqlLogSubscriber.configure do |config|
132
+ # Add MERGE SQL
133
+ config.target_statements << 'merge'
134
+
135
+ # Remove BEGIN SQL
136
+ config.target_stagements.delete('begin')
137
+
138
+ # Orverwite your target statements
139
+ config.target_statements = %w(insert update delete)
140
+ end
141
+ ```
142
+
143
+ ## Development
144
+
145
+ After checking out the repo, run `bin/setup` to install dependencies.
146
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
147
+
148
+ To install this gem onto your local machine, run `bundle exec rake install`.
149
+ To release a new version, update the version number in `mod_sql_log_subscriber.r`,
150
+ and then run `bundle exec rake release`, which will create a git tag for the version,
151
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
152
+
153
+ ## Contributing
154
+
155
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ryu39/active_record-mod_sql_log_subscriber.
156
+ This project is intended to be a safe, welcoming space for collaboration,
157
+ and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
158
+
159
+ ## License
160
+
161
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
162
+
163
+ ## Code of Conduct
164
+
165
+ Everyone interacting in the ActiveRecord::ModSqlLogSubscriber project’s codebases, issue trackers,
166
+ chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ryu39/active_record-mod_sql_log_subscriber/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,35 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "active_record/mod_sql_log_subscriber"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "active_record-mod_sql_log_subscriber"
7
+ spec.version = ActiveRecord::ModSqlLogSubscriber::VERSION
8
+ spec.authors = ["ryu39"]
9
+ spec.email = ["dev.ryu39@gmail.com"]
10
+
11
+ spec.summary = %q{An ActiveRecord::LogSubscriber which records only mod sql.}
12
+ spec.description = %q{An ActiveRecord::LogSubscriber which records only mod sql.}
13
+ spec.homepage = "https://github.com/ryu39/active_record-mod_sql_log_subscriber"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = spec.homepage
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_runtime_dependency "activesupport", '>= 5.1.5'
30
+ spec.add_runtime_dependency "activerecord", '>= 5.1.5'
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.17"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec"
35
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_record/mod_sql_log_subscriber"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'active_support/configurable'
5
+ require 'active_support/log_subscriber'
6
+ require 'active_record'
7
+ require 'active_record/log_subscriber'
8
+
9
+ module ActiveRecord
10
+ class ModSqlLogSubscriber < ::ActiveRecord::LogSubscriber
11
+ include ActiveSupport::Configurable
12
+
13
+ VERSION = "0.0.1"
14
+
15
+ config_accessor :disable, :log_level, :log_format, :target_statements
16
+
17
+ # Default values
18
+ self.disable = false
19
+ self.log_level = :info
20
+ self.log_format = :text
21
+ self.target_statements = %w(insert update delete truncate begin commit rollback savepoint release\ savepoint)
22
+
23
+ def sql(event)
24
+ return if self.disable
25
+
26
+ payload = event.payload
27
+ sql = payload[:sql]
28
+
29
+ return if sql.nil?
30
+ return if payload[:cached]
31
+ return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
32
+ return unless target_sql_checker.match?(sql)
33
+
34
+ binds = extract_binds(payload)
35
+ send(self.log_level, formatter.call(sql, binds))
36
+ end
37
+
38
+ private
39
+
40
+ def target_sql_checker
41
+ @target_sql_checker ||= /\A\s*(#{self.target_statements.join('|')})/mi
42
+ end
43
+
44
+ def extract_binds(payload)
45
+ binds = {}
46
+ unless (payload[:binds] || []).empty?
47
+ casted_params = type_casted_binds(payload[:type_casted_binds])
48
+ payload[:binds].zip(casted_params).map do |attr, value|
49
+ key, val = render_bind(attr, value)
50
+ binds[key] = val
51
+ end
52
+ end
53
+ binds
54
+ end
55
+
56
+ def formatter
57
+ @formatter ||=
58
+ case self.log_format
59
+ when :text
60
+ ->(sql, binds) { binds.empty? ? sql : "#{sql} #{binds.inspect}" }
61
+ when :json
62
+ -> (sql, binds) { ::JSON.generate(sql: sql, binds: binds) }
63
+ when :hash
64
+ -> (sql, binds) { { sql: sql, binds: binds } }
65
+ when ::Proc
66
+ self.log_format
67
+ else
68
+ raise "Unexpected log format: #{self.log_format}"
69
+ end
70
+ end
71
+ end
72
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record-mod_sql_log_subscriber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ryu39
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 5.1.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 5.1.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.17'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.17'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
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'
83
+ description: An ActiveRecord::LogSubscriber which records only mod sql.
84
+ email:
85
+ - dev.ryu39@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - CODE_OF_CONDUCT.md
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - active_record-mod_sql_log_subscriber.gemspec
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/active_record/mod_sql_log_subscriber.rb
100
+ homepage: https://github.com/ryu39/active_record-mod_sql_log_subscriber
101
+ licenses:
102
+ - MIT
103
+ metadata:
104
+ homepage_uri: https://github.com/ryu39/active_record-mod_sql_log_subscriber
105
+ source_code_uri: https://github.com/ryu39/active_record-mod_sql_log_subscriber
106
+ changelog_uri: https://github.com/ryu39/active_record-mod_sql_log_subscriber
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.7.6
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: An ActiveRecord::LogSubscriber which records only mod sql.
127
+ test_files: []