rails_middleware_log 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f55dbcb8d941307c2bfea74da17ff9c3a2fdee91
4
+ data.tar.gz: c7e7a9173d82d27d29cbd32d55dfe49aa7d7fc91
5
+ SHA512:
6
+ metadata.gz: 7c9fa90048f8869d87f0e6cde56e5ff22d7f71be264d65fbc9f59bbc7cf9ef20ec874ed7114580eb356b78272a838396ad7461493b5b1736da32a740a909c217
7
+ data.tar.gz: bbe26794cd09dea4a5a7cda9731a05f2a9f13bd3db8f11f46cdd74e492bcb2f91ea429d400eebe29ac0b4116bb94828ba94d2b2010a1aaa6e6eac30321dc17ca
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea/*
data/.rubocop.yml ADDED
@@ -0,0 +1,19 @@
1
+ # This is the configuration used to check the rubocop source code.
2
+
3
+ AllCops:
4
+ Exclude:
5
+ - 'vendor/**/*'
6
+ - 'spec/fixtures/**/*'
7
+ - 'tmp/**/*'
8
+ TargetRubyVersion: 1.9
9
+ Documentation:
10
+ Enabled: false
11
+ Style/Encoding:
12
+ Enabled: false
13
+
14
+ Style/FrozenStringLiteralComment:
15
+ EnforcedStyle: when_needed
16
+
17
+ Metrics/MethodLength:
18
+ CountComments: false # count full line comments?
19
+ Max: 15
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ group :development, :test do
5
+ gem 'rubocop', require: false
6
+ gem 'rspec'
7
+ gem 'rails', require: false
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 fanxiaopeng
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,86 @@
1
+ # RailsMiddlewareLog
2
+
3
+ Print Log for request when every middleware start in Rails. You can set which request to log, and the content you want to print.
4
+
5
+ 在Rails的每个middleware开始的时候打印日志,可以配置那些请求,以及自定义打印内容。
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rails_middleware_log'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rails_middleware_log
22
+
23
+ ## log
24
+
25
+ ```ruby
26
+ Started GET "/path/of/request" for ::1 at 2016-02-17 10:48:55 +0800
27
+ -------ActionDispatch::ShowExceptions
28
+ -------WebConsole::Middleware
29
+ -------ActionDispatch::DebugExceptions
30
+ -------ActionDispatch::RemoteIp
31
+ -------ActionDispatch::Reloader
32
+ -------ActionDispatch::Callbacks
33
+ -------ActiveRecord::Migration::CheckPending
34
+ ActiveRecord::SchemaMigration Load (23.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
35
+ -------ActiveRecord::ConnectionAdapters::ConnectionManagement
36
+ -------ActionDispatch::Cookies
37
+ -------ActionDispatch::Session::CookieStore
38
+ -------ActionDispatch::Flash
39
+ -------ActionDispatch::ParamsParser
40
+ -------Rack::Head
41
+ -------Rack::ConditionalGet
42
+ -------Rack::ETag
43
+ -------Rack::Head
44
+
45
+ ```
46
+
47
+ ## Usage
48
+ add the following example code to application.rb
49
+ ```ruby
50
+ RailsMiddlewareLog.configure do |config|
51
+ config.target_urls = ['/coupon/v1/coupon_batches']
52
+ config.before_middleware = ->(middleware_class, env) do
53
+ puts middleware_class
54
+ end
55
+ end
56
+ ```
57
+
58
+ 1. target_urls: select target request url to log. default: log nothing
59
+ - Boolean
60
+ true will log every request
61
+ - String
62
+ equal to PATH_INFO
63
+ - Array
64
+ contains PATH_INFO
65
+ - Regexp
66
+ match PATH_INFO
67
+ 2. before_middleware:a lambda with two args, for user to custom.
68
+ - middleware_class
69
+ class of current middleware
70
+ - env
71
+ one arg of middleware call method
72
+
73
+
74
+ ## Development
75
+
76
+ After checking out the repo, run `bin/setup` to install dependencies.
77
+
78
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it ( https://github.com/[my-github-username]/rails_middleware_log/fork )
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ task.rspec_opts = ['--color', '--format', 'nested']
8
+ end
9
+
10
+ task :default => :spec
@@ -0,0 +1,29 @@
1
+ require 'rails_middleware_log/version'
2
+
3
+ module RailsMiddlewareLog
4
+ end
5
+
6
+ # load Rails/Railtie
7
+ begin
8
+ require 'rails'
9
+ rescue LoadError
10
+ # do nothing
11
+ end
12
+
13
+ $stderr.puts <<-EOC if !defined?(Rails)
14
+ warning: no framework detected.
15
+
16
+ Your Gemfile might not be configured properly.
17
+ ---- e.g. ----
18
+ Rails:
19
+ gem 'rails_middleware_log'
20
+
21
+ EOC
22
+
23
+ # load components
24
+ require 'rails_middleware_log/config'
25
+ require 'rails_middleware_log/hooks'
26
+ require 'rails_middleware_log/log'
27
+
28
+ # if not using Railtie, call `RailsMiddlewareLog::Hooks.init` directly
29
+ require 'rails_middleware_log/railtie' if defined?(Rails)
@@ -0,0 +1,23 @@
1
+ require 'active_support/configurable'
2
+ # config file
3
+ module RailsMiddlewareLog
4
+ # Configures global settings
5
+ # RailsMiddlewareLog.configure do |config|
6
+ # config.logger = Logger.new
7
+ # end
8
+ def self.configure(&_block)
9
+ yield @config ||= RailsMiddlewareLog::Configuration.new
10
+ end
11
+
12
+ # Global settings
13
+ def self.config
14
+ @config || configure { |config| config }
15
+ end
16
+
17
+ # need a Class for 3.0
18
+ class Configuration #:nodoc:
19
+ include ActiveSupport::Configurable
20
+ config_accessor(:target_urls)
21
+ config_accessor(:before_middleware)
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ # Hook file for app
2
+ module RailsMiddlewareLog
3
+ class Hooks
4
+ def self.init
5
+ return unless RailsMiddlewareLog.config.target_urls
6
+ ActiveSupport.on_load(:after_initialize) do
7
+ RailsMiddlewareLog::Log.init(Rails.configuration.middleware.middlewares)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,61 @@
1
+ # log file when processing
2
+ module RailsMiddlewareLog
3
+ class Log
4
+ class << self
5
+ def init(middlewares)
6
+ middlewares.each do |middleware|
7
+ klass = middleware_class(middleware)
8
+ if klass && klass.method_defined?(:call)
9
+ klass.class_eval do
10
+ alias_method :old_call, :call
11
+
12
+ def call(env)
13
+ RailsMiddlewareLog::Log.before_call(self.class, env)
14
+ old_call(env)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ # get the current middleware class
22
+ def middleware_class(middleware)
23
+ case middleware.klass
24
+ when ActiveSupport::Cache::Strategy::LocalCache::Middleware
25
+ middleware.klass.class
26
+ when Class
27
+ middleware.klass
28
+ else
29
+ nil
30
+ end
31
+ end
32
+
33
+ def before_call(k_class, env)
34
+ if config_url_match?(env['PATH_INFO'])
35
+ if block = RailsMiddlewareLog.config.before_middleware
36
+ block.call(k_class, env)
37
+ else
38
+ Rails.logger.info "-------#{k_class.name}"
39
+ end
40
+ end
41
+ end
42
+
43
+ # check current request
44
+ def config_url_match?(url)
45
+ target_urls = RailsMiddlewareLog.config.target_urls
46
+ case target_urls
47
+ when true
48
+ true
49
+ when String
50
+ target_urls == url
51
+ when Array
52
+ target_urls.include? url
53
+ when Regexp
54
+ target_urls === url
55
+ else
56
+ false
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,7 @@
1
+ module RailsMiddlewareLog
2
+ class Railtie < ::Rails::Railtie #:nodoc:
3
+ initializer 'rails_middleware_log' do |_app|
4
+ RailsMiddlewareLog::Hooks.init
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module RailsMiddlewareLog
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_middleware_log/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rails_middleware_log'
8
+ spec.version = RailsMiddlewareLog::VERSION
9
+ spec.authors = ['fanxiaopeng']
10
+ spec.email = ['fanxiaopeng515@163.com']
11
+
12
+ spec.summary = 'log every middleware of rails'
13
+ spec.description = 'Print Log for request when every middleware start in Rails. You can set which request to log, and the content you want to print.'
14
+ spec.homepage = 'https://github.com/fanxiaopeng/rails_middleware_log'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split("\n")
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.9'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 0'
25
+ end
data/spec/log_spec.rb ADDED
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+ require 'active_support'
3
+ require 'rails'
4
+ require 'rails_middleware_log/log'
5
+ require 'rails_middleware_log/config'
6
+ describe 'RailsMiddlewareLog::Log' do
7
+ before do
8
+
9
+ class MiddleClass1
10
+ def call
11
+ end
12
+ end
13
+ class Middle1
14
+ def klass
15
+ MiddleClass1
16
+ end
17
+ end
18
+ @middle1 = Middle1.new
19
+
20
+ class Middle2
21
+ def klass
22
+ ActiveSupport::Cache::Strategy::LocalCache::Middleware.new('', '')
23
+ end
24
+ end
25
+
26
+ @middle2 = Middle2.new
27
+ end
28
+ describe '.init' do
29
+ it 'add old_call method success' do
30
+ RailsMiddlewareLog::Log.init([@middle1, @middle2])
31
+ @middle1.klass.method_defined?(:old_call).equal? true
32
+ @middle2.klass.class.method_defined?(:old_call).equal? true
33
+ end
34
+ end
35
+
36
+ describe '.before_call' do
37
+ before do
38
+ @env = { 'PATH_INFO' => '/api/path' }
39
+
40
+ module Rails
41
+ def self.logger
42
+ Logger.new(STDOUT)
43
+ end
44
+ end
45
+ end
46
+ describe 'target_urls match null' do
47
+ it 'exec null log' do
48
+ RailsMiddlewareLog.configure do |config|
49
+ config.target_urls = '/error/path'
50
+ end
51
+ result = RailsMiddlewareLog::Log.before_call(MiddleClass1, @env)
52
+ expect(result).to be_nil
53
+ end
54
+ end
55
+
56
+ describe 'no config target_urls' do
57
+ it 'exec null log' do
58
+ result = RailsMiddlewareLog::Log.before_call(MiddleClass1, @env)
59
+ expect(result).to be_nil
60
+ end
61
+ end
62
+
63
+ describe 'config before_middleware' do
64
+ it 'exec before_middleware code' do
65
+ RailsMiddlewareLog.configure do |config|
66
+ config.target_urls = '/api/path'
67
+ config.before_middleware = ->(name, env){
68
+ env['PATH_INFO']
69
+ }
70
+ end
71
+ result = RailsMiddlewareLog::Log.before_call(MiddleClass1, @env)
72
+ expect(result).to eq @env['PATH_INFO']
73
+ end
74
+ end
75
+
76
+ describe 'exec default log' do
77
+ it 'request url match String url' do
78
+ RailsMiddlewareLog.configure do |config|
79
+ config.target_urls = '/api/path'
80
+ end
81
+ result = RailsMiddlewareLog::Log.before_call(MiddleClass1, @env)
82
+ expect(result).to be_truthy
83
+ end
84
+ it 'request url match Boolean url' do
85
+ RailsMiddlewareLog.configure do |config|
86
+ config.target_urls = true
87
+ end
88
+ result = RailsMiddlewareLog::Log.before_call(MiddleClass1, @env)
89
+ expect(result).to be_truthy
90
+ end
91
+ it 'request url match Array url' do
92
+ RailsMiddlewareLog.configure do |config|
93
+ config.target_urls = ['/api/path']
94
+ end
95
+ result = RailsMiddlewareLog::Log.before_call(MiddleClass1, @env)
96
+ expect(result).to be_truthy
97
+ end
98
+
99
+ it 'request url match Regexp url' do
100
+ RailsMiddlewareLog.configure do |config|
101
+ config.target_urls = /api/
102
+ end
103
+ result = RailsMiddlewareLog::Log.before_call(MiddleClass1, @env)
104
+ expect(result).to be_truthy
105
+ end
106
+ end
107
+ end
108
+ end
File without changes
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_middleware_log
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - fanxiaopeng
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Print Log for request when every middleware start in Rails. You can set
56
+ which request to log, and the content you want to print.
57
+ email:
58
+ - fanxiaopeng515@163.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".idea/.name"
65
+ - ".idea/.rakeTasks"
66
+ - ".idea/misc.xml"
67
+ - ".idea/modules.xml"
68
+ - ".idea/rails_middleware_log.iml"
69
+ - ".idea/vcs.xml"
70
+ - ".rubocop.yml"
71
+ - ".travis.yml"
72
+ - CODE_OF_CONDUCT.md
73
+ - Gemfile
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - lib/rails_middleware_log.rb
78
+ - lib/rails_middleware_log/config.rb
79
+ - lib/rails_middleware_log/hooks.rb
80
+ - lib/rails_middleware_log/log.rb
81
+ - lib/rails_middleware_log/railtie.rb
82
+ - lib/rails_middleware_log/version.rb
83
+ - rails_middleware_log.gemspec
84
+ - spec/log_spec.rb
85
+ - spec/spec_helper.rb
86
+ homepage: https://github.com/fanxiaopeng/rails_middleware_log
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.4.8
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: log every middleware of rails
110
+ test_files: []
111
+ has_rdoc: