mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-18 23:12:17 +08:00
fix #2505, quick more naive version
This commit is contained in:
parent
540b1eff70
commit
7772aa912e
@ -33,7 +33,7 @@ const (
|
|||||||
reset = "\033[0m"
|
reset = "\033[0m"
|
||||||
)
|
)
|
||||||
|
|
||||||
var consoleColorMode = autoColor
|
var ConsoleColorMode = autoColor
|
||||||
|
|
||||||
// LoggerConfig defines the config for Logger middleware.
|
// LoggerConfig defines the config for Logger middleware.
|
||||||
type LoggerConfig struct {
|
type LoggerConfig struct {
|
||||||
@ -125,7 +125,7 @@ func (p *LogFormatterParams) ResetColor() string {
|
|||||||
|
|
||||||
// IsOutputColor indicates whether can colors be outputted to the log.
|
// IsOutputColor indicates whether can colors be outputted to the log.
|
||||||
func (p *LogFormatterParams) IsOutputColor() bool {
|
func (p *LogFormatterParams) IsOutputColor() bool {
|
||||||
return consoleColorMode == forceColor || (consoleColorMode == autoColor && p.isTerm)
|
return ConsoleColorMode == forceColor || (ConsoleColorMode == autoColor && p.isTerm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaultLogFormatter is the default log format function Logger middleware uses.
|
// defaultLogFormatter is the default log format function Logger middleware uses.
|
||||||
@ -154,12 +154,12 @@ var defaultLogFormatter = func(param LogFormatterParams) string {
|
|||||||
|
|
||||||
// DisableConsoleColor disables color output in the console.
|
// DisableConsoleColor disables color output in the console.
|
||||||
func DisableConsoleColor() {
|
func DisableConsoleColor() {
|
||||||
consoleColorMode = disableColor
|
ConsoleColorMode = disableColor
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForceConsoleColor force color output in the console.
|
// ForceConsoleColor force color output in the console.
|
||||||
func ForceConsoleColor() {
|
func ForceConsoleColor() {
|
||||||
consoleColorMode = forceColor
|
ConsoleColorMode = forceColor
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrorLogger returns a handlerfunc for any error type.
|
// ErrorLogger returns a handlerfunc for any error type.
|
||||||
|
@ -326,7 +326,7 @@ func TestIsOutputColor(t *testing.T) {
|
|||||||
isTerm: true,
|
isTerm: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
consoleColorMode = autoColor
|
ConsoleColorMode = autoColor
|
||||||
assert.Equal(t, true, p.IsOutputColor())
|
assert.Equal(t, true, p.IsOutputColor())
|
||||||
|
|
||||||
ForceConsoleColor()
|
ForceConsoleColor()
|
||||||
@ -340,7 +340,7 @@ func TestIsOutputColor(t *testing.T) {
|
|||||||
isTerm: false,
|
isTerm: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
consoleColorMode = autoColor
|
ConsoleColorMode = autoColor
|
||||||
assert.Equal(t, false, p.IsOutputColor())
|
assert.Equal(t, false, p.IsOutputColor())
|
||||||
|
|
||||||
ForceConsoleColor()
|
ForceConsoleColor()
|
||||||
@ -350,7 +350,7 @@ func TestIsOutputColor(t *testing.T) {
|
|||||||
assert.Equal(t, false, p.IsOutputColor())
|
assert.Equal(t, false, p.IsOutputColor())
|
||||||
|
|
||||||
// reset console color mode.
|
// reset console color mode.
|
||||||
consoleColorMode = autoColor
|
ConsoleColorMode = autoColor
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestErrorLogger(t *testing.T) {
|
func TestErrorLogger(t *testing.T) {
|
||||||
@ -415,20 +415,20 @@ func TestLoggerWithConfigSkippingPaths(t *testing.T) {
|
|||||||
|
|
||||||
func TestDisableConsoleColor(t *testing.T) {
|
func TestDisableConsoleColor(t *testing.T) {
|
||||||
New()
|
New()
|
||||||
assert.Equal(t, autoColor, consoleColorMode)
|
assert.Equal(t, autoColor, ConsoleColorMode)
|
||||||
DisableConsoleColor()
|
DisableConsoleColor()
|
||||||
assert.Equal(t, disableColor, consoleColorMode)
|
assert.Equal(t, disableColor, ConsoleColorMode)
|
||||||
|
|
||||||
// reset console color mode.
|
// reset console color mode.
|
||||||
consoleColorMode = autoColor
|
ConsoleColorMode = autoColor
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestForceConsoleColor(t *testing.T) {
|
func TestForceConsoleColor(t *testing.T) {
|
||||||
New()
|
New()
|
||||||
assert.Equal(t, autoColor, consoleColorMode)
|
assert.Equal(t, autoColor, ConsoleColorMode)
|
||||||
ForceConsoleColor()
|
ForceConsoleColor()
|
||||||
assert.Equal(t, forceColor, consoleColorMode)
|
assert.Equal(t, forceColor, ConsoleColorMode)
|
||||||
|
|
||||||
// reset console color mode.
|
// reset console color mode.
|
||||||
consoleColorMode = autoColor
|
ConsoleColorMode = autoColor
|
||||||
}
|
}
|
||||||
|
@ -50,8 +50,15 @@ func RecoveryWithWriter(out io.Writer, recovery ...RecoveryFunc) HandlerFunc {
|
|||||||
// CustomRecoveryWithWriter returns a middleware for a given writer that recovers from any panics and calls the provided handle func to handle it.
|
// CustomRecoveryWithWriter returns a middleware for a given writer that recovers from any panics and calls the provided handle func to handle it.
|
||||||
func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
|
||||||
var logger *log.Logger
|
var logger *log.Logger
|
||||||
|
|
||||||
|
// If gin.DisableConsoleColor() is invoked, respect it.
|
||||||
|
prefix := "\n\n"
|
||||||
|
if ConsoleColorMode != 1 {
|
||||||
|
prefix = "\n\n\x1b[31m"
|
||||||
|
}
|
||||||
|
|
||||||
if out != nil {
|
if out != nil {
|
||||||
logger = log.New(out, "\n\n\x1b[31m", log.LstdFlags)
|
logger = log.New(out, prefix, log.LstdFlags)
|
||||||
}
|
}
|
||||||
return func(c *Context) {
|
return func(c *Context) {
|
||||||
defer func() {
|
defer func() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user