Skip to content
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

re-implementing default logging middleware #313

Merged
merged 5 commits into from
Dec 29, 2024

Conversation

jwtly10
Copy link
Contributor

@jwtly10 jwtly10 commented Dec 26, 2024

This PR implements a simplified default logging middleware, taking on feedback from #303.

These changes simplify #303 and just provides default req/res logging, with options to disable either. Rather than supporting custom logging functions, users who need custom logging can just implement their own mw.

I see some of these use cases:

  1. Use default middleware entirely
  2. Disable fully/partially disable req&res logging
  3. Disable and implement their own logging middleware
  4. Use our middleware but override logging funcs

After thinking about how to do this, supporting case 4 does seems unnecessary and more complex that it needs to be - for users needing custom logging it would be cleaner to just implement their own.

Note: Some tests are failing for now due to some golden files not expecting this new middleware by default.

@jwtly10 jwtly10 force-pushed the chore/default-logging-middleware branch 2 times, most recently from 2ae7226 to e4c5bb8 Compare December 26, 2024 11:12
Copy link
Member

@EwenQuim EwenQuim left a comment

Choose a reason for hiding this comment

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

Very good PR!

Looking very close to what I expected, thank you very much.

I'd let the possibility for users to provide their own requestId, that's a really common use case. Just add a func() string in LoggingConfig :)

Thank you again!

default_middlewares.go Outdated Show resolved Hide resolved
default_middlewares.go Outdated Show resolved Hide resolved
@EwenQuim
Copy link
Member

Also, I'd add some tests with slogassert. There are some test existing in the codebase that you can mimic.

default_middlewares.go Outdated Show resolved Hide resolved
@jwtly10 jwtly10 force-pushed the chore/default-logging-middleware branch 2 times, most recently from abd8127 to 82efea5 Compare December 26, 2024 15:13
@jwtly10
Copy link
Contributor Author

jwtly10 commented Dec 26, 2024

The only thing I haven't addressed yet is failing tests due to including the new mw in open api docs:

 expected: "#### Controller: \n\n`github.com/go-fuego/fuego_test.helloWorld`\n\n---\n\ntest description"
 actual  : "#### Controller: \n\n`github.com/go-fuego/fuego_test.helloWorld`\n\n#### Middlewares:\n\n- `github.com/go-fuego/fuego.NewServer.defaultLoggingMiddleware.func16`\n\n---\n\ntest description"

./examples/petstore/lib/server_test.go: (I guess this would be best fixed with simple golden file update)

openapi_operations_test.go/options_test.go

I'm not sure what the best way to approach this is for these tests. I guess 2 options off the top of my head are

  1. disable the middleware for these tests
  2. update all the tests to handle this default case of having middleware attached as standard (will also need to change defaultLoggingMiddleware from being an anonymouse function to stop .func16 showing in docs)

Any other ideas?

@jwtly10 jwtly10 force-pushed the chore/default-logging-middleware branch from 82efea5 to 2a657fb Compare December 26, 2024 16:10
@EwenQuim
Copy link
Member

EwenQuim commented Dec 26, 2024

Please update the tests with make golden-update. The petstore example is an example of Fuego offers out-of-the-box, so it makes sense that the middleware is registered and shown.

We don't want to hide anything

@EwenQuim
Copy link
Member

EwenQuim commented Dec 26, 2024

will also need to change defaultLoggingMiddleware from being an anonymouse function to stop .func16 showing in docs

Yes and we need a goodNaming. I suggest defaultLogger? @dylanhitt any idea? If @jwtly10 you have a better idea, don't hesitate :)

Also, I think that if you keep the closure pattern, you'll always have a func in the name. You might need to make a method on a struct to pass arguments. It makes one more alloc on the stack but that should be fine. Maybe struct defaultLogger and the method middleware or handler ?

@jwtly10
Copy link
Contributor Author

jwtly10 commented Dec 26, 2024

@EwenQuim thanks for the feedback. defaultLogger makes sense for now. I guess it can be changed in future if needed.

Changes have been made, all tests are now passing :)

@jwtly10 jwtly10 marked this pull request as ready for review December 26, 2024 18:11
@dylanhitt
Copy link
Collaborator

On mobile, can you send a picture of the way it looks in stoplight?

@jwtly10
Copy link
Contributor Author

jwtly10 commented Dec 26, 2024

@dylanhitt Not sure what you mean? Do you mean how the open api spec renders when it has this middleware? (using stoplight on mobile?)

If I am completely misunderstanding and you mean you're on mobile and would like a screenshot, here you go, its the pet open api doc:

image

@dylanhitt
Copy link
Collaborator

Oh no, the screenshot is what I needed. Thank you. I like this

@dylanhitt dylanhitt force-pushed the chore/default-logging-middleware branch from cd306cc to 7fa2a70 Compare December 29, 2024 16:48
@dylanhitt
Copy link
Collaborator

I believe @EwenQuim request changes have been addressed. This closes #265

Thank you for the work @jwtly10! We appreciate it.

Cheers

@dylanhitt dylanhitt merged commit 2d6cfa4 into go-fuego:main Dec 29, 2024
5 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.

3 participants