diff --git a/README.md b/README.md index e8cbfc28..7ee0bc54 100644 --- a/README.md +++ b/README.md @@ -366,6 +366,37 @@ log.Info().Str("foo", "bar").Msg("Hello World") // Output: 2006-01-02T15:04:05Z07:00 | INFO | ***Hello World**** foo:BAR ``` +To use custom advanced formatting: + +```go +output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true, + PartsOrder: []string{"level", "one", "two", "three", "message"}, + FieldsExclude: []string{"one", "two", "three"}} +output.FormatLevel = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%-6s", i)) } +output.FormatFieldName = func(i interface{}) string { return fmt.Sprintf("%s:", i) } +output.FormatPartValueByName = func(i interface{}, s string) string { + var ret string + switch s { + case "one": + ret = strings.ToUpper(fmt.Sprintf("%s", i)) + case "two": + ret = strings.ToLower(fmt.Sprintf("%s", i)) + case "three": + ret = strings.ToLower(fmt.Sprintf("(%s)", i)) + } + return ret +} +log := zerolog.New(output) + +log.Info().Str("foo", "bar"). + Str("two", "TEST_TWO"). + Str("one", "test_one"). + Str("three", "test_three"). + Msg("Hello World") + +// Output: INFO TEST_ONE test_two (test_three) Hello World foo:bar +``` + ### Sub dictionary ```go diff --git a/console.go b/console.go index 8b0e0c61..c308a3cc 100644 --- a/console.go +++ b/console.go @@ -45,6 +45,10 @@ const ( // Formatter transforms the input into a formatted string. type Formatter func(interface{}) string +// FormatterByFieldName transforms the input into a formatted string, +// being able to differentiate formatting based on field name. +type FormatterByFieldName func(interface{}, string) string + // ConsoleWriter parses the JSON input and writes it in an // (optionally) colorized, human-friendly format to Out. type ConsoleWriter struct { @@ -74,6 +78,9 @@ type ConsoleWriter struct { FormatFieldValue Formatter FormatErrFieldName Formatter FormatErrFieldValue Formatter + // If this is configured it is used for "part" values and + // has precedence on FormatFieldValue + FormatPartValueByName FormatterByFieldName FormatExtra func(map[string]interface{}, *bytes.Buffer) error } @@ -248,6 +255,7 @@ func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer // writePart appends a formatted part to buf. func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{}, p string) { var f Formatter + var fvn FormatterByFieldName if w.PartsExclude != nil && len(w.PartsExclude) > 0 { for _, exclude := range w.PartsExclude { @@ -283,14 +291,21 @@ func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{}, f = w.FormatCaller } default: - if w.FormatFieldValue == nil { - f = consoleDefaultFormatFieldValue - } else { + if w.FormatPartValueByName != nil { + fvn = w.FormatPartValueByName + } else if w.FormatFieldValue != nil { f = w.FormatFieldValue + } else { + f = consoleDefaultFormatFieldValue } } - var s = f(evt[p]) + var s string + if f == nil { + s = fvn(evt[p], p) + } else { + s = f(evt[p]) + } if len(s) > 0 { if buf.Len() > 0 { diff --git a/console_test.go b/console_test.go index 18c2db70..ff75d2ef 100644 --- a/console_test.go +++ b/console_test.go @@ -30,6 +30,34 @@ func ExampleConsoleWriter_customFormatters() { // Output: INFO | Hello World foo:BAR } +func ExampleConsoleWriter_partValueFormatter() { + out := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true, + PartsOrder: []string{"level", "one", "two", "three", "message"}, + FieldsExclude: []string{"one", "two", "three"}} + out.FormatLevel = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%-6s", i)) } + out.FormatFieldName = func(i interface{}) string { return fmt.Sprintf("%s:", i) } + out.FormatPartValueByName = func(i interface{}, s string) string { + var ret string + switch s { + case "one": + ret = strings.ToUpper(fmt.Sprintf("%s", i)) + case "two": + ret = strings.ToLower(fmt.Sprintf("%s", i)) + case "three": + ret = strings.ToLower(fmt.Sprintf("(%s)", i)) + } + return ret + } + log := zerolog.New(out) + + log.Info().Str("foo", "bar"). + Str("two", "TEST_TWO"). + Str("one", "test_one"). + Str("three", "test_three"). + Msg("Hello World") + // Output: INFO TEST_ONE test_two (test_three) Hello World foo:bar +} + func ExampleNewConsoleWriter() { out := zerolog.NewConsoleWriter() out.NoColor = true // For testing purposes only