httplog 0.3.3 → 0.99.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4458a32430545d16e4403483a77ef00b59d30c19
4
- data.tar.gz: 5d334c5157c1da40ee45a1bd4c1142f1cd9c7636
3
+ metadata.gz: 04f8b802147c2e3feef23e52002cd36dfbd24b7f
4
+ data.tar.gz: 4826d8585db9817054996dd0b4f9f6bdcebd6a7a
5
5
  SHA512:
6
- metadata.gz: a3c3a854366b3d89ba534bfef525d488e7899cfd4935a11458aee52d06bd8a4bcc20d6b773795ba6432207aad2083baa96501c51cd9a07bd899f22139a7e469d
7
- data.tar.gz: f09ac99eb6b33c119b55c3a496dc3c6c49a1431dc80bf2cac01f6e6a77f19fc46544de7ccf9b0892572a7cd18cb1e7ac046aeb90817fc62b3925b2d428e250f6
6
+ metadata.gz: 2948b2216365799da61ece7891814da3fcb5db703704b24878dbf8fc23cad1ee9c877e773a80e353f971a8bf311aa5cddbb345631ae5bee761882a55c2fa1503
7
+ data.tar.gz: 202b4b85870bc9a497951b1c5302571fef172daeab7c4927639e2b4c627ba1841b875167601f59dee7faf0a725def65de34ded2c362cc0aa0f47ebde8d33b64d
data/README.md CHANGED
@@ -2,26 +2,29 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/httplog.png)](http://badge.fury.io/rb/httplog) [![Build Status](https://travis-ci.org/trusche/httplog.svg?branch=master)](https://travis-ci.org/trusche/httplog) [![Code Climate](https://codeclimate.com/github/trusche/httplog.png)](https://codeclimate.com/github/trusche/httplog)
4
4
 
5
+ **+++ This is the README for version 0.99.0 and higher. If you're on previous versions, please refer to [this README version](https://github.com/trusche/httplog/tree/v0.3.3), since the configuration syntax has changed.+++**
6
+
5
7
  Log outgoing HTTP requests made from your application. Helps with debugging pesky API error responses, or just generally understanding what's going on under the hood.
6
8
 
7
- So far this gem works with the following ruby modules and libraries:
9
+ **+++Requires ruby 2.2 or higher. If you're stuck with an older version of ruby for some reason, you're stuck with httplog v0.3.3.+++**
10
+
11
+ This gem works with the following ruby modules and libraries:
8
12
 
9
13
  * [Net::HTTP](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/index.html)
10
- * [Ethon](https://github.com/typhoeus/ethon) (**Needs work to support latest versions**)
11
- * [Excon](https://github.com/geemus/excon) (for excon >= v18.0, httplog >= 0.2.4 is required)
14
+ * [Ethon](https://github.com/typhoeus/ethon)
15
+ * [Excon](https://github.com/geemus/excon)
12
16
  * [OpenURI](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/index.html)
13
17
  * [Patron](https://github.com/toland/patron)
14
18
  * [HTTPClient](https://github.com/nahi/httpclient)
15
19
  * [HTTParty](https://github.com/jnunemaker/httparty)
16
20
  * [HTTP](https://github.com/httprb/http)
17
21
 
18
- These libraries are at least partially supported, where they use one of the above as adapters:
22
+ These libraries are at least partially supported, where they use one of the above as adapters, but not explicitly tested - YMMV:
19
23
 
20
24
  * [Faraday](https://github.com/technoweenie/faraday)
21
- * [Typhoeus](https://github.com/typhoeus/typhoeus) (**Needs work to support latest versions**)
25
+ * [Typhoeus](https://github.com/typhoeus/typhoeus)
22
26
 
23
- In theory, it should also work with any library built on top of these. But since
24
- the difference between theory and practice is bigger in practice than in theory, YMMV.
27
+ In theory, it should also work with any library built on top of these. But the difference between theory and practice is bigger in practice than in theory.
25
28
 
26
29
  This is very much a development and debugging tool; it is **not recommended** to
27
30
  use this in a production environment as it is moneky-patching the respective HTTP implementations.
@@ -46,33 +49,49 @@ By default, this will log all outgoing HTTP requests and their responses to $std
46
49
 
47
50
  You can override the following default options:
48
51
 
49
- HttpLog.options[:logger] = Logger.new($stdout)
50
- HttpLog.options[:severity] = Logger::Severity::DEBUG
51
- HttpLog.options[:log_connect] = true
52
- HttpLog.options[:log_request] = true
53
- HttpLog.options[:log_headers] = false
54
- HttpLog.options[:log_data] = true
55
- HttpLog.options[:log_status] = true
56
- HttpLog.options[:log_response] = true
57
- HttpLog.options[:log_benchmark] = true
58
- HttpLog.options[:compact_log] = false # setting this to true will make
59
- all "log_*" options redundant
60
- HttpLog.options[:color] = false # (see below)
61
- # only log requests made to specified hosts (URLs)
62
- HttpLog.options[:url_whitelist_pattern] = /.*/
63
- # overrides whitelist
64
- HttpLog.options[:url_blacklist_pattern] = nil
65
-
66
- So if you want to use this in a Rails app:
67
-
68
- # config/initializers/httplog.rb
69
- HttpLog.options[:logger] = Rails.logger
52
+ HttpLog.configure do |config|
53
+
54
+ # You can assign a different logger
55
+ config.logger = Logger.new($stdout)
56
+
57
+ # I really wouldn't change this...
58
+ config.severity = Logger::Severity::DEBUG
59
+
60
+ # Tweak which parts of the HTTP cycle to log...
61
+ config.log_connect = true
62
+ config.log_request = true
63
+ config.log_headers = false
64
+ config.log_data = true
65
+ config.log_status = true
66
+ config.log_response = true
67
+ config.log_benchmark = true
68
+
69
+ # ...or log all request as a single line by setting this to `true`
70
+ config.compact_log = false
71
+
72
+ # Prettify the output - see below
73
+ config.color = false
74
+
75
+ # Limit logging based on URL patterns
76
+ config.url_whitelist_pattern = /.*/
77
+ config.url_blacklist_pattern = nil
78
+ end
79
+
80
+ If you want to use this in a Rails app, I'd suggest configuring this specifically for each environment. A global initializer is not a good idea since `HttpLog` will be undefined in production. Because you're **not using this in production**, right? :)
81
+
82
+ # config/environments/development.rb
83
+
84
+ HttpLog.configure do |config|
85
+ config.logger = Rails.logger
86
+ end
70
87
 
71
88
  You can colorize the output to make it stand out in your logfile:
72
89
 
73
- HttpLog.options[:color] = {color: :black, background: :light_red}
90
+ HttpLog.configure do |config|
91
+ config.color = {color: :black, background: :light_red}
92
+ end
74
93
 
75
- For more color options see [colorize documentation](https://github.com/fazibear/colorize/blob/master/README.md)
94
+ For more color options [please refer to the colorize documentation](https://github.com/fazibear/colorize/blob/master/README.md)
76
95
 
77
96
  ### Compact logging
78
97
 
data/lib/httplog.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "httplog/version"
2
+ require "httplog/configuration"
2
3
  require "httplog/http_log"
3
4
  require "httplog/adapters/net_http"
4
5
  require "httplog/adapters/httpclient"
@@ -0,0 +1,50 @@
1
+ module HttpLog
2
+ class Configuration
3
+ attr_accessor :compact_log,
4
+ :logger,
5
+ :severity,
6
+ :prefix,
7
+ :log_connect,
8
+ :log_request,
9
+ :log_headers,
10
+ :log_data,
11
+ :log_status,
12
+ :log_response,
13
+ :log_benchmark,
14
+ :compact_log,
15
+ :url_whitelist_pattern,
16
+ :url_blacklist_pattern,
17
+ :color,
18
+ :prefix_data_lines,
19
+ :prefix_response_lines,
20
+ :prefix_line_numbers
21
+
22
+ def initialize
23
+ @compact_log = false
24
+ @logger = Logger.new($stdout)
25
+ @severity = Logger::Severity::DEBUG
26
+ @prefix = LOG_PREFIX
27
+ @log_connect = true
28
+ @log_request = true
29
+ @log_headers = false
30
+ @log_data = true
31
+ @log_status = true
32
+ @log_response = true
33
+ @log_benchmark = true
34
+ @compact_log = false
35
+ @url_whitelist_pattern = /.*/
36
+ @url_blacklist_pattern = nil
37
+ @color = false
38
+ @prefix_data_lines = false
39
+ @prefix_response_lines = false
40
+ @prefix_line_numbers = false
41
+ end
42
+
43
+ # TODO: remove in 1.0.0
44
+ def []=(key, value)
45
+ $stderr.puts "DEPRECATION WARNING: Assignment to HttpLog.options will be removed in version 1.0.0. Please use HttpLog.configure block instead as described here: https://github.com/trusche/httplog#configuration"
46
+ self.send("#{key.to_s}=", value)
47
+ end
48
+
49
+ end
50
+ end
@@ -6,83 +6,63 @@ require "colorize"
6
6
  module HttpLog
7
7
  LOG_PREFIX = "[httplog] ".freeze
8
8
 
9
- DEFAULT_LOGGER = Logger.new($stdout)
10
- DEFAULT_OPTIONS = {
11
- :logger => DEFAULT_LOGGER,
12
- :severity => Logger::Severity::DEBUG,
13
- :prefix => LOG_PREFIX,
14
- :log_connect => true,
15
- :log_request => true,
16
- :log_headers => false,
17
- :log_data => true,
18
- :log_status => true,
19
- :log_response => true,
20
- :log_benchmark => true,
21
- :compact_log => false,
22
- :url_whitelist_pattern => /.*/,
23
- :url_blacklist_pattern => nil,
24
- :color => false,
25
- :prefix_data_lines => false,
26
- :prefix_response_lines => false,
27
- :prefix_line_numbers => false
28
- }
9
+ class << self
29
10
 
11
+ attr_accessor :configuration
30
12
 
31
- class << self
32
- def options
33
- @@options ||= DEFAULT_OPTIONS.clone
13
+ def configuration
14
+ @configuration ||= Configuration.new
34
15
  end
16
+ alias_method :config, :configuration
17
+ alias_method :options, :configuration # TODO: remove with 1.0.0
35
18
 
36
- def reset_options!
37
- @@options = DEFAULT_OPTIONS.clone
19
+ def reset!
20
+ @configuration = nil
38
21
  end
39
22
 
23
+ def configure
24
+ yield(configuration)
25
+ end
26
+
40
27
  def url_approved?(url)
41
- unless options[:url_blacklist_pattern].nil?
42
- return false if url.to_s.match(options[:url_blacklist_pattern])
43
- end
44
-
45
- url.to_s.match(options[:url_whitelist_pattern])
28
+ return false if config.url_blacklist_pattern && url.to_s.match(config.url_blacklist_pattern)
29
+ url.to_s.match(config.url_whitelist_pattern)
46
30
  end
47
31
 
48
32
  def log(msg)
49
- # This builds a hash {0=>:DEBUG, 1=>:INFO, 2=>:WARN, 3=>:ERROR, 4=>:FATAL, 5=>:UNKNOWN}.
50
- # Courtesy of the delayed_job gem in this commit:
51
- # https://github.com/collectiveidea/delayed_job/commit/e7f5aa1ed806e61251bdb77daf25864eeb3aff59
52
- severities = Hash[*Logger::Severity.constants.enum_for(:each_with_index).collect{ |s, i| [i, s] }.flatten]
53
- severity = severities[options[:severity]].to_s.downcase
54
- options[:logger].send(severity, colorize(prefix + msg))
33
+ config.logger.log(config.severity, colorize(prefix + msg))
55
34
  end
56
35
 
57
36
  def log_connection(host, port = nil)
58
- return if options[:compact_log] || !options[:log_connect]
37
+ return if config.compact_log || !config.log_connect
59
38
  log("Connecting: #{[host, port].compact.join(":")}")
60
39
  end
61
40
 
62
41
  def log_request(method, uri)
63
- return if options[:compact_log] || !options[:log_request]
42
+ return if config.compact_log || !config.log_request
64
43
  log("Sending: #{method.to_s.upcase} #{uri}")
65
44
  end
66
45
 
67
46
  def log_headers(headers = {})
68
- return if options[:compact_log] || !options[:log_headers]
47
+ return if config.compact_log || !config.log_headers
69
48
  headers.each do |key,value|
70
49
  log("Header: #{key}: #{value}")
71
50
  end
72
51
  end
73
52
 
74
53
  def log_status(status)
75
- return if options[:compact_log] || !options[:log_status]
54
+ return if config.compact_log || !config.log_status
55
+ status = Rack::Utils.status_code(status) unless status == /\d{3}/
76
56
  log("Status: #{status}")
77
57
  end
78
58
 
79
59
  def log_benchmark(seconds)
80
- return if options[:compact_log] || !options[:log_benchmark]
81
- log("Benchmark: #{seconds} seconds")
60
+ return if config.compact_log || !config.log_benchmark
61
+ log("Benchmark: #{seconds.to_f.round(6)} seconds")
82
62
  end
83
63
 
84
64
  def log_body(body, encoding = nil, content_type=nil)
85
- return if options[:compact_log] || !options[:log_response]
65
+ return if config.compact_log || !config.log_response
86
66
 
87
67
  unless text_based?(content_type)
88
68
  log("Response: (not showing binary data)")
@@ -104,7 +84,7 @@ module HttpLog
104
84
 
105
85
  data = utf_encoded(body.to_s, content_type)
106
86
 
107
- if options[:prefix_response_lines]
87
+ if config.prefix_response_lines
108
88
  log("Response:")
109
89
  log_data_lines(data)
110
90
  else
@@ -114,10 +94,10 @@ module HttpLog
114
94
  end
115
95
 
116
96
  def log_data(data)
117
- return if options[:compact_log] || !options[:log_data]
97
+ return if config.compact_log || !config.log_data
118
98
  data = utf_encoded(data.to_s)
119
99
 
120
- if options[:prefix_data_lines]
100
+ if config.prefix_data_lines
121
101
  log("Data:")
122
102
  log_data_lines(data)
123
103
  else
@@ -126,14 +106,14 @@ module HttpLog
126
106
  end
127
107
 
128
108
  def log_compact(method, uri, status, seconds)
129
- return unless options[:compact_log]
109
+ return unless config.compact_log
130
110
  status = Rack::Utils.status_code(status) unless status == /\d{3}/
131
111
  log("#{method.to_s.upcase} #{uri} completed with status code #{status} in #{seconds} seconds")
132
112
  end
133
113
 
134
114
  def colorize(msg)
135
- return msg unless options[:color]
136
- msg.send(:colorize, options[:color])
115
+ return msg unless config.color
116
+ msg.send(:colorize, config.color)
137
117
  end
138
118
 
139
119
  private
@@ -154,7 +134,7 @@ module HttpLog
154
134
 
155
135
  def log_data_lines(data)
156
136
  data.each_line.with_index do |line, row|
157
- if options[:prefix_line_numbers]
137
+ if config.prefix_line_numbers
158
138
  log("#{row + 1}: #{line.chomp}")
159
139
  else
160
140
  log(line.strip)
@@ -163,10 +143,10 @@ module HttpLog
163
143
  end
164
144
 
165
145
  def prefix
166
- if options[:prefix].respond_to?(:call)
167
- options[:prefix].call
146
+ if config.prefix.respond_to?(:call)
147
+ config.prefix.call
168
148
  else
169
- options[:prefix].to_s
149
+ config.prefix.to_s
170
150
  end
171
151
  end
172
152
 
@@ -1,4 +1,4 @@
1
1
  module HttpLog
2
- VERSION = "0.3.3"
2
+ VERSION = "0.99.1"
3
3
  end
4
4
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httplog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.99.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thilo Rusche
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-28 00:00:00.000000000 Z
11
+ date: 2017-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -123,7 +123,7 @@ dependencies:
123
123
  - !ruby/object:Gem::Version
124
124
  version: 0.18.0
125
125
  - !ruby/object:Gem::Dependency
126
- name: typhoeus
126
+ name: ethon
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
@@ -137,7 +137,7 @@ dependencies:
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  - !ruby/object:Gem::Dependency
140
- name: ethon
140
+ name: patron
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - ">="
@@ -151,7 +151,7 @@ dependencies:
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
153
  - !ruby/object:Gem::Dependency
154
- name: patron
154
+ name: http
155
155
  requirement: !ruby/object:Gem::Requirement
156
156
  requirements:
157
157
  - - ">="
@@ -164,20 +164,6 @@ dependencies:
164
164
  - - ">="
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
- - !ruby/object:Gem::Dependency
168
- name: http
169
- requirement: !ruby/object:Gem::Requirement
170
- requirements:
171
- - - '='
172
- - !ruby/object:Gem::Version
173
- version: 1.0.4
174
- type: :development
175
- prerelease: false
176
- version_requirements: !ruby/object:Gem::Requirement
177
- requirements:
178
- - - '='
179
- - !ruby/object:Gem::Version
180
- version: 1.0.4
181
167
  - !ruby/object:Gem::Dependency
182
168
  name: simplecov
183
169
  requirement: !ruby/object:Gem::Requirement
@@ -193,7 +179,7 @@ dependencies:
193
179
  - !ruby/object:Gem::Version
194
180
  version: '0'
195
181
  - !ruby/object:Gem::Dependency
196
- name: log4r
182
+ name: rake
197
183
  requirement: !ruby/object:Gem::Requirement
198
184
  requirements:
199
185
  - - ">="
@@ -207,19 +193,19 @@ dependencies:
207
193
  - !ruby/object:Gem::Version
208
194
  version: '0'
209
195
  - !ruby/object:Gem::Dependency
210
- name: rake
196
+ name: listen
211
197
  requirement: !ruby/object:Gem::Requirement
212
198
  requirements:
213
- - - ">="
199
+ - - "~>"
214
200
  - !ruby/object:Gem::Version
215
- version: '0'
201
+ version: 3.0.8
216
202
  type: :development
217
203
  prerelease: false
218
204
  version_requirements: !ruby/object:Gem::Requirement
219
205
  requirements:
220
- - - ">="
206
+ - - "~>"
221
207
  - !ruby/object:Gem::Version
222
- version: '0'
208
+ version: 3.0.8
223
209
  - !ruby/object:Gem::Dependency
224
210
  name: colorize
225
211
  requirement: !ruby/object:Gem::Requirement
@@ -252,6 +238,7 @@ files:
252
238
  - lib/httplog/adapters/httpclient.rb
253
239
  - lib/httplog/adapters/net_http.rb
254
240
  - lib/httplog/adapters/patron.rb
241
+ - lib/httplog/configuration.rb
255
242
  - lib/httplog/http_log.rb
256
243
  - lib/httplog/version.rb
257
244
  homepage: http://github.com/trusche/httplog
@@ -273,7 +260,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
273
260
  version: '0'
274
261
  requirements: []
275
262
  rubyforge_project:
276
- rubygems_version: 2.5.1
263
+ rubygems_version: 2.6.8
277
264
  signing_key:
278
265
  specification_version: 4
279
266
  summary: Logs outgoing HTTP requests.