Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/message-formatter-from-event
Browse files Browse the repository at this point in the history
Rodrigo Martell committed Oct 23, 2024
1 parent 6abadab commit 89ec9a5
Showing 3 changed files with 45 additions and 12 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -352,6 +352,21 @@ output.FormatLevel = func(i interface{}) string {
output.FormatMessage = func(i interface{}) string {
return fmt.Sprintf("***%s****", i)
}
output.FormatMessageFromEvent = func(evt map[string]interface{}) zerolog.Formatter {
return func(i interface{}) string {
level := evt[zerolog.LevelFieldName]
switch level {
case zerolog.LevelInfoValue:
return fmt.Sprintf("=> %s", i, level)
case zerolog.LevelWarnValue:
return fmt.Sprintf("!! %s", i, level)
case zerolog.LevelErrorValue, zerolog.LevelFatalValue, zerolog.LevelPanicValue:
return fmt.Sprintf("XX %s", i, level)
default:
return fmt.Sprintf("%s", i)
}
}
}
output.FormatFieldName = func(i interface{}) string {
return fmt.Sprintf("%s:", i)
}
28 changes: 17 additions & 11 deletions console.go
Original file line number Diff line number Diff line change
@@ -47,6 +47,9 @@ const (
// Formatter transforms the input into a formatted string.
type Formatter func(interface{}) string

// FormatterFromEvent generates a Formatter from an event.
type FormatterFromEvent func(map[string]interface{}) Formatter

// ConsoleWriter parses the JSON input and writes it in an
// (optionally) colorized, human-friendly format to Out.
type ConsoleWriter struct {
@@ -77,14 +80,15 @@ type ConsoleWriter struct {
// FieldsExclude defines contextual fields to not display in output.
FieldsExclude []string

FormatTimestamp Formatter
FormatLevel Formatter
FormatCaller Formatter
FormatMessage Formatter
FormatFieldName Formatter
FormatFieldValue Formatter
FormatErrFieldName Formatter
FormatErrFieldValue Formatter
FormatTimestamp Formatter
FormatLevel Formatter
FormatCaller Formatter
FormatMessage Formatter
FormatMessageFromEvent FormatterFromEvent
FormatFieldName Formatter
FormatFieldValue Formatter
FormatErrFieldName Formatter
FormatErrFieldValue Formatter

FormatExtra func(map[string]interface{}, *bytes.Buffer) error

@@ -305,10 +309,12 @@ func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{},
f = w.FormatTimestamp
}
case MessageFieldName:
if w.FormatMessage == nil {
f = consoleDefaultFormatMessage(w.NoColor, evt[LevelFieldName])
} else {
if w.FormatMessageFromEvent != nil {
f = w.FormatMessageFromEvent(evt)
} else if w.FormatMessage != nil {
f = w.FormatMessage
} else {
f = consoleDefaultFormatMessage(w.NoColor, evt[LevelFieldName])
}
case CallerFieldName:
if w.FormatCaller == nil {
14 changes: 13 additions & 1 deletion console_test.go
Original file line number Diff line number Diff line change
@@ -24,10 +24,22 @@ func ExampleConsoleWriter_customFormatters() {
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.FormatFieldValue = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%s", i)) }
out.FormatMessageFromEvent = func(evt map[string]interface{}) zerolog.Formatter {
return func(i interface{}) string {
level := evt[zerolog.LevelFieldName]
switch level {
case zerolog.LevelInfoValue, zerolog.LevelWarnValue, zerolog.LevelErrorValue, zerolog.LevelFatalValue, zerolog.LevelPanicValue:
return fmt.Sprintf("%s (%q formatted message)", i, level)
default:
return fmt.Sprintf("%s", i)
}
}
}

log := zerolog.New(out)

log.Info().Str("foo", "bar").Msg("Hello World")
// Output: <nil> INFO | Hello World foo:BAR
// Output: <nil> INFO | Hello World ("info" formatted message) foo:BAR
}

func ExampleNewConsoleWriter() {

0 comments on commit 89ec9a5

Please sign in to comment.