simple-rack-logger 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
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
18
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_rack_logger.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ gem 'guard-rspec'
9
+ gem 'rb-inotify'
10
+ gem 'guard-bundler'
11
+ gem 'rack-test'
12
+ end
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'rspec', cli: "--color --format nested" do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 $GIT_AUTHOR_NAME
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,31 @@
1
+ # SimpleRackLogger
2
+
3
+ A simple rack for log information from environment.
4
+
5
+ For use in Rack application to log information from environment. Special from request data.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'simple_rack_logger'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install simple_rack_logger
20
+
21
+ ## Usage
22
+
23
+ use Rack::RequestLogger, Logger.new(File.open("log/development.log", File::WRONLY | File::APPEND))
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,4 @@
1
+ require "simple_rack_logger/version"
2
+ require 'cgi'
3
+
4
+ require 'simple_rack_logger/request_logger'
@@ -0,0 +1,33 @@
1
+ #encoding: utf-8
2
+ module Rack
3
+
4
+ class RequestLogger
5
+ attr_reader :logger
6
+
7
+ def initialize(app, logger)
8
+ @app = app
9
+ @logger = logger
10
+
11
+ def @logger.flush
12
+ # Chamando o flush diretamente, pois está demorando para descarregar a memória
13
+ # além de não ter a opção de executar
14
+ logdev = self.instance_variable_get(:@logdev)
15
+ logdev.instance_variable_get(:@dev).flush if logdev
16
+ end
17
+
18
+ end
19
+
20
+ def call(env)
21
+ logger.info(expose(Rack::Request.new(env)))
22
+ logger.flush
23
+ @app.call(env)
24
+ end
25
+
26
+ private
27
+ def expose(request)
28
+ params = request.params.map{|key, value| "#{key}=#{value}"}
29
+ "#{request.request_method}: #{request.url} for #{request.ip} with #{request.user_agent} [params: #{params.join('; ')}]"
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleRackLogger
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_rack_logger/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "simple-rack-logger"
8
+ gem.version = SimpleRackLogger::VERSION
9
+ gem.authors = ["Adriano Dadario"]
10
+ gem.email = ["dadario@gmail.com"]
11
+ gem.description = %q{A simple rack for log information from environment.}
12
+ gem.summary = %q{For use in Rack application to log information from environment. Special from request data}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::RequestLogger do
4
+
5
+ let(:logger) { logger = double(Logger) }
6
+
7
+ subject {
8
+ app = mock(Struct.new(:none))
9
+ app.should_receive(:call)
10
+ Rack::RequestLogger.new(app, logger)
11
+ }
12
+
13
+ it 'log request information from env' do
14
+
15
+ env = {
16
+ "GATEWAY_INTERFACE" => "CGI/1.1",
17
+ "PATH_INFO" => "/v1/crachas.xml",
18
+ "QUERY_STRING" => "",
19
+ "REMOTE_ADDR" => "127.0.0.1",
20
+ "REMOTE_HOST" => "localhost",
21
+ "REQUEST_METHOD" => "GET",
22
+ "REQUEST_URI" => "http://localhost:9393/v1/crachas.xml",
23
+ "SCRIPT_NAME" => "",
24
+ "SERVER_NAME" => "localhost",
25
+ "SERVER_PORT" => "9393",
26
+ "SERVER_PROTOCOL" => "HTTP/1.1",
27
+ "SERVER_SOFTWARE" => "WEBrick/1.3.1 (Ruby/1.9.3/2012-10-12)",
28
+ "HTTP_USER_AGENT" => "curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3",
29
+ "HTTP_HOST" => "localhost:9393",
30
+ "HTTP_ACCEPT" => "*/*",
31
+ "HTTP_VERSION" => "HTTP/1.1",
32
+ "REQUEST_PATH" => "/v1/crachas.xml",
33
+ "rack.input" => "",
34
+ "rack.errors" => "",
35
+ "rack.multithread" => true,
36
+ "rack.multiprocess" => false,
37
+ "rack.run_once" => false,
38
+ "rack.url_scheme" => "http"
39
+ }
40
+
41
+ logger.should_receive(:info).with('GET: http://localhost:9393/v1/crachas.xml for 127.0.0.1 with curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3 [params: ]')
42
+
43
+ subject.call(env)
44
+ end
45
+
46
+ end
@@ -0,0 +1,9 @@
1
+ require 'rspec'
2
+ require 'rack/test'
3
+ require 'logger'
4
+ require_relative '../lib/simple-rack-logger'
5
+
6
+ RSpec.configure do |config|
7
+ config.include Rack::Test::Methods
8
+ config.mock_framework = :rspec
9
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-rack-logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adriano Dadario
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple rack for log information from environment.
15
+ email:
16
+ - dadario@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - Guardfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/simple-rack-logger.rb
28
+ - lib/simple_rack_logger/request_logger.rb
29
+ - lib/simple_rack_logger/version.rb
30
+ - simple-rack-logger.gemspec
31
+ - spec/simple_rack_logger/request_logger_spec.rb
32
+ - spec/spec_helper.rb
33
+ homepage: ''
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.24
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: For use in Rack application to log information from environment. Special
57
+ from request data
58
+ test_files:
59
+ - spec/simple_rack_logger/request_logger_spec.rb
60
+ - spec/spec_helper.rb