faraday-detailed_logger 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 40a4b8eee431c057e71e75cbea811d3637c13939
4
+ data.tar.gz: fb290d8d2af9104d3a94b3fde042678e988076bd
5
+ SHA512:
6
+ metadata.gz: 044fe22fe6c4a0b692c6d342b661acbc80b3ac0481952ec7b194d960bf1a9d2ede75a2324ce53b3c9010e55dd9813445d26d967d9063c583ed565117daea0147
7
+ data.tar.gz: f27298c2fb4966bb1348bc5e4d8a31ab9de30222c5f4939c5a717fe8b831d30243031b0c76a24388ec3372321ef18c2b8e1c4e4b56c50fcebb641343f26a9262
@@ -0,0 +1,25 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .ruby-gemset
6
+ .ruby-version
7
+ .yardoc
8
+ Gemfile.lock
9
+ gemfiles/*.gemfile.lock
10
+ InstalledFiles
11
+ _yardoc
12
+ coverage
13
+ doc/
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
21
+ *.bundle
22
+ *.so
23
+ *.o
24
+ *.a
25
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.1.1
6
+ notifications:
7
+ email: false
@@ -0,0 +1,7 @@
1
+ appraise "faraday-0.8" do
2
+ gem "faraday", "~> 0.8.0"
3
+ end
4
+
5
+ appraise "faraday-current" do
6
+ gem "faraday"
7
+ end
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in faraday-detailed_logger.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Envy Labs, LLC
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.
@@ -0,0 +1,146 @@
1
+ # Faraday::DetailedLogger
2
+
3
+ [![Gem Version](https://img.shields.io/gem/v/faraday-detailed_logger.svg)](http://rubygems.org/gems/faraday-detailed_logger)
4
+ [![Build Status](https://img.shields.io/travis/envylabs/faraday-detailed_logger/master.svg)](https://travis-ci.org/envylabs/faraday-detailed_logger)
5
+ [![Code Climate](https://img.shields.io/codeclimate/github/envylabs/faraday-detailed_logger.svg)](https://codeclimate.com/github/envylabs/faraday-detailed_logger)
6
+ [![Dependency Status](https://gemnasium.com/envylabs/faraday-detailed_logger.svg)](https://gemnasium.com/envylabs/faraday-detailed_logger)
7
+
8
+ A Faraday middleware used for providing debug- and info-level logging
9
+ information. The request and response logs follow very closely with cURL output
10
+ for ease of understanding.
11
+
12
+ **Caution:** Be careful about your log level settings when using this
13
+ middleware, _especially_ in a production environment. With a DEBUG level log
14
+ enabled, there will be information security concerns.
15
+
16
+ At a DEBUG level, the request and response headers and their bodies will be
17
+ logged. This means that if you have Authorization information or API keys in
18
+ your headers or are passing around sensitive information in the bodies, only an
19
+ INFO level or above should be used.
20
+
21
+ No headers or bodies are logged at an INFO or greater log level.
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem "faraday-detailed_logger"
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ ```bash
34
+ $ bundle
35
+ ```
36
+
37
+ Or install it yourself as:
38
+
39
+ ```bash
40
+ $ gem install faraday-detailed_logger
41
+ ```
42
+
43
+ ## Usage
44
+
45
+ Once required, the logger can be added to any Faraday connection by inserting
46
+ it into your connection's request/response stack:
47
+
48
+ ```ruby
49
+ connection = Faraday.new(:url => "http://sushi.com") do |faraday|
50
+ faraday.request :url_encoded
51
+ faraday.response :detailed_logger # <-- Inserts the logger into the connection.
52
+ faraday.adapter Faraday.default_adapter
53
+ end
54
+ ```
55
+
56
+ By default, the Faraday::DetailedLogger will log to STDOUT. If this is not your
57
+ desired log location, simply provide any Logger-compatible object as a
58
+ parameter to the middleware definition:
59
+
60
+ ```ruby
61
+ my_logger = Logger.new("logfile.log")
62
+ my_logger.level = Logger::INFO
63
+
64
+ connection = Faraday.new(:url => "http://sushi.com") do |faraday|
65
+ faraday.request :url_encoded
66
+ faraday.response :detailed_logger, my_logger # <-- sets a custom logger.
67
+ faraday.adapter Faraday.default_adapter
68
+ end
69
+ ```
70
+
71
+ Or, perhaps use your Rails logger:
72
+
73
+ ```ruby
74
+ faraday.response :detailed_logger, Rails.logger
75
+ ```
76
+
77
+ ### Example output
78
+
79
+ Because logs generally work best with a single line of data per entry, the
80
+ DEBUG-level output which contains the headers and bodies is inspected prior to
81
+ logging. This crushes down and slightly manipulates the multi-line output one
82
+ would expect when performing a verbose cURL operation into a log-compatible
83
+ single line.
84
+
85
+ Below is a contrived example showing how this works. Presuming cURL generated
86
+ the following request and received the associated response:
87
+
88
+ ```bash
89
+ $ curl -v -d "requestbody=content" http://sushi.com/temaki
90
+ > GET /temaki HTTP/1.1
91
+ > User-Agent: Faraday::DetailedLogger
92
+ > Host: sushi.com
93
+ > Content-Type: application/x-www-form-urlencoded
94
+ >
95
+ > requestbody=content
96
+ >
97
+ < HTTP/1.1 200 OK
98
+ < Content-Type: application/json
99
+ <
100
+ < {"order_id":"1"}
101
+ ```
102
+
103
+ The Faraday::DetailedLogger would log something similar to the following, with
104
+ DEBUG-level logging enabled:
105
+
106
+ ```plain
107
+ POST http://sushi.com/nigirizushi
108
+ "User-Agent: Faraday::DetailedLogger\nContent-Type: application/x-www-form-urlencoded\n\nrequestbody=content"
109
+ HTTP 200
110
+ "Content-Type: application/json\n\n{\"order_id\":\"1\"}"
111
+ ```
112
+
113
+ #### Request logging
114
+
115
+ Log output for the request-portion of an HTTP interaction:
116
+
117
+ ```plain
118
+ POST http://sushi.com/temaki
119
+ "User-Agent: Faraday v0.9.0\nAccept: application/json\nContent-Type: application/json\n\n{\"name\":\"Polar Bear\",\"ingredients\":[\"Tuna\",\"Salmon\",\"Cream Cheese\",\"Tempura Flakes\"],\"garnish\":\"Eel Sauce\"}"
120
+ ```
121
+
122
+ The POST line is logged at an INFO level just before the request is transmitted
123
+ to the remote system. The second line containing the request headers and body
124
+ are logged at a DEBUG level.
125
+
126
+ #### Response logging
127
+
128
+ Log output for the response-portion of an HTTP interaction:
129
+ Response portion:
130
+
131
+ ```plain
132
+ HTTP 202
133
+ "server: nginx\ndate: Tue, 01 Jul 2014 21:56:52 GMT\ncontent-type: application/json\ncontent-length: 17\nconnection: close\nstatus: 202 Accepted\n\n{\"order_id\":\"1\"}"
134
+ ```
135
+
136
+ The HTTP status line is logged at an INFO level at the same time the response
137
+ is returned from the remote system. The second line containing the response
138
+ headers and body are logged at a DEBUG level.
139
+
140
+ ## Contributing
141
+
142
+ 1. Fork it ( https://github.com/envylabs/faraday-detailed_logger/fork )
143
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
144
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
145
+ 4. Push to the branch (`git push origin my-new-feature`)
146
+ 5. Create a new Pull Request
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'faraday/detailed_logger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "faraday-detailed_logger"
8
+ spec.version = Faraday::DetailedLogger::VERSION
9
+ spec.authors = ["Envy Labs"]
10
+ spec.email = [""]
11
+ spec.summary = %q{A detailed request and response logger for Faraday.}
12
+ spec.description = %q{A Faraday middleware for logging request and response activity including method, URI, headers, and body at varying log levels.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "faraday"
22
+
23
+ spec.add_development_dependency "appraisal", "~> 1.0"
24
+ spec.add_development_dependency "bundler", "~> 1.6"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday", "~> 0.8.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "faraday"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ require "faraday/detailed_logger/version"
2
+ require "faraday/detailed_logger/middleware"
3
+
4
+ module Faraday
5
+ module DetailedLogger
6
+ end
7
+ end
@@ -0,0 +1,75 @@
1
+ require "faraday"
2
+
3
+ module Faraday
4
+ module DetailedLogger
5
+ # A Faraday middleware used for providing debug-level logging information.
6
+ # The request and response logs follow very closely with cURL output for
7
+ # ease of understanding.
8
+ #
9
+ # Be careful about your log level settings when using this middleware,
10
+ # especially in a production environment. With a DEBUG level log enabled,
11
+ # there will be potential information security concerns, because the
12
+ # request and response headers and bodies will be logged out. At an INFO or
13
+ # greater level, this is not a concern.
14
+ #
15
+ class Middleware < Faraday::Response::Middleware
16
+
17
+ def self.default_logger
18
+ require "logger"
19
+ ::Logger.new(STDOUT)
20
+ end
21
+
22
+
23
+ # Public: Initialize a new Logger middleware.
24
+ #
25
+ # app - A Faraday-compatible middleware stack or application.
26
+ # logger - A Logger-compatible object to which the log information will
27
+ # be recorded.
28
+ # progname - A String containing a program name to use when logging.
29
+ #
30
+ # Returns a Logger instance.
31
+ #
32
+ def initialize(app, logger = nil, progname = nil)
33
+ super(app)
34
+ @logger = logger || self.class.default_logger
35
+ @progname = progname
36
+ end
37
+
38
+ # Public: Used by Faraday to execute the middleware during the
39
+ # request/response cycle.
40
+ #
41
+ # env - A Faraday-compatible request environment.
42
+ #
43
+ # Returns the result of the parent application execution.
44
+ #
45
+ def call(env)
46
+ @logger.info(@progname) { "#{env[:method].upcase} #{env[:url]}" }
47
+ @logger.debug(@progname) { curl_output(env[:request_headers], env[:body]).inspect }
48
+ super
49
+ end
50
+
51
+ # Internal: Used by Faraday as a callback hook to process a network
52
+ # response after it has completed.
53
+ #
54
+ # env - A Faraday-compatible response environment.
55
+ #
56
+ # Returns nothing.
57
+ #
58
+ def on_complete(env)
59
+ @logger.info(@progname) { "HTTP #{env[:status]}" }
60
+ @logger.debug(@progname) { curl_output(env[:response_headers], env[:body]).inspect }
61
+ end
62
+
63
+
64
+ private
65
+
66
+
67
+ def curl_output(headers, body)
68
+ string = headers.collect { |k,v| "#{k}: #{v}" }.join("\n")
69
+ string + "\n\n#{body}"
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ Faraday::Response.register_middleware(:detailed_logger => Faraday::DetailedLogger::Middleware)
@@ -0,0 +1,5 @@
1
+ module Faraday
2
+ module DetailedLogger
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,87 @@
1
+ require "spec_helper"
2
+
3
+ require "logger"
4
+ require "stringio"
5
+
6
+ describe Faraday::DetailedLogger::Middleware do
7
+ it "passes through the given progname to the logger" do
8
+ logger = Logger.new(log = StringIO.new)
9
+
10
+ expect {
11
+ connection(logger, "TESTPROGNAME").get("/temaki")
12
+ }.to change {
13
+ log.rewind
14
+ !!(log.read =~/\bTESTPROGNAME\b/)
15
+ }.to(true)
16
+ end
17
+
18
+ it "logs the request method at an INFO level" do
19
+ logger = Logger.new(log = StringIO.new)
20
+
21
+ connection(logger).get("/temaki")
22
+ log.rewind
23
+ expect(log.read).to match(/\bINFO\b.+\bGET\b/)
24
+ end
25
+
26
+ it "logs the request URI at an INFO level" do
27
+ logger = Logger.new(log = StringIO.new)
28
+
29
+ connection(logger).get("/temaki")
30
+ log.rewind
31
+ expect(log.read).to match(/\bINFO\b.+\bhttp:\/\/sushi.com\/temaki\b/)
32
+ end
33
+
34
+ it "logs a cURL-like request package at a DEBUG level" do
35
+ logger = Logger.new(log = StringIO.new)
36
+
37
+ connection(logger).post("/nigirizushi", {"body" => "content"}, {:user_agent => "Faraday::DetailedLogger"})
38
+ log.rewind
39
+ curl = <<-CURL.strip
40
+ User-Agent: Faraday::DetailedLogger
41
+ Content-Type: application/x-www-form-urlencoded
42
+
43
+ body=content
44
+ CURL
45
+ expect(log.read).to match(/\bDEBUG\b.+#{Regexp.escape(curl.inspect)}/)
46
+ end
47
+
48
+ it "logs the response status code at an INFO level" do
49
+ logger = Logger.new(log = StringIO.new)
50
+
51
+ connection(logger).get("/temaki")
52
+ log.rewind
53
+ expect(log.read).to match(/\bINFO\b.+\bHTTP 200\b/)
54
+ end
55
+
56
+ it "logs a cURL-like response package at a DEBUG level" do
57
+ logger = Logger.new(log = StringIO.new)
58
+
59
+ connection(logger).post("/nigirizushi")
60
+ log.rewind
61
+ curl = <<-CURL.strip
62
+ Content-Type: application/json
63
+
64
+ {"order_id":"1"}
65
+ CURL
66
+ expect(log.read).to match(/\bDEBUG\b.+#{Regexp.escape(curl.inspect)}/)
67
+ end
68
+
69
+
70
+ private
71
+
72
+
73
+ def connection(logger = nil, progname = nil)
74
+ Faraday.new(:url => "http://sushi.com") do |builder|
75
+ builder.request(:url_encoded)
76
+ builder.response(:detailed_logger, logger, progname)
77
+ builder.adapter(:test) do |stub|
78
+ stub.get("/temaki") {
79
+ [200, {"Content-Type" => "text/plain"}, "temaki"]
80
+ }
81
+ stub.post("/nigirizushi") {
82
+ [200, {"Content-Type" => "application/json"}, "{\"order_id\":\"1\"}"]
83
+ }
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,25 @@
1
+ require "bundler/setup"
2
+ require "faraday/detailed_logger"
3
+
4
+ RSpec.configure do |config|
5
+ config.filter_run :focus
6
+ config.run_all_when_everything_filtered = true
7
+
8
+ if config.files_to_run.one?
9
+ config.default_formatter = 'doc'
10
+ end
11
+
12
+ config.profile_examples = 10
13
+ config.order = :random
14
+
15
+ Kernel.srand config.seed
16
+
17
+ config.expect_with :rspec do |expectations|
18
+ expectations.syntax = :expect
19
+ end
20
+
21
+ config.mock_with :rspec do |mocks|
22
+ mocks.syntax = :expect
23
+ mocks.verify_partial_doubles = true
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday-detailed_logger
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Envy Labs
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: appraisal
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: A Faraday middleware for logging request and response activity including
84
+ method, URI, headers, and body at varying log levels.
85
+ email:
86
+ - ''
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Appraisals
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - faraday-detailed_logger.gemspec
100
+ - gemfiles/faraday_0.8.gemfile
101
+ - gemfiles/faraday_current.gemfile
102
+ - lib/faraday/detailed_logger.rb
103
+ - lib/faraday/detailed_logger/middleware.rb
104
+ - lib/faraday/detailed_logger/version.rb
105
+ - spec/faraday/detailed_logger/middleware_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: ''
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.3.0
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: A detailed request and response logger for Faraday.
131
+ test_files:
132
+ - spec/faraday/detailed_logger/middleware_spec.rb
133
+ - spec/spec_helper.rb