| 1 |
// Copyright (c) 2023 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package exzerolog |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"runtime" |
| 12 |
"strings" |
| 13 |
) |
| 14 |
|
| 15 |
// CallerWithFunctionName is an implementation for zerolog.CallerMarshalFunc that includes the caller function name |
| 16 |
// in addition to the file and line number. |
| 17 |
// |
| 18 |
// Use as |
| 19 |
// |
| 20 |
// zerolog.CallerMarshalFunc = exzerolog.CallerWithFunctionName |
| 21 |
func CallerWithFunctionName(pc uintptr, file string, line int) string { |
| 22 |
files := strings.Split(file, "/") |
| 23 |
file = files[len(files)-1] |
| 24 |
name := runtime.FuncForPC(pc).Name() |
| 25 |
fns := strings.Split(name, ".") |
| 26 |
name = fns[len(fns)-1] |
| 27 |
return fmt.Sprintf("%s:%d:%s()", file, line, name) |
| 28 |
} |