@@ -47,6 +47,10 @@ const (
47
47
// Formatter transforms the input into a formatted string.
48
48
type Formatter func (interface {}) string
49
49
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
+
50
54
// ConsoleWriter parses the JSON input and writes it in an
51
55
// (optionally) colorized, human-friendly format to Out.
52
56
type ConsoleWriter struct {
@@ -85,6 +89,9 @@ type ConsoleWriter struct {
85
89
FormatFieldValue Formatter
86
90
FormatErrFieldName Formatter
87
91
FormatErrFieldValue Formatter
92
+ // If this is configured it is used for "part" values and
93
+ // has precedence on FormatFieldValue
94
+ FormatPartValueByName FormatterByFieldName
88
95
89
96
FormatExtra func (map [string ]interface {}, * bytes.Buffer ) error
90
97
@@ -282,6 +289,7 @@ func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer
282
289
// writePart appends a formatted part to buf.
283
290
func (w ConsoleWriter ) writePart (buf * bytes.Buffer , evt map [string ]interface {}, p string ) {
284
291
var f Formatter
292
+ var fvn FormatterByFieldName
285
293
286
294
if len (w .PartsExclude ) > 0 {
287
295
for _ , exclude := range w .PartsExclude {
@@ -317,14 +325,21 @@ func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{},
317
325
f = w .FormatCaller
318
326
}
319
327
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 {
323
331
f = w .FormatFieldValue
332
+ } else {
333
+ f = consoleDefaultFormatFieldValue
324
334
}
325
335
}
326
336
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
+ }
328
343
329
344
if len (s ) > 0 {
330
345
if buf .Len () > 0 {
0 commit comments