From d6f6f5334f9f01b1a63bde594a8674f71b5b7e31 Mon Sep 17 00:00:00 2001 From: Simon Sawert Date: Sat, 26 Nov 2022 09:25:38 +0100 Subject: [PATCH] Add example for `WithFormatter` This will also hopefully explain the change in `FormatFunc` to return an `interface{}` and in general how to use `WithFormatter`. --- example/main.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/example/main.go b/example/main.go index 4ecda64..8e98466 100644 --- a/example/main.go +++ b/example/main.go @@ -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)) }