mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 21:32:11 +08:00
Add test for logger
This commit is contained in:
parent
46516f7d85
commit
6040638a26
@ -38,7 +38,7 @@ var consoleColorMode = autoColor
|
|||||||
|
|
||||||
// LoggerConfig defines the config for Logger middleware.
|
// LoggerConfig defines the config for Logger middleware.
|
||||||
type LoggerConfig struct {
|
type LoggerConfig struct {
|
||||||
// Optional. Default value is gin.defaultLogFormatter
|
// Formatter is optional, the default value is gin.defaultLogFormatter
|
||||||
Formatter LogFormatter
|
Formatter LogFormatter
|
||||||
|
|
||||||
// Output is a writer where logs are written.
|
// Output is a writer where logs are written.
|
||||||
@ -265,9 +265,7 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
|||||||
|
|
||||||
param.Path = path
|
param.Path = path
|
||||||
|
|
||||||
if _, err := out.Write(bytesconv.StringToBytes(formatter(param))); err != nil {
|
out.Write(bytesconv.StringToBytes(formatter(param)))
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -147,6 +147,15 @@ func TestLoggerWithConfig(t *testing.T) {
|
|||||||
assert.Contains(t, buffer.String(), "/notfound")
|
assert.Contains(t, buffer.String(), "/notfound")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoggerWithConfigWriteError(t *testing.T) {
|
||||||
|
w := &mockErrWriter{}
|
||||||
|
router := New()
|
||||||
|
router.Use(LoggerWithConfig(LoggerConfig{Output: w}))
|
||||||
|
router.GET("/example", func(c *Context) {})
|
||||||
|
PerformRequest(router, "GET", "/example?a=100")
|
||||||
|
assert.True(t, w.triggerErrorWrite)
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoggerWithFormatter(t *testing.T) {
|
func TestLoggerWithFormatter(t *testing.T) {
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
|
|
||||||
@ -432,3 +441,12 @@ func TestForceConsoleColor(t *testing.T) {
|
|||||||
// reset console color mode.
|
// reset console color mode.
|
||||||
consoleColorMode = autoColor
|
consoleColorMode = autoColor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type mockErrWriter struct {
|
||||||
|
triggerErrorWrite bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *mockErrWriter) Write(p []byte) (n int, err error) {
|
||||||
|
w.triggerErrorWrite = true
|
||||||
|
return 0, errors.New("test error writer")
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user