rack_logger 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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +33 -0
- data/Rakefile +2 -0
- data/lib/rack_logger/version.rb +3 -0
- data/lib/rack_logger.rb +58 -0
- data/rack_logger.gemspec +22 -0
- metadata +91 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Allen Wei
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# RackLogger
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
gem 'rack_logger'
|
8
|
+
|
9
|
+
And then execute:
|
10
|
+
|
11
|
+
$ bundle
|
12
|
+
|
13
|
+
Or install it yourself as:
|
14
|
+
|
15
|
+
$ gem install rack_logger
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
use RackLogger, LOGGER, {:label => "ActiveRecord", :class => ActiveRecord::LogSubscriber}
|
20
|
+
|
21
|
+
will generate two logs per request:
|
22
|
+
|
23
|
+
Started GET "http://localhost:3000/ping" for 127.0.0.1 at 2012-08-13 23:22:23 +0800 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.77 Safari/537.1
|
24
|
+
|
25
|
+
Completed GET http://localhost:3456/ping 200 in 0.76ms (ActiveRecord: 0.30ms)
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/rack_logger.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require "rack_logger/version"
|
2
|
+
|
3
|
+
require 'cgi'
|
4
|
+
# Completed 404 Not Found in 105ms (Views: 4.6ms | ActiveRecord: 2.3ms | Sphinx: 0.0ms | Gateway: 86.3ms)
|
5
|
+
class RackLogger
|
6
|
+
attr_reader :logger, :log_subscribers
|
7
|
+
# log_subscribers:
|
8
|
+
# [
|
9
|
+
# {
|
10
|
+
# :label => "label",
|
11
|
+
# :class => ActiveRecord::LogSubscriber
|
12
|
+
# }
|
13
|
+
# ]
|
14
|
+
def initialize(app, logger, *log_subscribers)
|
15
|
+
@app = app
|
16
|
+
@logger = logger
|
17
|
+
@log_subscribers = log_subscribers
|
18
|
+
end
|
19
|
+
|
20
|
+
def call(env)
|
21
|
+
dup._call(env)
|
22
|
+
end
|
23
|
+
|
24
|
+
def _call(env)
|
25
|
+
begin
|
26
|
+
request = Rack::Request.new(env)
|
27
|
+
time_start = Time.now
|
28
|
+
logger.info "Started #{request.request_method} \"#{request.url}\" for #{request.ip} at #{Time.now} #{request.user_agent}"
|
29
|
+
status, header, body = @app.call(env)
|
30
|
+
# million seconds
|
31
|
+
time_cost = (Time.now - time_start) * 1000
|
32
|
+
header["x-runtime"] = "%.2fms" % time_cost
|
33
|
+
|
34
|
+
[status, header, body]
|
35
|
+
ensure
|
36
|
+
benchmarks = []
|
37
|
+
|
38
|
+
time_cost ||= (Time.now - time_start) * 1000
|
39
|
+
|
40
|
+
log_subscribers.each do |subscriber|
|
41
|
+
label = subscriber[:label]
|
42
|
+
subscriber_class = subscriber[:class]
|
43
|
+
|
44
|
+
if t = subscriber_class.send(:runtime)
|
45
|
+
benchmarks << "#{label}: %.2fms" % t
|
46
|
+
subscriber_class.send(:reset_runtime)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
msg = "Completed #{request.request_method} #{ CGI.unescape(request.url)} #{status} in %.2fms" % time_cost
|
51
|
+
msg << " ("
|
52
|
+
msg << benchmarks.join(' | ')
|
53
|
+
msg << ") "
|
54
|
+
msg << "\n\n"
|
55
|
+
logger.info msg
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/rack_logger.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rack_logger/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Allen Wei"]
|
6
|
+
gem.email = ["digruby@gmail.com"]
|
7
|
+
gem.description = %q{Rack logger support ActiveSupport LogSubscriber}
|
8
|
+
gem.summary = %q{Rack logger support ActiveSupport LogSubscriber}
|
9
|
+
gem.homepage = "https://github.com/allenwei/rack-logger"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "rack_logger"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = RackLogger::VERSION
|
17
|
+
|
18
|
+
|
19
|
+
gem.add_runtime_dependency 'rack'
|
20
|
+
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack_logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Allen Wei
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Rack logger support ActiveSupport LogSubscriber
|
47
|
+
email:
|
48
|
+
- digruby@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/rack_logger.rb
|
59
|
+
- lib/rack_logger/version.rb
|
60
|
+
- rack_logger.gemspec
|
61
|
+
homepage: https://github.com/allenwei/rack-logger
|
62
|
+
licenses: []
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
hash: 4491364392232407222
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
hash: 4491364392232407222
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.8.24
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Rack logger support ActiveSupport LogSubscriber
|
91
|
+
test_files: []
|