Skip to content

faraday: Add logging formatter signatures#1037

Merged
github-actions[bot] merged 1 commit into
ruby:mainfrom
rossta:feat/faraday-logging-formatter
May 20, 2026
Merged

faraday: Add logging formatter signatures#1037
github-actions[bot] merged 1 commit into
ruby:mainfrom
rossta:feat/faraday-logging-formatter

Conversation

@rossta
Copy link
Copy Markdown
Contributor

@rossta rossta commented May 18, 2026

@github-actions
Copy link
Copy Markdown

@rossta Thanks for your contribution!

Please follow the instructions below for each change.
See also: https://github.com/ruby/gem_rbs_collection/blob/main/docs/CONTRIBUTING.md

Available commands

You can use the following commands by commenting on this PR.

  • /merge: Merge this PR if CI passes

faraday

You changed RBS files for an existing gem.
You need to get approval from the reviewers of this gem.

@yykamei, please review this pull request.
If this change is acceptable, please make a review comment including APPROVE from here.
Screen Shot 2024-03-19 at 14 13 36

After that, the PR author or the reviewers can merge this PR.
Just comment /merge to merge this PR.

Copy link
Copy Markdown
Contributor

@yykamei yykamei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for putting this together! Adding signatures for the logging formatter is really helpful for users writing custom formatters, and the example in _test/ makes the intended usage very clear. Appreciate the contribution!

end

module Logging
class Formatter
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these return types should be void rather than untyped.
In the actual implementation, request, response, exception, and filter are side-effect-oriented methods: they write logs or update the formatter's filter list, and their return values are not part of the public contract.

def request: (Env env) -> void
def response: (Env env) -> void
def exception: (untyped exc) -> void
def filter: (untyped filter_word, untyped filter_replacement) -> void

The test signature in _test/test_logging_formatter.rbs should probably use void as well, since it documents the expected override contract for custom formatters.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in request, response, filter changes in gems/faraday/2.5. Addressed exception in gems/faraday/2.7/faraday-2.7.rbs.

end

class Response
class Logger < Middleware
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Faraday::Response::Logger also defines on_error(exc) in the implementation, but it is missing from this signature.
Could you add it here?

def on_error: (untyped exc) -> void

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented in gems/faraday/2.7/faraday-2.7.rbs

Comment thread gems/faraday/2.5/faraday.rbs Outdated
class Logger < Middleware
def initialize: (untyped app, ?untyped logger, ?::Hash[Symbol, untyped] options) ?{ (Logging::Formatter formatter) -> void } -> void
def call: (Env env) -> untyped
def on_complete: (Env env) -> untyped
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since on_complete only delegates to @formatter.response(env) and the formatter method is side-effect-oriented, I think this should be void rather than untyped.

Suggested change
def on_complete: (Env env) -> untyped
def on_complete: (Env env) -> void

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!


private

def dump_headers: (untyped headers) -> String
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dump_headers can return nil when headers is nil in the implementation:

return if headers.nil?

So this should be String? rather than String.

Suggested change
def dump_headers: (untyped headers) -> String
def dump_headers: (untyped headers) -> String?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added signature overload in gems/faraday/2.7/faraday-2.7.rbs

def response: (Env env) -> untyped
def filter: (untyped filter_word, untyped filter_replacement) -> ::Array[[untyped, untyped]]

private
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation also has a private log_errors? method, which is used by exception, but it is missing from the RBS.

Could you add it alongside log_headers? and log_body??

def log_errors?: () -> boolish

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added to gems/faraday/2.7/faraday-2.7.rbs

@rossta rossta force-pushed the feat/faraday-logging-formatter branch from 057a326 to b3dbade Compare May 19, 2026 15:06
@rossta
Copy link
Copy Markdown
Contributor Author

rossta commented May 19, 2026

@yykamei Thank you for the thorough review!

To be sure signatures were exposed in the correct VERSION/faraday.rbs, I reviewed Faraday sources for 2.5.x, 2.6.0, and 2.7.x releases.

Confirmed for 2.5/faraday.rbs:

  • Faraday::Logging::Formatter, request, response, filter, delegated debug/info/warn/error/fatal, log_headers?, and log_body? are present from 2.5.0.
  • Faraday::Response::Logger#on_complete is present from 2.5.0.
  • log_errors?, on_error, and exception are not part of 2.5.
  • dump_headers does not have the nil guard in 2.5, so I left it as String.

Patch availability for 2.7-only surface:

  • Faraday::Response::Logger#on_error: added in 2.7.0.
  • Faraday::Logging::Formatter#log_errors?: added in 2.7.0.
  • Faraday::Logging::Formatter#error as the error callback: added in 2.7.0, replaced by exception in 2.7.2.
  • Faraday::Logging::Formatter#exception: added in 2.7.2.
  • dump_headers returning nil for nil headers: added in 2.7.8.

Implemented:

  • Updated 2.5 side-effect methods to return void
  • Added 2.7 overlay signatures for on_error, exception, and log_errors?
  • Added 2.7 overloaded signature for dump_headers returning nil
  • Added 2.7 formatter tests
  • Amended commit

Verified:

bin/test gems/faraday/2.5
bin/test gems/faraday/2.7

Current amended commit is 2acd424. Please let me know whether this satisfies your comments.

@rossta rossta force-pushed the feat/faraday-logging-formatter branch from b3dbade to 2acd424 Compare May 19, 2026 15:23
Copy link
Copy Markdown
Contributor

@yykamei yykamei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

APPROVE

@github-actions
Copy link
Copy Markdown

Thanks for your review, @yykamei!

@rossta, @yykamei This PR is ready to be merged.
Just comment /merge to merge this PR.

@yykamei
Copy link
Copy Markdown
Contributor

yykamei commented May 20, 2026

/merge

@github-actions github-actions Bot merged commit 3f5e8df into ruby:main May 20, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants