Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow message formatting using event data #690

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %s", level, i)
case zerolog.LevelWarnValue:
return fmt.Sprintf("!! %s %s", level, i)
case zerolog.LevelErrorValue, zerolog.LevelFatalValue, zerolog.LevelPanicValue:
return fmt.Sprintf("XX %s %s", level, i)
default:
return fmt.Sprintf("%s", i)
}
}
}
output.FormatFieldName = func(i interface{}) string {
return fmt.Sprintf("%s:", i)
}
Expand Down
28 changes: 17 additions & 11 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 13 additions & 1 deletion console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down