simple-rack-logger 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,9 +4,10 @@ module Rack
4
4
  class RequestLogger
5
5
  attr_reader :logger
6
6
 
7
- def initialize(app, logger)
8
- @app = app
9
- @logger = logger
7
+ def initialize(app, logger, *filters)
8
+ @app = app
9
+ @logger = logger
10
+ @filters = filters
10
11
 
11
12
  def @logger.flush
12
13
  # Chamando o flush diretamente, pois está demorando para descarregar a memória
@@ -14,18 +15,23 @@ module Rack
14
15
  logdev = self.instance_variable_get(:@logdev)
15
16
  logdev.instance_variable_get(:@dev).flush if logdev
16
17
  end
17
-
18
18
  end
19
19
 
20
20
  def call(env)
21
- logger.info(expose(Rack::Request.new(env)))
22
- logger.flush
21
+ request = Rack::Request.new(env)
22
+ block = @filters.select { |filter| filter.respond_to?(:filter) && filter.send(:filter, request) }
23
+
24
+ if block.empty?
25
+ logger.info(expose(request))
26
+ logger.flush
27
+ end
28
+
23
29
  @app.call(env)
24
30
  end
25
31
 
26
32
  private
27
33
  def expose(request)
28
- params = request.params.map{|key, value| "#{key}=#{value}"}
34
+ params = request.params.map { |key, value| "#{key}=#{value}" }
29
35
  "#{request.request_method}: #{request.url} for #{request.ip} with #{request.user_agent} [params: #{params.join('; ')}]"
30
36
  end
31
37
 
@@ -1,3 +1,3 @@
1
1
  module SimpleRackLogger
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -3,16 +3,12 @@ require 'spec_helper'
3
3
  describe Rack::RequestLogger do
4
4
 
5
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)
6
+ let(:app) {
7
+ mock(Struct.new(:none))
11
8
  }
12
9
 
13
- it 'log request information from env' do
14
-
15
- env = {
10
+ let(:env) do
11
+ {
16
12
  "GATEWAY_INTERFACE" => "CGI/1.1",
17
13
  "PATH_INFO" => "/v1/crachas.xml",
18
14
  "QUERY_STRING" => "",
@@ -37,10 +33,35 @@ describe Rack::RequestLogger do
37
33
  "rack.run_once" => false,
38
34
  "rack.url_scheme" => "http"
39
35
  }
36
+ end
37
+
38
+ context 'inicial state without filter' do
39
+ subject {
40
+ Rack::RequestLogger.new(app, logger)
41
+ }
42
+
43
+ it 'log request information from env' do
44
+ app.should_receive(:call)
45
+ 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: ]')
46
+ subject.call(env)
47
+ end
48
+ end
49
+
50
+ context 'inicial state with filter' do
51
+ subject {
52
+ filter = Object.new
53
+ def filter.filter(request)
54
+ request.ip =~/127.0.0.1/
55
+ end
56
+ Rack::RequestLogger.new(app, logger, filter)
57
+ }
40
58
 
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: ]')
59
+ it 'dont log request information from env' do
60
+ app.should_receive(:call)
61
+ logger.should_not_receive(:info)
62
+ subject.call(env)
63
+ end
42
64
 
43
- subject.call(env)
44
65
  end
45
66
 
46
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-rack-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-09 00:00:00.000000000 Z
12
+ date: 2012-11-12 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A simple rack for log information from environment.
15
15
  email: