mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-23 11:33:37 +08:00
Merge cb90cbdeea1e065b088af7bc26f5515301987404 into 0f09c3a9b4626d9fe9979cf4d0521d6ae32ab646
This commit is contained in:
commit
b7536c675f
20
logger.go
20
logger.go
@ -203,6 +203,26 @@ func ForceConsoleColor() {
|
||||
consoleColorMode = forceColor
|
||||
}
|
||||
|
||||
// consoleColorEnabled reports whether ANSI colors should be written to out,
|
||||
// matching Logger's DisableConsoleColor / ForceConsoleColor / auto-terminal rules.
|
||||
func consoleColorEnabled(out io.Writer) bool {
|
||||
switch consoleColorMode {
|
||||
case disableColor:
|
||||
return false
|
||||
case forceColor:
|
||||
return true
|
||||
default:
|
||||
return writerIsTerminal(out)
|
||||
}
|
||||
}
|
||||
|
||||
func writerIsTerminal(out io.Writer) bool {
|
||||
if w, ok := out.(*os.File); ok {
|
||||
return isatty.IsTerminal(w.Fd()) || isatty.IsCygwinTerminal(w.Fd())
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ErrorLogger returns a HandlerFunc for any error type.
|
||||
func ErrorLogger() HandlerFunc {
|
||||
return ErrorLoggerT(ErrorTypeAny)
|
||||
|
||||
17
recovery.go
17
recovery.go
@ -53,7 +53,9 @@ func RecoveryWithWriter(out io.Writer, recovery ...RecoveryFunc) HandlerFunc {
|
||||
func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
||||
var logger *log.Logger
|
||||
if out != nil {
|
||||
logger = log.New(out, "\n\n\x1b[31m", log.LstdFlags)
|
||||
// Color prefix/reset are applied per panic so DisableConsoleColor /
|
||||
// ForceConsoleColor remain effective after the middleware is created.
|
||||
logger = log.New(out, "\n\n", log.LstdFlags)
|
||||
}
|
||||
return func(c *Context) {
|
||||
defer func() {
|
||||
@ -68,14 +70,21 @@ func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
||||
errors.Is(err, http.ErrAbortHandler)
|
||||
}
|
||||
if logger != nil {
|
||||
colorReset := ""
|
||||
if consoleColorEnabled(out) {
|
||||
colorReset = reset
|
||||
logger.SetPrefix("\n\n\x1b[31m")
|
||||
} else {
|
||||
logger.SetPrefix("\n\n")
|
||||
}
|
||||
if isBrokenPipe {
|
||||
logger.Printf("%s\n%s%s", rec, secureRequestDump(c.Request), reset)
|
||||
logger.Printf("%s\n%s%s", rec, secureRequestDump(c.Request), colorReset)
|
||||
} else if IsDebugging() {
|
||||
logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s",
|
||||
timeFormat(time.Now()), secureRequestDump(c.Request), rec, stack(stackSkip), reset)
|
||||
timeFormat(time.Now()), secureRequestDump(c.Request), rec, stack(stackSkip), colorReset)
|
||||
} else {
|
||||
logger.Printf("[Recovery] %s panic recovered:\n%s\n%s%s",
|
||||
timeFormat(time.Now()), rec, stack(stackSkip), reset)
|
||||
timeFormat(time.Now()), rec, stack(stackSkip), colorReset)
|
||||
}
|
||||
}
|
||||
if isBrokenPipe {
|
||||
|
||||
@ -405,6 +405,74 @@ func TestReadNthLine(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecoveryRespectsConsoleColorMode asserts Recovery honors DisableConsoleColor /
|
||||
// ForceConsoleColor (issue #2505). Without ForceConsoleColor, a non-terminal writer
|
||||
// must not receive ANSI escape sequences.
|
||||
func TestRecoveryRespectsConsoleColorMode(t *testing.T) {
|
||||
prev := consoleColorMode
|
||||
t.Cleanup(func() { consoleColorMode = prev })
|
||||
|
||||
runPanic := func() string {
|
||||
buffer := new(strings.Builder)
|
||||
router := New()
|
||||
router.Use(RecoveryWithWriter(buffer))
|
||||
router.GET("/recovery", func(_ *Context) {
|
||||
panic("color-mode check")
|
||||
})
|
||||
_ = PerformRequest(router, http.MethodGet, "/recovery")
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
DisableConsoleColor()
|
||||
out := runPanic()
|
||||
assert.Contains(t, out, "panic recovered")
|
||||
assert.Contains(t, out, "color-mode check")
|
||||
assert.NotContains(t, out, "\x1b[31m")
|
||||
assert.NotContains(t, out, reset)
|
||||
|
||||
ForceConsoleColor()
|
||||
out = runPanic()
|
||||
assert.Contains(t, out, "panic recovered")
|
||||
assert.Contains(t, out, "\x1b[31m")
|
||||
assert.Contains(t, out, reset)
|
||||
|
||||
consoleColorMode = autoColor
|
||||
out = runPanic()
|
||||
assert.Contains(t, out, "panic recovered")
|
||||
assert.NotContains(t, out, "\x1b[31m")
|
||||
assert.NotContains(t, out, reset)
|
||||
}
|
||||
|
||||
func TestRecoveryWithFileWriterAutoColor(t *testing.T) {
|
||||
prev := consoleColorMode
|
||||
t.Cleanup(func() { consoleColorMode = prev })
|
||||
consoleColorMode = autoColor
|
||||
|
||||
file, err := os.CreateTemp(t.TempDir(), "recovery-*.log")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { assert.NoError(t, file.Close()) })
|
||||
|
||||
router := New()
|
||||
router.Use(RecoveryWithWriter(file))
|
||||
router.GET("/recovery", func(_ *Context) {
|
||||
panic("file-writer check")
|
||||
})
|
||||
|
||||
w := PerformRequest(router, http.MethodGet, "/recovery")
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
|
||||
output, err := os.ReadFile(file.Name())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Contains(t, string(output), "panic recovered")
|
||||
assert.Contains(t, string(output), "file-writer check")
|
||||
assert.Contains(t, string(output), t.Name())
|
||||
assert.NotContains(t, string(output), "\x1b[")
|
||||
}
|
||||
|
||||
func BenchmarkStack(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for b.Loop() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user