From e9344a8c507b5f25a4962ff022526be0ddab8e72 Mon Sep 17 00:00:00 2001 From: Igor Beliakov <46579601+weisdd@users.noreply.github.com> Date: Sat, 12 Mar 2022 17:33:09 +0100 Subject: [PATCH] docs: add an example for Lshortfile-like implementation of CallerMarshalFunc (#423) Signed-off-by: Igor Beliakov --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 06e6a6d..c22ee2e 100644 --- a/README.md +++ b/README.md @@ -399,6 +399,8 @@ log.Logger = log.With().Str("foo", "bar").Logger() ### Add file and line number to log +Equivalent of `Llongfile`: + ```go log.Logger = log.With().Caller().Logger() log.Info().Msg("hello world") @@ -406,6 +408,25 @@ log.Info().Msg("hello world") // Output: {"level": "info", "message": "hello world", "caller": "/go/src/your_project/some_file:21"} ``` +Equivalent of `Lshortfile`: + +```go +zerolog.CallerMarshalFunc = func(file string, line int) string { + short := file + for i := len(file) - 1; i > 0; i-- { + if file[i] == '/' { + short = file[i+1:] + break + } + } + file = short + return file + ":" + strconv.Itoa(line) +} +log.Logger = log.With().Caller().Logger() +log.Info().Msg("hello world") + +// Output: {"level": "info", "message": "hello world", "caller": "some_file:21"} +``` ### Thread-safe, lock-free, non-blocking writer @@ -560,7 +581,7 @@ func main() { // Output (Line 1: Console; Line 2: Stdout) // 12:36PM INF Hello World! // {"level":"info","time":"2019-11-07T12:36:38+03:00","message":"Hello World!"} -``` +``` ## Global Settings