faraday-detailed_logger 1.1.0 → 2.0.0

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: e2977037d8f6588227b30e82bfd0c25e184c35fd
4
- data.tar.gz: 26ebc6311679aef4db54a2d3eeeebc1a8415e22a
3
+ metadata.gz: 39391a10ebf6f5c1e3afd4a16142b4a7a5cf44de
4
+ data.tar.gz: 7c339da08c5eba109844ec73cc5fe555b7a32f06
5
5
  SHA512:
6
- metadata.gz: 565f8d0ecb8a9b1e055c89b27e9692298f636fc525b4d077a3b7b08ebfde533294dd9a7b1f21e86425810b3168ef91f7b7e10cf921bbb358345e2d57b34fc0c9
7
- data.tar.gz: e2e0d6698cc0681ed37022bb75df70832b0470c7fb4914c09f8101e6caaa2b1f51f730a44ab76af8f204710bf5fc450c1a965b7b1f45f878f478c0220920f6f1
6
+ metadata.gz: 5c4eb19555cb5d2062e6574f06c20c2104351ea4416912012296e323cc2c6c2b6f5a3342c6771cf350b7ddd5a997cc30a00e29a3d66298ff92e9afabb42834c9
7
+ data.tar.gz: 04c930843a10137ddccf41c45530519c25eebbd7e38e04e18f19ff207cb1686b449461a23df23ba01855a822502920f693c43ea925cf7c6d8c3babf35579794c
@@ -10,6 +10,18 @@
10
10
 
11
11
  * No significant changes.
12
12
 
13
+ ## [2.0.0][] / 2016-07-08
14
+
15
+ * Remove Logger `progname` support/configuration. Varying the progname appears
16
+ to make logging in a syslog-like environment unnecessarily more difficult.
17
+ * Add tagging support to the logger. Any number of tags may be given which will
18
+ be prepended to all lines logged. This is largely follows the
19
+ ActiveSupport::TaggedLogging log functionality. "Old" usages of this library
20
+ will treat any previous `progname` strings as a tag and continue to record
21
+ them to the log. Even though the progname is now logged as a tag, this is
22
+ still considered a breaking change just in case system configurations were
23
+ dependent on the progname for log output redirection (syslog).
24
+
13
25
  ## [1.1.0][] / 2016-06-15
14
26
 
15
27
  * Log HTTP 4XX and HTTP 5XX responses at a WARN level.
@@ -20,4 +32,5 @@
20
32
 
21
33
  [1.0.0]: https://github.com/envylabs/faraday-detailed_logger/tree/v1.0.0
22
34
  [1.1.0]: https://github.com/envylabs/faraday-detailed_logger/compare/v1.0.0...v1.1.0
23
- [HEAD]: https://github.com/envylabs/faraday-detailed_logger/compare/v1.1.0...master
35
+ [2.0.0]: https://github.com/envylabs/faraday-detailed_logger/compare/v1.1.0...v2.0.0
36
+ [HEAD]: https://github.com/envylabs/faraday-detailed_logger/compare/v2.0.0...master
data/README.md CHANGED
@@ -75,6 +75,13 @@ Or, perhaps use your Rails logger:
75
75
  faraday.response :detailed_logger, Rails.logger
76
76
  ```
77
77
 
78
+ Further, you might like to tag logged output to make it easily located in your
79
+ logs:
80
+
81
+ ```ruby
82
+ faraday.response :detailed_logger, Rails.logger, "Sushi Request"
83
+ ```
84
+
78
85
  ### Example output
79
86
 
80
87
  Because logs generally work best with a single line of data per entry, the
@@ -1,4 +1,5 @@
1
1
  require "faraday"
2
+ require "faraday/detailed_logger/tagged_logging"
2
3
 
3
4
  module Faraday
4
5
  module DetailedLogger
@@ -25,14 +26,14 @@ module Faraday
25
26
  # app - A Faraday-compatible middleware stack or application.
26
27
  # logger - A Logger-compatible object to which the log information will
27
28
  # be recorded.
28
- # progname - A String containing a program name to use when logging.
29
+ # tags - An optional array of tags to apply to the log output.
29
30
  #
30
31
  # Returns a Logger instance.
31
32
  #
32
- def initialize(app, logger = nil, progname = nil)
33
+ def initialize(app, logger = nil, *tags)
33
34
  super(app)
34
- @logger = logger || self.class.default_logger
35
- @progname = progname
35
+ @logger = TaggedLogging.new(logger || self.class.default_logger)
36
+ @tags = tags
36
37
  end
37
38
 
38
39
  # Public: Used by Faraday to execute the middleware during the
@@ -43,8 +44,10 @@ module Faraday
43
44
  # Returns the result of the parent application execution.
44
45
  #
45
46
  def call(env)
46
- @logger.info(@progname) { "#{env[:method].upcase} #{env[:url]}" }
47
- @logger.debug(@progname) { curl_output(env[:request_headers], env[:body]).inspect }
47
+ logger.tagged(*tags) do
48
+ logger.info { "#{env[:method].upcase} #{env[:url]}" }
49
+ logger.debug { curl_output(env[:request_headers], env[:body]).inspect }
50
+ end
48
51
  super
49
52
  end
50
53
 
@@ -57,25 +60,31 @@ module Faraday
57
60
  #
58
61
  def on_complete(env)
59
62
  status = env[:status]
60
- log_response_status(@progname, status) { "HTTP #{status}" }
61
- @logger.debug(@progname) { curl_output(env[:response_headers], env[:body]).inspect }
63
+
64
+ logger.tagged(*tags) do
65
+ log_response_status(status) { "HTTP #{status}" }
66
+ logger.debug { curl_output(env[:response_headers], env[:body]).inspect }
67
+ end
62
68
  end
63
69
 
64
70
 
65
71
  private
66
72
 
67
73
 
74
+ attr_reader :logger
75
+ attr_reader :tags
76
+
68
77
  def curl_output(headers, body)
69
78
  string = headers.collect { |k,v| "#{k}: #{v}" }.join("\n")
70
79
  string + "\n\n#{body}"
71
80
  end
72
81
 
73
- def log_response_status(progname, status, &block)
82
+ def log_response_status(status, &block)
74
83
  case status
75
84
  when 200..399
76
- @logger.info(progname, &block)
85
+ logger.info(&block)
77
86
  else
78
- @logger.warn(progname, &block)
87
+ logger.warn(&block)
79
88
  end
80
89
  end
81
90
  end
@@ -0,0 +1,75 @@
1
+ require 'logger'
2
+
3
+ module Faraday
4
+ module DetailedLogger
5
+ # This was largely lifted from ActiveSupport::TaggedLogging. Modifications
6
+ # made to remove ActiveSupport dependencies (blank?, delegation, and
7
+ # ActiveSupport::Logger).
8
+ #
9
+ module TaggedLogging
10
+ extend Forwardable
11
+
12
+ module Formatter
13
+ BLANK = lambda { |value| value.respond_to?(:empty?) ? !!value.empty? : !value }
14
+
15
+ def call(severity, timestamp, progname, msg)
16
+ super(severity, timestamp, progname, "#{tags_text}#{msg}")
17
+ end
18
+
19
+ def tagged(*tags)
20
+ new_tags = push_tags(*tags)
21
+ yield self
22
+ ensure
23
+ pop_tags(new_tags.size)
24
+ end
25
+
26
+ def push_tags(*tags)
27
+ tags.flatten.reject(&BLANK).tap do |new_tags|
28
+ current_tags.concat new_tags
29
+ end
30
+ end
31
+
32
+ def pop_tags(size = 1)
33
+ current_tags.pop size
34
+ end
35
+
36
+ def clear_tags!
37
+ current_tags.clear
38
+ end
39
+
40
+ def current_tags
41
+ @thread_key ||= "faraday_detailed_logger_tagged_logging_tags:#{object_id}".freeze
42
+ Thread.current[@thread_key] ||= []
43
+ end
44
+
45
+ private
46
+ def tags_text
47
+ tags = current_tags
48
+ if tags.any?
49
+ tags.collect { |tag| "[#{tag}] " }.join
50
+ end
51
+ end
52
+ end
53
+
54
+
55
+ def self.new(logger)
56
+ unless logger.respond_to?(:tagged)
57
+ logger.formatter ||= ::Logger::Formatter.new
58
+ logger.formatter.extend Formatter
59
+ logger.extend(self)
60
+ end
61
+
62
+ logger
63
+ end
64
+
65
+ def tagged(*tags)
66
+ formatter.tagged(*tags) { yield self }
67
+ end
68
+
69
+ def flush
70
+ clear_tags!
71
+ super if defined?(super)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -1,5 +1,5 @@
1
1
  module Faraday
2
2
  module DetailedLogger
3
- VERSION = "1.1.0"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-detailed_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Envy Labs
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-16 00:00:00.000000000 Z
11
+ date: 2016-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -119,6 +119,7 @@ files:
119
119
  - gemfiles/faraday_release.gemfile
120
120
  - lib/faraday/detailed_logger.rb
121
121
  - lib/faraday/detailed_logger/middleware.rb
122
+ - lib/faraday/detailed_logger/tagged_logging.rb
122
123
  - lib/faraday/detailed_logger/version.rb
123
124
  homepage: https://github.com/envylabs/faraday-detailed_logger
124
125
  licenses: