rack-simple_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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cdeacb05b7b9ef32c4ca054b779605f82fb2735b
4
+ data.tar.gz: 403ca7a567112c8e266223b1f3d850ee570f4fdb
5
+ SHA512:
6
+ metadata.gz: f8bbf3633a24a4f93c6720af50ace4f5bd33667944fae194a0a7553ceda5fd896cfe14ebb920fb04ccd505970f80bb61c4d8707524720cdb1bcd68a230eea29e
7
+ data.tar.gz: 3ebbc796fceb3015091c4f080b1e1df3c6c9c55f6c85a1b088bd4f3d7c51806d34af4b5634d21292d33a574c3093c71361389717555d1906694a908f9790defc
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-simple_logger.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'coveralls', require: false
8
+ gem 'simplecov', require: false
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 i2bskn
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,54 @@
1
+ # Rack::SimpleLogger
2
+
3
+ Simple logger for rack.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-simple_logger'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-simple_logger
18
+
19
+ ## Usage
20
+
21
+ Include as Rack middleware to config.ru.
22
+
23
+ for local file:
24
+
25
+ ```ruby
26
+ require "rack_application"
27
+ require "rack/simple_logger"
28
+
29
+ use Rack::SimpleLogger, log: File.expand_path("../log/production.log", __FILE__)
30
+ run RackApplication.new
31
+ ```
32
+
33
+ for MongoDB:
34
+
35
+ ```ruby
36
+ require "rack_application"
37
+ require "mongo"
38
+ require "rack/simple_logger"
39
+
40
+ client = Mongo::Connection.new("localhost", 27017)
41
+ database = client["logs"]
42
+ collection = database["racklog"]
43
+
44
+ use Rack::SimpleLogger, log: collection
45
+ run RackApplication.new
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ desc "Run all specs"
5
+ RSpec::Core::RakeTask.new(:spec) do |t|
6
+ t.rspec_opts = ["-c", "-fs"]
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,46 @@
1
+ # coding: utf-8
2
+
3
+ module Rack
4
+ class LogProxy
5
+ def initialize(logger)
6
+ case logger.class.to_s
7
+ when "Logger"
8
+ @logger = logger
9
+ @log_type = :logger
10
+ when "String", "IO"
11
+ @logger = ::Logger.new(logger)
12
+ @log_type = :logger
13
+ when "Mongo::Collection"
14
+ @logger = logger
15
+ @log_type = :mongo
16
+ else
17
+ @logger = logger
18
+ @log_type = :other
19
+ end
20
+
21
+ logger_formatter if @log_type == :logger
22
+ end
23
+
24
+ def logger_formatter
25
+ @logger.formatter = Proc.new do |severity, datetime, progname, msg|
26
+ "#{msg}\n"
27
+ end
28
+ end
29
+
30
+ def write(log_hash)
31
+ send("write_#{@log_type}", log_hash)
32
+ end
33
+
34
+ def write_logger(log_hash)
35
+ @logger.info log_hash.map{|k,v| [k, v].join(":")}.join("\t")
36
+ end
37
+
38
+ def write_mongo(log_hash)
39
+ @logger.insert log_hash
40
+ end
41
+
42
+ def write_other(log_hash)
43
+ @logger.write log_hash
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class SimpleLogger
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+
3
+ require "logger"
4
+
5
+ require "rack/simple_logger/version"
6
+ require "rack/simple_logger/log_proxy"
7
+
8
+ module Rack
9
+ class SimpleLogger
10
+ def initialize(app, options={})
11
+ options[:log] ||= STDOUT
12
+ @logger = LogProxy.new(options[:log])
13
+ @app = app
14
+ end
15
+
16
+ def call(env)
17
+ began_at = Time.now
18
+ status, header, body = @app.call(env)
19
+ log(env, status, header, began_at)
20
+ [status, header, body]
21
+ end
22
+
23
+ private
24
+ def log(env, status, header, began_at)
25
+ @logger.write(
26
+ xff: env["HTTP_X_FORWARDED_FOR"] || "-",
27
+ host: env["REMOTE_ADDR"],
28
+ time: began_at.strftime("%Y-%m-%d %H:%M:%S"),
29
+ method: env["REQUEST_METHOD"],
30
+ path: env["PATH_INFO"],
31
+ query_strings: env["QUERY_STRING"] || "-",
32
+ status: status,
33
+ ua: env["HTTP_USER_AGENT"],
34
+ res_size: header["Content-Length"],
35
+ app_time: Time.now - began_at
36
+ )
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/simple_logger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-simple_logger"
8
+ spec.version = Rack::SimpleLogger::VERSION
9
+ spec.authors = ["i2bskn"]
10
+ spec.email = ["i2bskn@gmail.com"]
11
+ spec.description = %q{Simple logger for rack}
12
+ spec.summary = %q{Simple logger for rack}
13
+ spec.homepage = "https://github.com/i2bskn/rack-simple_logger"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_dependency "rack"
26
+ end
@@ -0,0 +1,3 @@
1
+ # coding: utf-8
2
+
3
+ require "spec_helper"
@@ -0,0 +1,15 @@
1
+ require "simplecov"
2
+ require "coveralls"
3
+ Coveralls.wear!
4
+
5
+ # SimpleCov.formatter = SimpleCov::Formatter::HTMLFormatter
6
+ SimpleCov.start do
7
+ add_filter "spec"
8
+ add_filter ".bundle"
9
+ end
10
+
11
+ require "rack/simple_logger"
12
+
13
+ RSpec.configure do |config|
14
+ config.order = "random"
15
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-simple_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - i2bskn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-23 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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
+ - !ruby/object:Gem::Dependency
56
+ name: rack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Simple logger for rack
70
+ email:
71
+ - i2bskn@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/rack/simple_logger.rb
82
+ - lib/rack/simple_logger/log_proxy.rb
83
+ - lib/rack/simple_logger/version.rb
84
+ - rack-simple_logger.gemspec
85
+ - spec/rack/simple_logger_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: https://github.com/i2bskn/rack-simple_logger
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.0
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Simple logger for rack
111
+ test_files:
112
+ - spec/rack/simple_logger_spec.rb
113
+ - spec/spec_helper.rb