Skip to content

Commit 3f5e8df

Browse files
authored
faraday: Add logging formatter signatures (#1037)
1 parent 81fa8bd commit 3f5e8df

6 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class MyFormatter < Faraday::Logging::Formatter
2+
def request(env)
3+
# Build a custom message using `env`
4+
info('Request') { 'Sending Request' }
5+
end
6+
7+
def response(env)
8+
# Build a custom message using `env`
9+
info('Response') { 'Response Received' }
10+
end
11+
12+
def exception(exc)
13+
# Build a custom message using `exc`
14+
info('Error') { 'Error Raised' }
15+
end
16+
end
17+
18+
conn = Faraday.new(url: 'http:///example.com') do |faraday|
19+
faraday.response :logger, nil, formatter: MyFormatter
20+
end
21+
22+
conn.get('/abc')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class MyFormatter < Faraday::Logging::Formatter
2+
def request: (Faraday::Env env) -> void
3+
def response: (Faraday::Env env) -> void
4+
def exception: (untyped exc) -> void
5+
end

gems/faraday/2.5/faraday.rbs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ module Faraday
225225
end
226226

227227
class Response
228+
class Logger < Middleware
229+
def initialize: (untyped app, ?untyped logger, ?::Hash[Symbol, untyped] options) ?{ (Logging::Formatter formatter) -> void } -> void
230+
def call: (Env env) -> untyped
231+
def on_complete: (Env env) -> void
232+
end
233+
228234
def status: () -> Integer
229235
def headers: () -> untyped
230236
def body: () -> String
@@ -264,6 +270,42 @@ module Faraday
264270
def close: () -> void
265271
end
266272

273+
module Logging
274+
class Formatter
275+
extend Forwardable
276+
277+
DEFAULT_OPTIONS: ::Hash[Symbol, untyped]
278+
279+
@logger: untyped
280+
@options: ::Hash[Symbol, untyped]
281+
@filter: ::Array[[untyped, untyped]]
282+
283+
def initialize: (logger: untyped, options: ::Hash[Symbol, untyped]) -> void
284+
285+
def debug: (?untyped progname) ?{ () -> untyped } -> untyped
286+
def info: (?untyped progname) ?{ () -> untyped } -> untyped
287+
def warn: (?untyped progname) ?{ () -> untyped } -> untyped
288+
def error: (?untyped progname) ?{ () -> untyped } -> untyped
289+
def fatal: (?untyped progname) ?{ () -> untyped } -> untyped
290+
291+
def request: (Env env) -> void
292+
def response: (Env env) -> void
293+
def filter: (untyped filter_word, untyped filter_replacement) -> void
294+
295+
private
296+
297+
def dump_headers: (untyped headers) -> String
298+
def dump_body: (untyped body) -> String
299+
def pretty_inspect: (untyped body) -> String
300+
def log_headers?: (String | Symbol type) -> boolish
301+
def log_body?: (String | Symbol type) -> boolish
302+
def apply_filters: (untyped output) -> String
303+
def log_level: () -> Symbol
304+
def log_headers: (String type, untyped headers) -> untyped
305+
def log_body: (String type, untyped body) -> untyped
306+
end
307+
end
308+
267309
module MiddlewareRegistry
268310
def registered_middleware: () -> Hash[Symbol, untyped]
269311
def register_middleware: (**untyped mappings) -> void
@@ -329,5 +371,8 @@ module Faraday
329371
attr_accessor response_body: untyped
330372

331373
alias body request_body
374+
375+
def []: (untyped key) -> untyped
376+
def []=: (untyped key, untyped value) -> untyped
332377
end
333378
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class MyFormatter < Faraday::Logging::Formatter
2+
def exception(exc)
3+
# Build a custom message using `exc`
4+
info('Error') { 'Error Raised' }
5+
end
6+
7+
def dump_nil_headers
8+
dump_headers(nil)
9+
end
10+
end
11+
12+
conn = Faraday.new(url: 'http:///example.com') do |faraday|
13+
faraday.response :logger, nil, formatter: MyFormatter, errors: true
14+
end
15+
16+
conn.get('/abc')
17+
18+
formatter = MyFormatter.new(logger: Object.new, options: {})
19+
formatter.dump_nil_headers
20+
21+
logger = Faraday::Response::Logger.new(Object.new)
22+
logger.on_error(StandardError.new('boom'))
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class MyFormatter < Faraday::Logging::Formatter
2+
def exception: (untyped exc) -> void
3+
def dump_nil_headers: () -> nil
4+
end

gems/faraday/2.7/faraday-2.7.rbs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
module Faraday
22
class TooManyRequestsError < ClientError
33
end
4+
5+
class Response
6+
class Logger
7+
def on_error: (untyped exc) -> void
8+
end
9+
end
10+
11+
module Logging
12+
class Formatter
13+
def exception: (untyped exc) -> void
14+
15+
private
16+
17+
def dump_headers: (nil headers) -> nil
18+
| ...
19+
def log_errors?: () -> boolish
20+
end
21+
end
422
end

0 commit comments

Comments
 (0)