-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
🔥 feat: Add Filter option to logger middleware #3333
base: main
Are you sure you want to change the base?
Changes from 18 commits
8a74eba
0a70ee8
efdd738
a652e62
c1f2278
2794761
2fd940e
7dcc904
d6e64a0
4cb5e1c
93b57a9
572233e
b600183
2154baf
10e6cb9
6528b9f
75976af
288edb2
e2295e5
0ac13c8
bb05227
d3030b1
82e27f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,6 +171,58 @@ func Test_Logger_Done(t *testing.T) { | |
require.Positive(t, buf.Len(), 0) | ||
} | ||
|
||
func Test_Logger_Filter(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused about how this is working. If the condition is True, it will become False in defaultLogger. But we dont check for that in the test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gaby Thanks for the heads up. I've just checked and found a few things that need to be refined, it's almost midnight here, I'll submit a more complete unit test and documentation note tomorrow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good. Thanks! |
||
t.Parallel() | ||
|
||
t.Run("Test Not Found", func(t *testing.T) { | ||
t.Parallel() | ||
app := fiber.New() | ||
|
||
logOutput := bytes.Buffer{} | ||
|
||
// Create a single logging middleware with both filter and output capture | ||
app.Use(New(Config{ | ||
Filter: func(c fiber.Ctx) bool { | ||
return c.Response().StatusCode() == fiber.StatusNotFound | ||
}, | ||
Output: &logOutput, | ||
})) | ||
|
||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/nonexistent", nil)) | ||
require.NoError(t, err) | ||
require.Equal(t, fiber.StatusNotFound, resp.StatusCode) | ||
|
||
// Verify the log output contains the "404" message | ||
require.Contains(t, logOutput.String(), "404") | ||
}) | ||
|
||
t.Run("Test OK", func(t *testing.T) { | ||
t.Parallel() | ||
app := fiber.New() | ||
|
||
logOutput := bytes.Buffer{} | ||
|
||
// Create a single logging middleware with both filter and output capture | ||
app.Use(New(Config{ | ||
Filter: func(c fiber.Ctx) bool { | ||
return c.Response().StatusCode() == fiber.StatusNotFound | ||
}, | ||
Output: &logOutput, | ||
})) | ||
|
||
app.Get("/", func(c fiber.Ctx) error { | ||
return c.SendStatus(fiber.StatusOK) | ||
}) | ||
|
||
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil)) | ||
require.NoError(t, err) | ||
require.Equal(t, fiber.StatusOK, resp.StatusCode) | ||
|
||
// Verify the log output does not contain the "200" message | ||
require.NotContains(t, logOutput.String(), "200") | ||
}) | ||
} | ||
|
||
// go test -run Test_Logger_ErrorTimeZone | ||
func Test_Logger_ErrorTimeZone(t *testing.T) { | ||
t.Parallel() | ||
|
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.
Shouldn't this be
!=
?If its 404 it returns True, which the defaultLogger negates into False and doesn't filter
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.
well, if he only wants to log every 404, then that should be fine
depends on what he wants to do
it is always logged if the condition is true
-> if of course he wants everything else to be logged except 404, then an unequal should be used
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.
@JIeJaitt
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.
@ReneWerner87 I've added new examples in the documentation, you can take a look at the