Skip to content

Commit b40d4c1

Browse files
sairoutinethinkerou
authored andcommitted
IsTerm flag should not be affected by DisableConsoleColor method. (#1802)
* IsTerm flag should not be affected by DisableConsoleColor method. * change public property to private
1 parent c16bfa7 commit b40d4c1

File tree

2 files changed

+75
-25
lines changed

2 files changed

+75
-25
lines changed

logger.go

+30-19
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,24 @@ import (
1414
"github.com/mattn/go-isatty"
1515
)
1616

17+
type consoleColorModeValue int
18+
19+
const (
20+
autoColor consoleColorModeValue = iota
21+
disableColor
22+
forceColor
23+
)
24+
1725
var (
18-
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
19-
white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
20-
yellow = string([]byte{27, 91, 57, 48, 59, 52, 51, 109})
21-
red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
22-
blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
23-
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
24-
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
25-
reset = string([]byte{27, 91, 48, 109})
26-
disableColor = false
27-
forceColor = false
26+
green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
27+
white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
28+
yellow = string([]byte{27, 91, 57, 48, 59, 52, 51, 109})
29+
red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
30+
blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
31+
magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
32+
cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
33+
reset = string([]byte{27, 91, 48, 109})
34+
consoleColorMode = autoColor
2835
)
2936

3037
// LoggerConfig defines the config for Logger middleware.
@@ -62,8 +69,8 @@ type LogFormatterParams struct {
6269
Path string
6370
// ErrorMessage is set if error has occurred in processing the request.
6471
ErrorMessage string
65-
// IsTerm shows whether does gin's output descriptor refers to a terminal.
66-
IsTerm bool
72+
// isTerm shows whether does gin's output descriptor refers to a terminal.
73+
isTerm bool
6774
// BodySize is the size of the Response Body
6875
BodySize int
6976
// Keys are the keys set on the request's context.
@@ -115,10 +122,15 @@ func (p *LogFormatterParams) ResetColor() string {
115122
return reset
116123
}
117124

125+
// IsOutputColor indicates whether can colors be outputted to the log.
126+
func (p *LogFormatterParams) IsOutputColor() bool {
127+
return consoleColorMode == forceColor || (consoleColorMode == autoColor && p.isTerm)
128+
}
129+
118130
// defaultLogFormatter is the default log format function Logger middleware uses.
119131
var defaultLogFormatter = func(param LogFormatterParams) string {
120132
var statusColor, methodColor, resetColor string
121-
if param.IsTerm {
133+
if param.IsOutputColor() {
122134
statusColor = param.StatusCodeColor()
123135
methodColor = param.MethodColor()
124136
resetColor = param.ResetColor()
@@ -137,12 +149,12 @@ var defaultLogFormatter = func(param LogFormatterParams) string {
137149

138150
// DisableConsoleColor disables color output in the console.
139151
func DisableConsoleColor() {
140-
disableColor = true
152+
consoleColorMode = disableColor
141153
}
142154

143155
// ForceConsoleColor force color output in the console.
144156
func ForceConsoleColor() {
145-
forceColor = true
157+
consoleColorMode = forceColor
146158
}
147159

148160
// ErrorLogger returns a handlerfunc for any error type.
@@ -199,9 +211,8 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
199211

200212
isTerm := true
201213

202-
if w, ok := out.(*os.File); (!ok ||
203-
(os.Getenv("TERM") == "dumb" || (!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd()))) ||
204-
disableColor) && !forceColor {
214+
if w, ok := out.(*os.File); !ok || os.Getenv("TERM") == "dumb" ||
215+
(!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd())) {
205216
isTerm = false
206217
}
207218

@@ -228,7 +239,7 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
228239
if _, ok := skip[path]; !ok {
229240
param := LogFormatterParams{
230241
Request: c.Request,
231-
IsTerm: isTerm,
242+
isTerm: isTerm,
232243
Keys: c.Keys,
233244
}
234245

logger_test.go

+45-6
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func TestDefaultLogFormatter(t *testing.T) {
240240
Method: "GET",
241241
Path: "/",
242242
ErrorMessage: "",
243-
IsTerm: false,
243+
isTerm: false,
244244
}
245245

246246
termTrueParam := LogFormatterParams{
@@ -251,7 +251,7 @@ func TestDefaultLogFormatter(t *testing.T) {
251251
Method: "GET",
252252
Path: "/",
253253
ErrorMessage: "",
254-
IsTerm: true,
254+
isTerm: true,
255255
}
256256

257257
assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 5s | 20.20.20.20 | GET /\n", defaultLogFormatter(termFalseParam))
@@ -296,6 +296,39 @@ func TestResetColor(t *testing.T) {
296296
assert.Equal(t, string([]byte{27, 91, 48, 109}), p.ResetColor())
297297
}
298298

299+
func TestIsOutputColor(t *testing.T) {
300+
// test with isTerm flag true.
301+
p := LogFormatterParams{
302+
isTerm: true,
303+
}
304+
305+
consoleColorMode = autoColor
306+
assert.Equal(t, true, p.IsOutputColor())
307+
308+
ForceConsoleColor()
309+
assert.Equal(t, true, p.IsOutputColor())
310+
311+
DisableConsoleColor()
312+
assert.Equal(t, false, p.IsOutputColor())
313+
314+
// test with isTerm flag false.
315+
p = LogFormatterParams{
316+
isTerm: false,
317+
}
318+
319+
consoleColorMode = autoColor
320+
assert.Equal(t, false, p.IsOutputColor())
321+
322+
ForceConsoleColor()
323+
assert.Equal(t, true, p.IsOutputColor())
324+
325+
DisableConsoleColor()
326+
assert.Equal(t, false, p.IsOutputColor())
327+
328+
// reset console color mode.
329+
consoleColorMode = autoColor
330+
}
331+
299332
func TestErrorLogger(t *testing.T) {
300333
router := New()
301334
router.Use(ErrorLogger())
@@ -358,14 +391,20 @@ func TestLoggerWithConfigSkippingPaths(t *testing.T) {
358391

359392
func TestDisableConsoleColor(t *testing.T) {
360393
New()
361-
assert.False(t, disableColor)
394+
assert.Equal(t, autoColor, consoleColorMode)
362395
DisableConsoleColor()
363-
assert.True(t, disableColor)
396+
assert.Equal(t, disableColor, consoleColorMode)
397+
398+
// reset console color mode.
399+
consoleColorMode = autoColor
364400
}
365401

366402
func TestForceConsoleColor(t *testing.T) {
367403
New()
368-
assert.False(t, forceColor)
404+
assert.Equal(t, autoColor, consoleColorMode)
369405
ForceConsoleColor()
370-
assert.True(t, forceColor)
406+
assert.Equal(t, forceColor, consoleColorMode)
407+
408+
// reset console color mode.
409+
consoleColorMode = autoColor
371410
}

0 commit comments

Comments
 (0)