New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the log formatter that is easy to override and safe to inherit #889
Conversation
@prikha: Thank you for making your suggestion AND filing a PR with an implementation. I agree with the pain points you stated in #888, however I'm not sold on the implementation. It leaves the Logger middleware class so bare that I wonder why it's even there. class Faraday::Response::Logger < Response::Middleware
def call(env)
@formatter.request(env)
super
end
def on_complete(env)
@formatter.response(env)
end
end Did you consider implementing I suppose passing a custom Logger would require something like this (as you suggested in #888): Faraday::Response.register_middleware logger: -> { FaradayMiddlewares::MyLogger }
conn = Faraday.new(:url => 'http://sushi.com/api_key=s3cr3t') do |faraday|
faraday.response :logger, logger
end That would at least let you replace all of the loggers in an application at once, but could obscure the fact that you're using a custom logger if you're just looking at a single instance of Faraday usage. Is there some big reason that you feel the Formatter should be extracted out to a separate class that I'm missing? I think refactoring the |
Hey @technoweenie thanks for taking a look. Strictly speaking there is nothing a decent programmer can't overcome(well at least in Ruby 😆). One can invent custom logging middleware and then do whatever I want there. That is true. However I would like to safely reuse some of the code from the I see no issue in having a logging middleware just for the purpose of exposing the logging capabilities. With the approach I suggest one can safely inherit from the current formatter. However inheriting from logging middleware implies that every change to parents On the other hand custom middleware accepts a logger which is expected to have more or less trivial behaviour, like expose some methods having some signatures. Well ruby isn't a typed language, but when I see Maybe it would make sense to have In general it would be cool to have logging extracted out of To sum up, I see #888 as an opportunity to improve the integration points of |
To add on top of what @technoweenie already said, I'm personally happy with both solutions (a more modular logger contract, or a separate formatter). However I'd like to point out that the current implementation doesn't allow to reuse some useful methods even if you sub-class |
@iMacTia thanks for participating. Maybe I'm not the best seller(I appreciate some help here), I am happy to tune the code to the point where we get a clean point of integration, however. Can you clarify about the |
@prikha My fault, I was sure you couldn't access |
@technoweenie Although both solutions are perfectly fine, I personally like the separation of concerns introduced with this PR as it clearly identifies the different actors in the logging process:
It's quite similar to how |
Great, lets go with this PR's approach 🤘 My only concern now is whether it breaks any existing APIs. Just looking at the current Logger class, we may have to keep |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me, but I'd like to see a test with a custom formatter in spec/faraday/response/logger_spec.rb
.
lib/faraday/response/logger.rb
Outdated
end | ||
|
||
def pretty_inspect(body) | ||
require 'pp' unless body.respond_to?(:pretty_inspect) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any thoughts on moving this to the top of the file instead? The Faraday Middleware Registry should prevent this from being loaded until someone uses the logger middleware for the first time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense to me
@technoweenie comments addressed, when we agree on the changes - I'll rebase and fix conflicts. |
@prikha Thanks for addressing the comments. I commented on a nit pick in the tests, but I won't let that block the merging of this PR. We should be able to merge this once you fix the conflict. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to go for me as well as soon as conflict with base branch are fixed 👍 !
3b097ca
to
9d1a970
Compare
9d1a970
to
ebb7c36
Compare
@iMacTia @technoweenie Thanks for the tip. Addressed. Give it a quick look and it's good to go. |
🌞 😍 🌞 Thank you so much for taking this all the way! |
Description
Extract Formatter from logging middleware and enable it to be configurable.
Fixes #888