log-me 0.0.5 → 0.0.6

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: c9341d345fdaea0e92971d20b2ecb4d339d3040f
4
- data.tar.gz: 38aab7a27f126a223968cfde1a6153a21e88f01f
3
+ metadata.gz: cb5839bd443aebe30a0038d2722d887d4db1a499
4
+ data.tar.gz: 2814475e21a754d7934eba90fa49a9cb27b47ade
5
5
  SHA512:
6
- metadata.gz: 44c0f529ec28f735bc7c63aa3293facf587a360f57770f52d3e173fac13eca39cba0df29c0bb8305ccbdd871ec38b3194d1fcd8f0774eb2df083ea06029820ee
7
- data.tar.gz: d66794d8cd55d822b9efb9b82e86dc7e8aafc6023c8c2763505f7204659eed943eed55534282f890d69bcef42e6060ec0b712f95235acd2b52eccdcb30cc0db3
6
+ metadata.gz: 9307159a7b9e4c9b39aa2edcf7e000ba66880d5f662577cd166d1f8d7c0ed147de7b760b3e9eda2ea218a36256d3632d1eccebddaf41d276e21ebe23b0641b96
7
+ data.tar.gz: fee25615f75c5c540fa26c792e3a268adc1b598e356ab327f66a17611aafdf4f94251716bd2be5902c6a563f911a96b8037c4c56022783abfdd90b3a3753bb5a
@@ -1,3 +1,6 @@
1
+ == Version 0.0.6
2
+ - New: Log formatted request and response messages from Net::HTTP.
3
+
1
4
  == Version 0.0.5
2
5
  - Warning: Minimal required Ruby version from now is 1.9.3.
3
6
  - New: Label to log messages.
@@ -2,7 +2,7 @@
2
2
 
3
3
  A simple way to configure log in your gem.
4
4
 
5
- LogMe is especially useful when you need to log Web Service calls or HTTP requests and responses.
5
+ LogMe is especially useful when you need to log Web Service calls or HTTP requests and responses (using Net::HTTP).
6
6
 
7
7
  == Instalation
8
8
 
@@ -24,7 +24,7 @@ In your gem:
24
24
  module CoolGem
25
25
  class SomeClass
26
26
  def do_something
27
- # do something and log
27
+ # Do something and log
28
28
  CoolGem.log "I am logging something here."
29
29
  end
30
30
  end
@@ -37,6 +37,34 @@ Using your gem:
37
37
  By default will be logged in STDOUT using log level :info:
38
38
  I, [2011-08-24T01:22:52.677395 #3026] INFO -- : [CoolGem] I am logging something here.
39
39
 
40
+ Logging Net::HTTP requests and responses:
41
+ module CoolGem
42
+ class WebService
43
+ def do_something
44
+ url = "http://prodis.blog.br"
45
+
46
+ # Some logic to create a Net::HTTP request class.
47
+ request = create_request
48
+ CoolGem.log_request request, url
49
+
50
+ # Some logic to obtain a Net::HTTP response.
51
+ response = do_request(request, url)
52
+ CoolGem.log_response response
53
+ end
54
+ end
55
+ end
56
+
57
+ ws = CoolGem::WebService.new
58
+ ws.do_something
59
+
60
+ In the log:
61
+ [CoolGem] Request:
62
+ POST http://prodis.blog.br/some_resource
63
+ param1=value1&param2=value2
64
+
65
+ [CoolGem] Response:
66
+ HTTP/1.1 201 Created
67
+
40
68
  Your gem consumer can configure the logger:
41
69
  CoolGem.configure do |config|
42
70
  config.log_enabled = false # Disable log
@@ -45,6 +73,7 @@ Your gem consumer can configure the logger:
45
73
  config.logger = Rails.logger # Use the Rails logger
46
74
  end
47
75
 
76
+
48
77
  == Author
49
78
  - {Fernando Hamasaki de Amorim (prodis)}[http://prodis.blog.br]
50
79
 
@@ -1,5 +1,6 @@
1
1
  require 'logger'
2
2
  require 'logme/configuration'
3
+ require 'logme/net_http_formatter'
3
4
 
4
5
  module LogMe
5
6
  attr_writer :log_enabled
@@ -27,7 +28,21 @@ module LogMe
27
28
  logger.send log_level, "[#{log_label}] #{message}\n" if log_enabled?
28
29
  end
29
30
 
31
+ def log_request(request, url)
32
+ log formatter.format_request(request, url)
33
+ end
34
+
35
+ def log_response(response)
36
+ log formatter.format_response(response)
37
+ end
38
+
30
39
  def self.extended(base)
31
40
  base.send :extend, LogMe::Configuration
32
41
  end
42
+
43
+ private
44
+
45
+ def formatter
46
+ @formatter ||= LogMe::NetHttpFormatter.new
47
+ end
33
48
  end
@@ -0,0 +1,28 @@
1
+ module LogMe
2
+ class NetHttpFormatter
3
+ def format_request(request, url)
4
+ message = format_message(request) do
5
+ message = with_line_break { "Request:" }
6
+ message << with_line_break { "#{request.method} #{url}" }
7
+ end
8
+ end
9
+
10
+ def format_response(response)
11
+ message = format_message(response) do
12
+ message = with_line_break { "Response:" }
13
+ message << with_line_break { "HTTP/#{response.http_version} #{response.code} #{response.message}" }
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def format_message(http)
20
+ message = yield
21
+ message << with_line_break { http.body }
22
+ end
23
+
24
+ def with_line_break
25
+ "#{yield}\n"
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module LogMe
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -129,4 +129,35 @@ describe LogMe do
129
129
  end
130
130
  end
131
131
  end
132
+
133
+ describe "#log_request" do
134
+ let(:log_stream) { StringIO.new }
135
+ let(:request) { double "Request" }
136
+ let(:url) { "http://prodis.blog.br" }
137
+
138
+ before do
139
+ subject.configure { |config| config.logger = ::Logger.new(log_stream) }
140
+ end
141
+
142
+ it "logs formatted request message" do
143
+ LogMe::NetHttpFormatter.any_instance.stub(:format_request).with(request, url).and_return("Request message.")
144
+ subject.log_request(request, url)
145
+ expect(log_stream.string).to include "[#{subject.name}] Request message.\n"
146
+ end
147
+ end
148
+
149
+ describe "#log_response" do
150
+ let(:log_stream) { StringIO.new }
151
+ let(:response) { double "Response" }
152
+
153
+ before do
154
+ subject.configure { |config| config.logger = ::Logger.new(log_stream) }
155
+ end
156
+
157
+ it "logs formatted response message" do
158
+ LogMe::NetHttpFormatter.any_instance.stub(:format_response).with(response).and_return("Response message.")
159
+ subject.log_response response
160
+ expect(log_stream.string).to include "[#{subject.name}] Response message.\n"
161
+ end
162
+ end
132
163
  end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe LogMe::NetHttpFormatter do
4
+ describe "#format_request" do
5
+ let(:request) do
6
+ double "Net::HTTP::Post", method: "POST", body: "param1=value1&param2=value2"
7
+ end
8
+ let(:url) { "http://prodis.blog.br" }
9
+
10
+ it "formats request message" do
11
+ expected_message = "Request:\nPOST #{url}\nparam1=value1&param2=value2\n"
12
+ expect(subject.format_request(request, url)).to eq expected_message
13
+ end
14
+ end
15
+
16
+ describe "#format_response" do
17
+ let(:response) do
18
+ double "Net::HTTP::OK", http_version: "1.1", code: "200", message: "OK", body: "<xml><fake />"
19
+ end
20
+
21
+ it "formats response message" do
22
+ expected_message = "Response:\nHTTP/1.1 200 OK\n<xml><fake />\n"
23
+ expect(subject.format_response(response)).to eq expected_message
24
+ end
25
+ end
26
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log-me
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Prodis a.k.a. Fernando Hamasaki de Amorim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-19 00:00:00.000000000 Z
11
+ date: 2014-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -69,9 +69,11 @@ files:
69
69
  - Rakefile
70
70
  - lib/log-me.rb
71
71
  - lib/logme/configuration.rb
72
+ - lib/logme/net_http_formatter.rb
72
73
  - lib/logme/version.rb
73
74
  - log-me.gemspec
74
75
  - spec/log-me_spec.rb
76
+ - spec/logme/net_http_formatter_spec.rb
75
77
  - spec/spec_helper.rb
76
78
  homepage: http://github.com/prodis/log-me
77
79
  licenses:
@@ -99,4 +101,5 @@ specification_version: 4
99
101
  summary: A simple way to configure log in your gem.
100
102
  test_files:
101
103
  - spec/log-me_spec.rb
104
+ - spec/logme/net_http_formatter_spec.rb
102
105
  - spec/spec_helper.rb