http_logger 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,6 +2,7 @@ source "http://rubygems.org"
2
2
 
3
3
 
4
4
  group :development do
5
+ gem "debugger", :platform => "ruby_19"
5
6
  gem "rspec"
6
7
  gem "bundler"
7
8
  gem "jeweler"
data/Gemfile.lock CHANGED
@@ -1,6 +1,14 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ columnize (0.3.6)
5
+ debugger (1.2.1)
6
+ columnize (>= 0.3.1)
7
+ debugger-linecache (~> 1.1.1)
8
+ debugger-ruby_core_source (~> 1.1.4)
9
+ debugger-linecache (1.1.2)
10
+ debugger-ruby_core_source (>= 1.1.1)
11
+ debugger-ruby_core_source (1.1.4)
4
12
  diff-lcs (1.1.3)
5
13
  fakeweb (1.3.0)
6
14
  git (1.2.5)
@@ -27,6 +35,7 @@ PLATFORMS
27
35
 
28
36
  DEPENDENCIES
29
37
  bundler
38
+ debugger
30
39
  fakeweb
31
40
  jeweler
32
41
  rspec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.3.0
data/http_logger.gemspec CHANGED
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = "http_logger"
8
- s.version = "0.2.5"
7
+ s.name = %q{http_logger}
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bogdan Gusiev"]
12
- s.date = "2013-01-21"
13
- s.description = "This gem keep an eye on every Net::HTTP library usage and dump all request and response data to the log file"
14
- s.email = "agresso@gmail.com"
12
+ s.date = %q{2013-02-12}
13
+ s.description = %q{This gem keep an eye on every Net::HTTP library usage and dump all request and response data to the log file}
14
+ s.email = %q{agresso@gmail.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt"
17
17
  ]
@@ -32,27 +32,30 @@ Gem::Specification.new do |s|
32
32
  "spec/http_logger_spec.rb",
33
33
  "spec/spec_helper.rb"
34
34
  ]
35
- s.homepage = "http://github.com/bogdan/http_logger"
35
+ s.homepage = %q{http://github.com/bogdan/http_logger}
36
36
  s.licenses = ["MIT"]
37
37
  s.require_paths = ["lib"]
38
- s.rubygems_version = "1.8.24"
39
- s.summary = "Log your http api calls just like SQL queries"
38
+ s.rubygems_version = %q{1.6.0}
39
+ s.summary = %q{Log your http api calls just like SQL queries}
40
40
 
41
41
  if s.respond_to? :specification_version then
42
42
  s.specification_version = 3
43
43
 
44
44
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_development_dependency(%q<debugger>, [">= 0"])
45
46
  s.add_development_dependency(%q<rspec>, [">= 0"])
46
47
  s.add_development_dependency(%q<bundler>, [">= 0"])
47
48
  s.add_development_dependency(%q<jeweler>, [">= 0"])
48
49
  s.add_development_dependency(%q<fakeweb>, [">= 0"])
49
50
  else
51
+ s.add_dependency(%q<debugger>, [">= 0"])
50
52
  s.add_dependency(%q<rspec>, [">= 0"])
51
53
  s.add_dependency(%q<bundler>, [">= 0"])
52
54
  s.add_dependency(%q<jeweler>, [">= 0"])
53
55
  s.add_dependency(%q<fakeweb>, [">= 0"])
54
56
  end
55
57
  else
58
+ s.add_dependency(%q<debugger>, [">= 0"])
56
59
  s.add_dependency(%q<rspec>, [">= 0"])
57
60
  s.add_dependency(%q<bundler>, [">= 0"])
58
61
  s.add_dependency(%q<jeweler>, [">= 0"])
data/lib/http_logger.rb CHANGED
@@ -7,8 +7,8 @@ require "uri"
7
7
  #
8
8
  # == Setup logger
9
9
  #
10
- # Net::HTTP.logger = Logger.new('/tmp/all.log')
11
- # Net::HTTP.log_headers = true
10
+ # HttpLogger.logger = Logger.new('/tmp/all.log')
11
+ # HttpLogger.log_headers = true
12
12
  #
13
13
  # == Do request
14
14
  #
@@ -16,10 +16,13 @@ require "uri"
16
16
  # http.request(req)
17
17
  # }
18
18
  # ...
19
-
20
- class Net::HTTP
21
-
19
+ #
20
+ # == View the log
21
+ #
22
+ # cat /tmp/all.log
23
+ class HttpLogger
22
24
  class << self
25
+ attr_accessor :collapse_body_limit
23
26
  attr_accessor :log_headers
24
27
  attr_accessor :logger
25
28
  attr_accessor :colorize
@@ -27,17 +30,26 @@ class Net::HTTP
27
30
 
28
31
  self.log_headers = false
29
32
  self.colorize = true
33
+ self.collapse_body_limit = 5000
30
34
 
35
+ def self.perform(*args, &block)
36
+ instance.perform(*args, &block)
37
+ end
31
38
 
32
- alias_method :request_without_logging, :request
39
+ def self.instance
40
+ @instance ||= HttpLogger.new
41
+ end
33
42
 
34
- def request(request, body = nil, &block)
43
+ def self.deprecate_config(option)
44
+ warn "Net::HTTP.#{option} is deprecated. Use HttpLogger.#{option} instead."
45
+ end
46
+
47
+ def perform(http, request, request_body)
35
48
  time = Time.now
36
- response = request_without_logging(request, body, &block)
37
- response
49
+ response = yield
38
50
  ensure
39
- if self.require_logging?(request)
40
- url = "http#{"s" if self.use_ssl?}://#{self.address}:#{self.port}#{request.path}"
51
+ if self.require_logging?(http, request)
52
+ url = "http#{"s" if http.use_ssl?}://#{http.address}:#{http.port}#{request.path}"
41
53
  ofset = Time.now - time
42
54
  log("HTTP #{request.method} (%0.2fms)" % (ofset * 1000), URI.decode(url))
43
55
  request.each_capitalized { |k,v| log("HTTP request header", "#{k}: #{v}") } if self.class.log_headers
@@ -48,24 +60,31 @@ class Net::HTTP
48
60
  log("Response status", "#{response.class} (#{response.code})")
49
61
  response.each_capitalized { |k,v| log("HTTP response header", "#{k}: #{v}") } if self.class.log_headers
50
62
  body = response.body
51
- log("Response body", body) unless body.is_a?(Net::ReadAdapter)
63
+ unless body.is_a?(Net::ReadAdapter)
64
+ if collapse_body_limit && collapse_body_limit > 0 && body.size >= collapse_body_limit
65
+ body = body[0..1000] + "\n\n<some data truncated>\n\n" + body[(body.size - 1000)..body.size]
66
+ end
67
+ log("Response body", body)
68
+ else
69
+ log("response body", "<impossible to log>")
70
+ end
52
71
  end
53
72
  end
54
73
  end
55
74
 
56
- def require_logging?(request)
75
+ protected
76
+ def require_logging?(http, request)
57
77
  fakeweb = if defined?(::FakeWeb)
58
- uri = ::FakeWeb::Utility.request_uri_as_string(self, request)
78
+ uri = ::FakeWeb::Utility.request_uri_as_string(http, request)
59
79
  method = request.method.downcase.to_sym
60
80
  ::FakeWeb.registered_uri?(method, uri)
61
81
  else
62
82
  false
63
83
  end
64
- self.logger && (self.started? || fakeweb)
84
+ self.logger && (http.started? || fakeweb)
65
85
  end
66
86
 
67
87
 
68
- protected
69
88
  def log(message, dump)
70
89
  self.logger.debug(format_log_entry(message, dump))
71
90
  end
@@ -85,20 +104,50 @@ class Net::HTTP
85
104
  self.class.logger
86
105
  end
87
106
 
107
+ def collapse_body_limit
108
+ self.class.collapse_body_limit
109
+ end
88
110
  end
89
111
 
112
+ class Net::HTTP
113
+
114
+ def self.log_headers=(value)
115
+ HttpLogger.deprecate_config("log_headers")
116
+ HttpLogger.log_headers = value
117
+ end
118
+
119
+ def self.colorize=(value)
120
+ HttpLogger.deprecate_config("colorize")
121
+ HttpLogger.colorize = value
122
+ end
123
+
124
+ def self.logger=(value)
125
+ HttpLogger.deprecate_config("logger")
126
+ HttpLogger.logger = value
127
+ end
128
+
129
+
130
+ alias_method :request_without_logging, :request
131
+
132
+ def request(request, body = nil, &block)
133
+ HttpLogger.perform(self, request, body) do
134
+ request_without_logging(request, body, &block)
135
+ end
136
+ end
137
+
138
+ end
90
139
 
91
140
  if defined?(Rails)
92
141
 
93
142
  if !Rails.respond_to?(:application) || (Rails.application && Rails.application.config)
94
143
  # Rails2
95
144
  Rails.configuration.after_initialize do
96
- Net::HTTP.logger = Rails.logger
145
+ HttpLogger.logger = Rails.logger
97
146
  end
98
147
  elsif defined?(ActiveSupport) && ActiveSupport.respond_to?(:on_load)
99
148
  # Rails3
100
149
  ActiveSupport.on_load(:after_initialize) do
101
- Net::HTTP.logger = Rails.logger
150
+ HttpLogger.logger = Rails.logger
102
151
  end
103
152
  end
104
153
  end
@@ -1,8 +1,7 @@
1
1
  require 'spec_helper'
2
- require 'fakeweb'
3
2
  require "uri"
4
3
 
5
- describe "HttpLogger" do
4
+ describe HttpLogger do
6
5
 
7
6
  before do
8
7
  # flush log
@@ -17,6 +16,7 @@ describe "HttpLogger" do
17
16
  end
18
17
 
19
18
  subject do
19
+ _context if defined?(_context)
20
20
  request
21
21
  File.read(LOGFILE)
22
22
  end
@@ -34,14 +34,14 @@ describe "HttpLogger" do
34
34
  context "when headers logging is on" do
35
35
 
36
36
  before(:each) do
37
- Net::HTTP.log_headers = true
37
+ HttpLogger.log_headers = true
38
38
  end
39
39
 
40
40
  it { should include("HTTP response header") }
41
41
  it { should include("HTTP request header") }
42
42
 
43
43
  after(:each) do
44
- Net::HTTP.log_headers = false
44
+ HttpLogger.log_headers = false
45
45
  end
46
46
 
47
47
  end
@@ -65,4 +65,22 @@ describe "HttpLogger" do
65
65
  it {should include("a=hello&b=1")}
66
66
  it {should include("PUT params")}
67
67
  end
68
+
69
+ context "with long response body" do
70
+
71
+ let(:body) do
72
+ "12,Dodo case,dodo@case.com,tech@dodcase.com,single elimination\n" * 50 +
73
+ "12,Bonobos,bono@bos.com,tech@bonobos.com,double elimination\n" * 50
74
+ end
75
+
76
+ let(:url) do
77
+ FakeWeb.register_uri(:get, "http://github.com", :body => body)
78
+ "http://github.com"
79
+ end
80
+
81
+ it { should include("12,Dodo case,dodo@case.com,tech@dodcase.com,single elimination\n")}
82
+ it { should include("<some data truncated>") }
83
+ it { should include("12,Bonobos,bono@bos.com,tech@bonobos.com,double elimination\n")}
84
+
85
+ end
68
86
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  require 'rspec'
4
+ require 'fakeweb'
4
5
  require 'http_logger'
5
6
  require "logger"
6
7
  require "fileutils"
@@ -14,6 +15,6 @@ RSpec.configure do |config|
14
15
 
15
16
 
16
17
  FileUtils.rm_f(LOGFILE)
17
- Net::HTTP.logger = Logger.new(LOGFILE)
18
+ HttpLogger.logger = Logger.new(LOGFILE)
18
19
 
19
20
  end
metadata CHANGED
@@ -1,88 +1,102 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: http_logger
3
- version: !ruby/object:Gem::Version
4
- version: 0.2.5
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Bogdan Gusiev
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2013-01-21 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: rspec
16
- requirement: !ruby/object:Gem::Requirement
17
+
18
+ date: 2013-02-12 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
17
23
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ version_requirements: *id001
32
+ prerelease: false
33
+ name: debugger
22
34
  type: :development
35
+ - !ruby/object:Gem::Dependency
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ version_requirements: *id002
23
46
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
47
+ name: rspec
48
+ type: :development
49
+ - !ruby/object:Gem::Dependency
50
+ requirement: &id003 !ruby/object:Gem::Requirement
25
51
  none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
- - !ruby/object:Gem::Dependency
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ version_requirements: *id003
60
+ prerelease: false
31
61
  name: bundler
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0'
38
62
  type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
63
+ - !ruby/object:Gem::Dependency
64
+ requirement: &id004 !ruby/object:Gem::Requirement
41
65
  none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
46
- - !ruby/object:Gem::Dependency
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ version_requirements: *id004
74
+ prerelease: false
47
75
  name: jeweler
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
76
  type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
77
+ - !ruby/object:Gem::Dependency
78
+ requirement: &id005 !ruby/object:Gem::Requirement
57
79
  none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- - !ruby/object:Gem::Dependency
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ version_requirements: *id005
88
+ prerelease: false
63
89
  name: fakeweb
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
90
  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: '0'
78
- description: This gem keep an eye on every Net::HTTP library usage and dump all request
79
- and response data to the log file
91
+ description: This gem keep an eye on every Net::HTTP library usage and dump all request and response data to the log file
80
92
  email: agresso@gmail.com
81
93
  executables: []
94
+
82
95
  extensions: []
83
- extra_rdoc_files:
96
+
97
+ extra_rdoc_files:
84
98
  - LICENSE.txt
85
- files:
99
+ files:
86
100
  - .document
87
101
  - .rspec
88
102
  - Gemfile
@@ -98,32 +112,39 @@ files:
98
112
  - screenshots/solr.png
99
113
  - spec/http_logger_spec.rb
100
114
  - spec/spec_helper.rb
115
+ has_rdoc: true
101
116
  homepage: http://github.com/bogdan/http_logger
102
- licenses:
117
+ licenses:
103
118
  - MIT
104
119
  post_install_message:
105
120
  rdoc_options: []
106
- require_paths:
121
+
122
+ require_paths:
107
123
  - lib
108
- required_ruby_version: !ruby/object:Gem::Requirement
124
+ required_ruby_version: !ruby/object:Gem::Requirement
109
125
  none: false
110
- requirements:
111
- - - ! '>='
112
- - !ruby/object:Gem::Version
113
- version: '0'
114
- segments:
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ hash: 3
130
+ segments:
115
131
  - 0
116
- hash: 544667989649191470
117
- required_rubygems_version: !ruby/object:Gem::Requirement
132
+ version: "0"
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
134
  none: false
119
- requirements:
120
- - - ! '>='
121
- - !ruby/object:Gem::Version
122
- version: '0'
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ hash: 3
139
+ segments:
140
+ - 0
141
+ version: "0"
123
142
  requirements: []
143
+
124
144
  rubyforge_project:
125
- rubygems_version: 1.8.24
145
+ rubygems_version: 1.6.0
126
146
  signing_key:
127
147
  specification_version: 3
128
148
  summary: Log your http api calls just like SQL queries
129
149
  test_files: []
150
+