@@ -47,6 +47,10 @@ const (
4747// Formatter transforms the input into a formatted string.
4848type Formatter func (interface {}) string
4949
50+ // FormatterByFieldName transforms the input into a formatted string,
51+ // being able to differentiate formatting based on field name.
52+ type FormatterByFieldName func (interface {}, string ) string
53+
5054// ConsoleWriter parses the JSON input and writes it in an
5155// (optionally) colorized, human-friendly format to Out.
5256type ConsoleWriter struct {
@@ -85,6 +89,9 @@ type ConsoleWriter struct {
8589 FormatFieldValue Formatter
8690 FormatErrFieldName Formatter
8791 FormatErrFieldValue Formatter
92+ // If this is configured it is used for "part" values and
93+ // has precedence on FormatFieldValue
94+ FormatPartValueByName FormatterByFieldName
8895
8996 FormatExtra func (map [string ]interface {}, * bytes.Buffer ) error
9097
@@ -282,6 +289,7 @@ func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer
282289// writePart appends a formatted part to buf.
283290func (w ConsoleWriter ) writePart (buf * bytes.Buffer , evt map [string ]interface {}, p string ) {
284291 var f Formatter
292+ var fvn FormatterByFieldName
285293
286294 if len (w .PartsExclude ) > 0 {
287295 for _ , exclude := range w .PartsExclude {
@@ -317,14 +325,21 @@ func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{},
317325 f = w .FormatCaller
318326 }
319327 default :
320- if w .FormatFieldValue = = nil {
321- f = consoleDefaultFormatFieldValue
322- } else {
328+ if w .FormatPartValueByName ! = nil {
329+ fvn = w . FormatPartValueByName
330+ } else if w . FormatFieldValue != nil {
323331 f = w .FormatFieldValue
332+ } else {
333+ f = consoleDefaultFormatFieldValue
324334 }
325335 }
326336
327- var s = f (evt [p ])
337+ var s string
338+ if f == nil {
339+ s = fvn (evt [p ], p )
340+ } else {
341+ s = f (evt [p ])
342+ }
328343
329344 if len (s ) > 0 {
330345 if buf .Len () > 0 {
0 commit comments