@@ -14,17 +14,24 @@ import (
14
14
"github.com/mattn/go-isatty"
15
15
)
16
16
17
+ type consoleColorModeValue int
18
+
19
+ const (
20
+ autoColor consoleColorModeValue = iota
21
+ disableColor
22
+ forceColor
23
+ )
24
+
17
25
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
28
35
)
29
36
30
37
// LoggerConfig defines the config for Logger middleware.
@@ -62,8 +69,8 @@ type LogFormatterParams struct {
62
69
Path string
63
70
// ErrorMessage is set if error has occurred in processing the request.
64
71
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
67
74
// BodySize is the size of the Response Body
68
75
BodySize int
69
76
// Keys are the keys set on the request's context.
@@ -115,10 +122,15 @@ func (p *LogFormatterParams) ResetColor() string {
115
122
return reset
116
123
}
117
124
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
+
118
130
// defaultLogFormatter is the default log format function Logger middleware uses.
119
131
var defaultLogFormatter = func (param LogFormatterParams ) string {
120
132
var statusColor , methodColor , resetColor string
121
- if param .IsTerm {
133
+ if param .IsOutputColor () {
122
134
statusColor = param .StatusCodeColor ()
123
135
methodColor = param .MethodColor ()
124
136
resetColor = param .ResetColor ()
@@ -137,12 +149,12 @@ var defaultLogFormatter = func(param LogFormatterParams) string {
137
149
138
150
// DisableConsoleColor disables color output in the console.
139
151
func DisableConsoleColor () {
140
- disableColor = true
152
+ consoleColorMode = disableColor
141
153
}
142
154
143
155
// ForceConsoleColor force color output in the console.
144
156
func ForceConsoleColor () {
145
- forceColor = true
157
+ consoleColorMode = forceColor
146
158
}
147
159
148
160
// ErrorLogger returns a handlerfunc for any error type.
@@ -199,9 +211,8 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
199
211
200
212
isTerm := true
201
213
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 ())) {
205
216
isTerm = false
206
217
}
207
218
@@ -228,7 +239,7 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
228
239
if _ , ok := skip [path ]; ! ok {
229
240
param := LogFormatterParams {
230
241
Request : c .Request ,
231
- IsTerm : isTerm ,
242
+ isTerm : isTerm ,
232
243
Keys : c .Keys ,
233
244
}
234
245
0 commit comments