Skip to content

Commit

Permalink
Add example for WithFormatter
Browse files Browse the repository at this point in the history
This will also hopefully explain the change in `FormatFunc` to return an
`interface{}` and in general how to use `WithFormatter`.
  • Loading branch information
bombsimon committed Nov 26, 2022
1 parent 352a8db commit d6f6f53
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,24 @@ func main() {
log.V(2).Info("you should see this as trace")
log.V(1).V(1).Info("you should see this as trace")
log.V(10).Info("you should not see this")

fmt.Println("")

type myInt int

logrusLog = logrus.New()
logrusLog.SetFormatter(&logrus.JSONFormatter{})

log = logrusr.New(
logrusLog,
logrusr.WithFormatter(func(in interface{}) interface{} {
if v, ok := in.(myInt); ok && v == 1 {
return "1"
}

return in
}),
)

log.Info("custom types", "my_int", myInt(1), "my_other_int", myInt(2))
}

0 comments on commit d6f6f53

Please sign in to comment.