mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-22 01:12:16 +08:00
IsTerm flag should not be affected by DisableConsoleColor method.
This commit is contained in:
parent
3dc247893e
commit
7f3e50236e
43
logger.go
43
logger.go
@ -14,17 +14,24 @@ import (
|
|||||||
"github.com/mattn/go-isatty"
|
"github.com/mattn/go-isatty"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type consoleColorModeValue int
|
||||||
|
|
||||||
|
const (
|
||||||
|
autoColor consoleColorModeValue = iota
|
||||||
|
disableColor
|
||||||
|
forceColor
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
|
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
|
||||||
white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
|
white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
|
||||||
yellow = string([]byte{27, 91, 57, 48, 59, 52, 51, 109})
|
yellow = string([]byte{27, 91, 57, 48, 59, 52, 51, 109})
|
||||||
red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
|
red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
|
||||||
blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
|
blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
|
||||||
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
|
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
|
||||||
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
|
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
|
||||||
reset = string([]byte{27, 91, 48, 109})
|
reset = string([]byte{27, 91, 48, 109})
|
||||||
disableColor = false
|
consoleColorMode = autoColor
|
||||||
forceColor = false
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoggerConfig defines the config for Logger middleware.
|
// LoggerConfig defines the config for Logger middleware.
|
||||||
@ -115,10 +122,15 @@ func (p *LogFormatterParams) ResetColor() string {
|
|||||||
return reset
|
return reset
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsOutputColor indicates whether can colors be outputted to the log.
|
||||||
|
func (p *LogFormatterParams) IsOutputColor() bool {
|
||||||
|
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.
|
||||||
var defaultLogFormatter = func(param LogFormatterParams) string {
|
var defaultLogFormatter = func(param LogFormatterParams) string {
|
||||||
var statusColor, methodColor, resetColor string
|
var statusColor, methodColor, resetColor string
|
||||||
if param.IsTerm {
|
if param.IsOutputColor() {
|
||||||
statusColor = param.StatusCodeColor()
|
statusColor = param.StatusCodeColor()
|
||||||
methodColor = param.MethodColor()
|
methodColor = param.MethodColor()
|
||||||
resetColor = param.ResetColor()
|
resetColor = param.ResetColor()
|
||||||
@ -137,12 +149,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() {
|
||||||
disableColor = true
|
consoleColorMode = disableColor
|
||||||
}
|
}
|
||||||
|
|
||||||
// ForceConsoleColor force color output in the console.
|
// ForceConsoleColor force color output in the console.
|
||||||
func ForceConsoleColor() {
|
func ForceConsoleColor() {
|
||||||
forceColor = true
|
consoleColorMode = forceColor
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrorLogger returns a handlerfunc for any error type.
|
// ErrorLogger returns a handlerfunc for any error type.
|
||||||
@ -199,9 +211,8 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
|
|||||||
|
|
||||||
isTerm := true
|
isTerm := true
|
||||||
|
|
||||||
if w, ok := out.(*os.File); (!ok ||
|
if w, ok := out.(*os.File); !ok || os.Getenv("TERM") == "dumb" ||
|
||||||
(os.Getenv("TERM") == "dumb" || (!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd()))) ||
|
(!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd())) {
|
||||||
disableColor) && !forceColor {
|
|
||||||
isTerm = false
|
isTerm = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -297,6 +297,39 @@ func TestResetColor(t *testing.T) {
|
|||||||
assert.Equal(t, string([]byte{27, 91, 48, 109}), p.ResetColor())
|
assert.Equal(t, string([]byte{27, 91, 48, 109}), p.ResetColor())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsOutputColor(t *testing.T) {
|
||||||
|
// test with IsTerm flag true.
|
||||||
|
p := LogFormatterParams{
|
||||||
|
IsTerm: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
consoleColorMode = autoColor
|
||||||
|
assert.Equal(t, true, p.IsOutputColor())
|
||||||
|
|
||||||
|
ForceConsoleColor()
|
||||||
|
assert.Equal(t, true, p.IsOutputColor())
|
||||||
|
|
||||||
|
DisableConsoleColor()
|
||||||
|
assert.Equal(t, false, p.IsOutputColor())
|
||||||
|
|
||||||
|
// test with IsTerm flag false.
|
||||||
|
p = LogFormatterParams{
|
||||||
|
IsTerm: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
consoleColorMode = autoColor
|
||||||
|
assert.Equal(t, false, p.IsOutputColor())
|
||||||
|
|
||||||
|
ForceConsoleColor()
|
||||||
|
assert.Equal(t, true, p.IsOutputColor())
|
||||||
|
|
||||||
|
DisableConsoleColor()
|
||||||
|
assert.Equal(t, false, p.IsOutputColor())
|
||||||
|
|
||||||
|
// reset console color mode.
|
||||||
|
consoleColorMode = autoColor
|
||||||
|
}
|
||||||
|
|
||||||
func TestErrorLogger(t *testing.T) {
|
func TestErrorLogger(t *testing.T) {
|
||||||
router := New()
|
router := New()
|
||||||
router.Use(ErrorLogger())
|
router.Use(ErrorLogger())
|
||||||
@ -359,14 +392,20 @@ func TestLoggerWithConfigSkippingPaths(t *testing.T) {
|
|||||||
|
|
||||||
func TestDisableConsoleColor(t *testing.T) {
|
func TestDisableConsoleColor(t *testing.T) {
|
||||||
New()
|
New()
|
||||||
assert.False(t, disableColor)
|
assert.Equal(t, autoColor, consoleColorMode)
|
||||||
DisableConsoleColor()
|
DisableConsoleColor()
|
||||||
assert.True(t, disableColor)
|
assert.Equal(t, disableColor, consoleColorMode)
|
||||||
|
|
||||||
|
// reset console color mode.
|
||||||
|
consoleColorMode = autoColor
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestForceConsoleColor(t *testing.T) {
|
func TestForceConsoleColor(t *testing.T) {
|
||||||
New()
|
New()
|
||||||
assert.False(t, forceColor)
|
assert.Equal(t, autoColor, consoleColorMode)
|
||||||
ForceConsoleColor()
|
ForceConsoleColor()
|
||||||
assert.True(t, forceColor)
|
assert.Equal(t, forceColor, consoleColorMode)
|
||||||
|
|
||||||
|
// reset console color mode.
|
||||||
|
consoleColorMode = autoColor
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user