File tree 2 files changed +49
-3
lines changed
2 files changed +49
-3
lines changed Original file line number Diff line number Diff line change @@ -55,11 +55,25 @@ var (
55
55
56
56
faintBoldColor = color .New (color .Faint , color .Bold )
57
57
faintColor = color .New (color .Faint )
58
- faintMultiLinePrefix = faintColor . Sprint ( " | " )
59
- faintFieldSeparator = faintColor . Sprint ( "=" )
60
- faintFieldSeparatorWithNewLine = faintColor . Sprint ( "= \n " )
58
+ faintMultiLinePrefix string
59
+ faintFieldSeparator string
60
+ faintFieldSeparatorWithNewLine string
61
61
)
62
62
63
+ func init () {
64
+ // Force all the colors to enabled because we do our own detection of color usage.
65
+ for _ , c := range _levelToColor {
66
+ c .EnableColor ()
67
+ }
68
+
69
+ faintBoldColor .EnableColor ()
70
+ faintColor .EnableColor ()
71
+
72
+ faintMultiLinePrefix = faintColor .Sprint (" | " )
73
+ faintFieldSeparator = faintColor .Sprint ("=" )
74
+ faintFieldSeparatorWithNewLine = faintColor .Sprint ("=\n " )
75
+ }
76
+
63
77
// Make sure that intLogger is a Logger
64
78
var _ Logger = & intLogger {}
65
79
Original file line number Diff line number Diff line change @@ -257,6 +257,38 @@ func TestLogger(t *testing.T) {
257
257
assert .Equal (t , "[INFO] sublogger: this is test\n " , rest )
258
258
})
259
259
260
+ t .Run ("can force colors to on in any context" , func (t * testing.T ) {
261
+ if runtime .GOOS == "windows" {
262
+ t .Skip ("colors are different on windows" )
263
+ }
264
+
265
+ var buf bytes.Buffer
266
+
267
+ logger := New (& LoggerOptions {
268
+ // No name!
269
+ Output : & buf ,
270
+ Level : Trace ,
271
+ Color : ForceColor ,
272
+ TimeFormat : "<time>" ,
273
+ })
274
+
275
+ logger .Trace ("trace" )
276
+ logger .Debug ("debug" )
277
+ logger .Info ("info" )
278
+ logger .Warn ("warn" )
279
+ logger .Error ("error" )
280
+ str := buf .String ()
281
+
282
+ assert .Equal (t , "" +
283
+ "\033 [92m<time> [TRACE] trace\n \033 [0m" +
284
+ "\033 [97m<time> [DEBUG] debug\n \033 [0m" +
285
+ "\033 [94m<time> [INFO] info\n \033 [0m" +
286
+ "\033 [93m<time> [WARN] warn\n \033 [0m" +
287
+ "\033 [91m<time> [ERROR] error\n \033 [0m" ,
288
+ str ,
289
+ )
290
+ })
291
+
260
292
t .Run ("use a different time format" , func (t * testing.T ) {
261
293
var buf bytes.Buffer
262
294
You can’t perform that action at this time.
0 commit comments