-
Notifications
You must be signed in to change notification settings - Fork 54
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
log plugin #16
Comments
Basically, you've to read and cache both bodies In certain scenarios you have to do some pre/post processing of steams data, so the typical strategy would be reading the whole stream, caching it and create a new stream to replace the original, already used one. You can achieve this via Also, in order to read the response body data, you should perform all of this process before the I hope this help. |
Thanks for your detailed explanation. I helps me a lot!
I don't know the difference between middlewares and plugins, so I tried both. The plugin case is comment-outed above at the line
Could you give me advice? |
gentleman is plugin-driven, but plugins require a bit of interface overhead, so middleware handlers are the simplified version of a phase-specific plugin without constructor/interface overhead, just define the phase name and function to execute. This is simply provided for convenience, but behind the scenes all are plugins. You code is fine. The problem is due the plugin registration priority. Effectively, gentleman has no explicit plugin priority as sort of priority queue structure, so the execution order is defined by the developer, behaving as a simple FIFO queue. Since gentleman is fully plugin-driven, this affects to the whole library. In this case you're registering the request log plugin before the body is defined to the Request instance, so that's why you can't read it. Solutions:
cli.Request().
Path("/_db/_system/_api/version").
BodyString(`{"dummy":"not_used"}`).
Use(genlog.RequestSaver(reqKey)).
Send()
body := `{"dummy":"not_used"}`
req := cli.Request()
req.Path("/_db/_system/_api/version")
req.Context.Set("req.body", body)
req.Body(body)
req.Send() There are other approaches, but I think less convenient. |
Thanks again for your detailed explanation!
The output.
Could you tell me what do you think? |
That's the perfect one. I completely forget about the If you even want to have a cleaner and more elegant solution, you can write a generic log plugin who will trigger in both phases: |
Also, if you want to contribute and you're open to, you can create an open source log plugin with a structured, modular design, giving the ability to developers to register its own log handlers, responsible of writing logs events to different outputs, such as |
Thanks for your comment. A sample program.
log plugin.
example output.
I made two separated plugins And yes, I want to contribute this log plugin. Can I send a pull request to create the github.com/h2non/gentleman/plugins/log package? Could you tell me what do you think? |
Thanks for sharing the code. I would suggest the following points:
|
I wrote a simple Apache-like HTTP logger for the server side that you can take a reference, however it's pretty simple not designed for versatility, just log things as Apache does: |
Thanks for your suggestions. Thanks again for you help! |
That's nice. If you want to evolve it in the future, I would suggest to take an eye apex/log structured implemented as design reference point: Once the package is mature and versatile enough, you can make a PR request to add it to the list of plugins. |
First of all, thanks for creating and sharing a nice software!
I would like to have log plugin which will log requests and responses using github.com/go-kit/kig/log.Logger interface.
I tried to create such a plugin myself, but I'm stuck now.
Here is an example usage.
Here is my plugin code.
Could you tell me how to retrieve the request body and the response body?
The text was updated successfully, but these errors were encountered: