@@ -76,6 +76,8 @@ type ConsoleWriter struct {
7676 FormatErrFieldValue Formatter
7777
7878 FormatExtra func (map [string ]interface {}, * bytes.Buffer ) error
79+
80+ FormatPrepare func (map [string ]interface {}) error
7981}
8082
8183// NewConsoleWriter creates and initializes a new ConsoleWriter.
@@ -124,6 +126,13 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) {
124126 return n , fmt .Errorf ("cannot decode event: %s" , err )
125127 }
126128
129+ if w .FormatPrepare != nil {
130+ err = w .FormatPrepare (evt )
131+ if err != nil {
132+ return n , err
133+ }
134+ }
135+
127136 for _ , p := range w .PartsOrder {
128137 w .writePart (buf , evt , p )
129138 }
@@ -272,7 +281,7 @@ func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{},
272281 }
273282 case MessageFieldName :
274283 if w .FormatMessage == nil {
275- f = consoleDefaultFormatMessage
284+ f = consoleDefaultFormatMessage ( w . NoColor , evt [ LevelFieldName ])
276285 } else {
277286 f = w .FormatMessage
278287 }
@@ -310,8 +319,13 @@ func needsQuote(s string) bool {
310319 return false
311320}
312321
313- // colorize returns the string s wrapped in ANSI code c, unless disabled is true.
322+ // colorize returns the string s wrapped in ANSI code c, unless disabled is true or c is 0 .
314323func colorize (s interface {}, c int , disabled bool ) string {
324+ e := os .Getenv ("NO_COLOR" )
325+ if e != "" || c == 0 {
326+ disabled = true
327+ }
328+
315329 if disabled {
316330 return fmt .Sprintf ("%s" , s )
317331 }
@@ -373,27 +387,16 @@ func consoleDefaultFormatLevel(noColor bool) Formatter {
373387 return func (i interface {}) string {
374388 var l string
375389 if ll , ok := i .(string ); ok {
376- switch ll {
377- case LevelTraceValue :
378- l = colorize ("TRC" , colorMagenta , noColor )
379- case LevelDebugValue :
380- l = colorize ("DBG" , colorYellow , noColor )
381- case LevelInfoValue :
382- l = colorize ("INF" , colorGreen , noColor )
383- case LevelWarnValue :
384- l = colorize ("WRN" , colorRed , noColor )
385- case LevelErrorValue :
386- l = colorize (colorize ("ERR" , colorRed , noColor ), colorBold , noColor )
387- case LevelFatalValue :
388- l = colorize (colorize ("FTL" , colorRed , noColor ), colorBold , noColor )
389- case LevelPanicValue :
390- l = colorize (colorize ("PNC" , colorRed , noColor ), colorBold , noColor )
391- default :
392- l = colorize (ll , colorBold , noColor )
390+ level , _ := ParseLevel (ll )
391+ fl , ok := FormattedLevels [level ]
392+ if ok {
393+ l = colorize (fl , LevelColors [level ], noColor )
394+ } else {
395+ l = strings .ToUpper (ll )[0 :3 ]
393396 }
394397 } else {
395398 if i == nil {
396- l = colorize ( "???" , colorBold , noColor )
399+ l = "???"
397400 } else {
398401 l = strings .ToUpper (fmt .Sprintf ("%s" , i ))[0 :3 ]
399402 }
@@ -420,11 +423,18 @@ func consoleDefaultFormatCaller(noColor bool) Formatter {
420423 }
421424}
422425
423- func consoleDefaultFormatMessage (i interface {}) string {
424- if i == nil {
425- return ""
426+ func consoleDefaultFormatMessage (noColor bool , level interface {}) Formatter {
427+ return func (i interface {}) string {
428+ if i == nil || i == "" {
429+ return ""
430+ }
431+ switch level {
432+ case LevelInfoValue , LevelWarnValue , LevelErrorValue , LevelFatalValue , LevelPanicValue :
433+ return colorize (fmt .Sprintf ("%s" , i ), colorBold , noColor )
434+ default :
435+ return fmt .Sprintf ("%s" , i )
436+ }
426437 }
427- return fmt .Sprintf ("%s" , i )
428438}
429439
430440func consoleDefaultFormatFieldName (noColor bool ) Formatter {
@@ -445,6 +455,6 @@ func consoleDefaultFormatErrFieldName(noColor bool) Formatter {
445455
446456func consoleDefaultFormatErrFieldValue (noColor bool ) Formatter {
447457 return func (i interface {}) string {
448- return colorize (fmt .Sprintf ("%s" , i ), colorRed , noColor )
458+ return colorize (colorize ( fmt .Sprintf ("%s" , i ), colorBold , noColor ), colorRed , noColor )
449459 }
450460}
0 commit comments