rack-common_logger-fluent 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ #
9
+ gem 'fluent-logger'
10
+ gem 'rack'
11
+
12
+ group :development do
13
+ gem "rspec", "~> 2.8.0"
14
+ gem "rdoc", "~> 3.12"
15
+ gem "bundler"
16
+ gem "jeweler", "~> 1.8.3"
17
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Keiji, Yoshimi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,41 @@
1
+ = rack-common_logger-fluent
2
+
3
+ rack middleware for writing access log to fluentd.
4
+
5
+ == Example
6
+ # in your config.ru
7
+
8
+ require 'fluent-logger'
9
+ require 'rack/common_logger/fluent'
10
+
11
+ logger = Fluent::Logger::FluentLogger.new(nil, :host => 'localhost', :port => 24224)
12
+ use Rack::CommonLogger::Fluent, 'myapp', logger
13
+
14
+ # if you want to customize format
15
+ use Rack::CommonLogger::Fluent, 'myapp', logger do |info|
16
+ result = {}
17
+
18
+ # ...
19
+
20
+ result
21
+ end
22
+
23
+ == WARN
24
+
25
+ THIS PROJECT is early development. compatibility may break.
26
+
27
+ == Contributing to rack-common_logger-fluent
28
+
29
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
30
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
31
+ * Fork the project.
32
+ * Start a feature/bugfix branch.
33
+ * Commit and push until you are happy with your contribution.
34
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
35
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
36
+
37
+ == Copyright
38
+
39
+ Copyright (c) 2012 Keiji, Yoshimi. See LICENSE.txt for
40
+ further details.
41
+
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "rack-common_logger-fluent"
18
+ gem.homepage = "http://github.com/walf443/rack-common_logger-fluent"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{rack middleware for writing access log to fluentd. }
21
+ gem.description = %Q{rack middleware for writing access log to fluentd. }
22
+ gem.email = "walf443@gmail.com"
23
+ gem.authors = ["Keiji, Yoshimi"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ task :default => :spec
35
+
36
+ require 'rdoc/task'
37
+ Rake::RDocTask.new do |rdoc|
38
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
39
+
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "rack-common_logger-fluent #{version}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,2 @@
1
+ # just alias.
2
+ require 'rack/common_logger/fluent'
@@ -0,0 +1,102 @@
1
+ require 'fluent-logger'
2
+ require 'time'
3
+
4
+ module Rack
5
+ class CommonLogger
6
+ #
7
+ # @example
8
+ # # in your config.ru
9
+ #
10
+ # require 'fluent-logger'
11
+ # require 'rack/common_logger/fluent'
12
+ #
13
+ # logger = Fluent::Logger::FluentLogger.new(nil, :host => 'localhost', :port => 24224)
14
+ # use Rack::CommonLogger::Fluent, 'myapp.access', logger
15
+ # # will output to log
16
+ # # #=> 2012-05-12T17:56:50+09:00 myapp.access {"remote_addr":"127.0.0.1","date":"2012-05-12T17:56:50+09:00","request_method":"GET","path_info":"/","query_string":"?body=1","http_version":"HTTP/1.1","http_status":304,"user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19","content_type":null,"content_length":null,"runtime":0.007976}
17
+ #
18
+ # # if you want to customize format
19
+ # use Rack::CommonLogger::Fluent, 'myapp', logger do |info|
20
+ # result = {}
21
+ #
22
+ # # ...
23
+ #
24
+ # result
25
+ # end
26
+ #
27
+ #
28
+ class Fluent
29
+ # tag is Fluent::Logger's tag for access log.
30
+ # +logger+ should implement post(+tag+, +message) method. logger is a Fluent::Logger::FluentLogger object.
31
+ # +format+ is block that take Hash that has +env+(Rack's env), +status+, +header+, +now+, +runtime+, and should return Hash that contain access_log element.
32
+ # +format+ is optional and use +default_format+ as default.
33
+ def initialize(app, tag, logger=nil, &format)
34
+ @app = app
35
+ @logger = logger || ::Fluent::Logger::FluentLogger.new(nil, :host => 'localhost', :port => 24224)
36
+ @tag = tag
37
+ @format = format || lambda do |info|
38
+ self.default_format(info)
39
+ end
40
+ end
41
+
42
+ # run application and post access information to fluentd.
43
+ #
44
+ # This middleware set @logger to rack.fluent_logger of env.
45
+ # So your application can post to fluentd like this.
46
+ # def call(env)
47
+ # env["fluent_logger"].post("app.log", { "message": "xxxxxx" })
48
+ # # ...
49
+ # end
50
+ #
51
+ def call(env)
52
+ env["rack.fluent_logger"] = @logger
53
+ began_at = Time.now
54
+ status, header, body = @app.call(env)
55
+ header = Utils::HeaderHash.new(header)
56
+ body = BodyProxy.new(body) { log(env, status, header, began_at) }
57
+ [status, header, body]
58
+ end
59
+
60
+ # This method return default_format.
61
+ # hostname: HTTP_HOST or SERVER_NAME
62
+ # remote_addr: HTTP_X_FORWARDED_FOR or REMOTE_ADDR
63
+ # date: iso8601 datetime string.
64
+ # request_method: rack's REQUEST_METHOD
65
+ # path_info: rack's PATH_INFO
66
+ # query_string: rack's QUERY_STRING
67
+ # http_version: rack's HTTP_VERSION
68
+ # http_status: response code. (Fixnum)
69
+ # user_agent: User-Agent request header.
70
+ # content_type: Content-Type response header.
71
+ # content_length: Content-Length response header.(Fixnum)
72
+ # runtime: seconds of application running. (Float)
73
+ #
74
+ def default_format(info)
75
+ hash = {}
76
+
77
+ hash["hostname"] = info[:env]["HTTP_HOST"] || info[:env]["SERVER_NAME"]
78
+ hash["remote_addr"] = info[:env]["HTTP_X_FORWARDED_FOR"] || info[:env]["REMOTE_ADDR"] || nil
79
+ hash["date"] = info[:now].iso8601
80
+ hash["request_method"] = info[:env]["REQUEST_METHOD"]
81
+ hash["path_info"] = info[:env]["PATH_INFO"].gsub("%2F", '/') # some case "/" to be %2F.
82
+ hash["query_string"] = info[:env]["QUERY_STRING"].empty? ? "" : '?' + info[:env]["QUERY_STRING"]
83
+ hash["http_version"] = info[:env]["HTTP_VERSION"]
84
+ hash["http_status"] = info[:status].to_s[0..3].to_i
85
+ hash["user_agent"] = info[:env]["HTTP_USER_AGENT"]
86
+ hash["content_type"] = info[:header]["Content-Type"]
87
+ hash["content_length"] = info[:header]["Content-Length"] ? info[:header]["Content-Length"].to_i : nil
88
+ hash["runtime"] = info[:runtime]
89
+
90
+ hash
91
+ end
92
+
93
+ def log(env, status, header, began_at)
94
+ now = Time.now
95
+ result = @format.call({ :env => env, :status => status, :header => header, :now => now, :runtime => now - began_at })
96
+ @logger.post(@tag, result)
97
+ end
98
+
99
+ end
100
+ end
101
+ end
102
+
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "rack-common_logger-fluent"
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Keiji, Yoshimi"]
12
+ s.date = "2012-05-12"
13
+ s.description = "rack middleware for writing access log to fluentd. "
14
+ s.email = "walf443@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/rack-common_logger-fluent.rb",
28
+ "lib/rack/common_logger/fluent.rb",
29
+ "rack-common_logger-fluent.gemspec",
30
+ "spec/rack-common_logger-fluent_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = "http://github.com/walf443/rack-common_logger-fluent"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.21"
37
+ s.summary = "rack middleware for writing access log to fluentd."
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<fluent-logger>, [">= 0"])
44
+ s.add_runtime_dependency(%q<rack>, [">= 0"])
45
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
46
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
47
+ s.add_development_dependency(%q<bundler>, [">= 0"])
48
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
49
+ else
50
+ s.add_dependency(%q<fluent-logger>, [">= 0"])
51
+ s.add_dependency(%q<rack>, [">= 0"])
52
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
53
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
54
+ s.add_dependency(%q<bundler>, [">= 0"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<fluent-logger>, [">= 0"])
59
+ s.add_dependency(%q<rack>, [">= 0"])
60
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
61
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
62
+ s.add_dependency(%q<bundler>, [">= 0"])
63
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
64
+ end
65
+ end
66
+
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "RackCommonLoggerFluent" do
4
+ before do
5
+ body = "foobar"
6
+ @app = Rack::Lint.new(lambda {|env|
7
+ [200, { "Content-Type" => "text/html", "Content-Length" => body.size.to_s }, [body] ]
8
+ })
9
+ end
10
+
11
+ it do
12
+ logger = Fluent::Logger::TestLogger.new
13
+ res = Rack::MockRequest.new(Rack::CommonLogger::Fluent.new(@app, 'myapp', logger)).get('/')
14
+ logger.queue.size.should == 1
15
+ message = logger.queue.first
16
+ message["hostname"].should == "example.org"
17
+ message["content_length"].should == 6
18
+ message["content_type"].should == "text/html"
19
+ message["request_method"].should == "GET"
20
+ message["path_info"].should == "/"
21
+ message["http_status"].should == 200
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'rack-common_logger-fluent'
5
+ require 'rack/lint'
6
+ require 'rack/mock'
7
+
8
+ # Requires supporting files with custom matchers and macros, etc,
9
+ # in ./support/ and its subdirectories.
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
11
+
12
+ RSpec.configure do |config|
13
+
14
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-common_logger-fluent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Keiji, Yoshimi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fluent-logger
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: rack
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
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
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 2.8.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.8.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: rdoc
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '3.12'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '3.12'
78
+ - !ruby/object:Gem::Dependency
79
+ name: bundler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: jeweler
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.8.3
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.8.3
110
+ description: ! 'rack middleware for writing access log to fluentd. '
111
+ email: walf443@gmail.com
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files:
115
+ - LICENSE.txt
116
+ - README.rdoc
117
+ files:
118
+ - .document
119
+ - .rspec
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.rdoc
123
+ - Rakefile
124
+ - VERSION
125
+ - lib/rack-common_logger-fluent.rb
126
+ - lib/rack/common_logger/fluent.rb
127
+ - rack-common_logger-fluent.gemspec
128
+ - spec/rack-common_logger-fluent_spec.rb
129
+ - spec/spec_helper.rb
130
+ homepage: http://github.com/walf443/rack-common_logger-fluent
131
+ licenses:
132
+ - MIT
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ segments:
144
+ - 0
145
+ hash: -4004731729266765288
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ! '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 1.8.21
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: rack middleware for writing access log to fluentd.
158
+ test_files: []