mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
Fix recover print stack trace log level (#1604)
* Fix recover print stack trace log level * Add recover log level test * Add default LogLevel to DefaultRecoverConfig
This commit is contained in:
parent
de3a2d4df3
commit
e125b2cf84
@ -5,6 +5,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/labstack/gommon/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -25,6 +26,10 @@ type (
|
|||||||
// DisablePrintStack disables printing stack trace.
|
// DisablePrintStack disables printing stack trace.
|
||||||
// Optional. Default value as false.
|
// Optional. Default value as false.
|
||||||
DisablePrintStack bool `yaml:"disable_print_stack"`
|
DisablePrintStack bool `yaml:"disable_print_stack"`
|
||||||
|
|
||||||
|
// LogLevel is log level to printing stack trace.
|
||||||
|
// Optional. Default value 0 (Print).
|
||||||
|
LogLevel log.Lvl
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -35,6 +40,7 @@ var (
|
|||||||
StackSize: 4 << 10, // 4 KB
|
StackSize: 4 << 10, // 4 KB
|
||||||
DisableStackAll: false,
|
DisableStackAll: false,
|
||||||
DisablePrintStack: false,
|
DisablePrintStack: false,
|
||||||
|
LogLevel: 0,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -70,7 +76,21 @@ func RecoverWithConfig(config RecoverConfig) echo.MiddlewareFunc {
|
|||||||
stack := make([]byte, config.StackSize)
|
stack := make([]byte, config.StackSize)
|
||||||
length := runtime.Stack(stack, !config.DisableStackAll)
|
length := runtime.Stack(stack, !config.DisableStackAll)
|
||||||
if !config.DisablePrintStack {
|
if !config.DisablePrintStack {
|
||||||
c.Logger().Printf("[PANIC RECOVER] %v %s\n", err, stack[:length])
|
msg := fmt.Sprintf("[PANIC RECOVER] %v %s\n", err, stack[:length])
|
||||||
|
switch config.LogLevel {
|
||||||
|
case log.DEBUG:
|
||||||
|
c.Logger().Debug(msg)
|
||||||
|
case log.INFO:
|
||||||
|
c.Logger().Info(msg)
|
||||||
|
case log.WARN:
|
||||||
|
c.Logger().Warn(msg)
|
||||||
|
case log.ERROR:
|
||||||
|
c.Logger().Error(msg)
|
||||||
|
case log.OFF:
|
||||||
|
// None.
|
||||||
|
default:
|
||||||
|
c.Logger().Print(msg)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c.Error(err)
|
c.Error(err)
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,13 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/labstack/gommon/log"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,3 +26,58 @@ func TestRecover(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
||||||
assert.Contains(t, buf.String(), "PANIC RECOVER")
|
assert.Contains(t, buf.String(), "PANIC RECOVER")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRecoverWithConfig_LogLevel(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
logLevel log.Lvl
|
||||||
|
levelName string
|
||||||
|
}{{
|
||||||
|
logLevel: log.DEBUG,
|
||||||
|
levelName: "DEBUG",
|
||||||
|
}, {
|
||||||
|
logLevel: log.INFO,
|
||||||
|
levelName: "INFO",
|
||||||
|
}, {
|
||||||
|
logLevel: log.WARN,
|
||||||
|
levelName: "WARN",
|
||||||
|
}, {
|
||||||
|
logLevel: log.ERROR,
|
||||||
|
levelName: "ERROR",
|
||||||
|
}, {
|
||||||
|
logLevel: log.OFF,
|
||||||
|
levelName: "OFF",
|
||||||
|
}}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
tt := tt
|
||||||
|
t.Run(tt.levelName, func(t *testing.T) {
|
||||||
|
e := echo.New()
|
||||||
|
e.Logger.SetLevel(log.DEBUG)
|
||||||
|
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
e.Logger.SetOutput(buf)
|
||||||
|
|
||||||
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
c := e.NewContext(req, rec)
|
||||||
|
|
||||||
|
config := DefaultRecoverConfig
|
||||||
|
config.LogLevel = tt.logLevel
|
||||||
|
h := RecoverWithConfig(config)(echo.HandlerFunc(func(c echo.Context) error {
|
||||||
|
panic("test")
|
||||||
|
}))
|
||||||
|
|
||||||
|
h(c)
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
||||||
|
|
||||||
|
output := buf.String()
|
||||||
|
if tt.logLevel == log.OFF {
|
||||||
|
assert.Empty(t, output)
|
||||||
|
} else {
|
||||||
|
assert.Contains(t, output, "PANIC RECOVER")
|
||||||
|
assert.Contains(t, output, fmt.Sprintf(`"level":"%s"`, tt.levelName))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user