1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-24 03:46:37 +02:00
kratos/log/value.go
Tony Chen 21557b38f6
log: add log context (#766)
* add log context & valuer
2021-03-14 15:36:00 +08:00

31 lines
581 B
Go

package log
import (
"runtime"
"strconv"
"strings"
)
// Valuer is returns a log value.
type Valuer func() interface{}
// Value return the function value.
func Value(v interface{}) interface{} {
if v, ok := v.(Valuer); ok {
return v()
}
return v
}
// Caller returns returns a Valuer that returns a pkg/file:line description of the caller.
func Caller(depth int) Valuer {
return func() interface{} {
_, file, line, ok := runtime.Caller(depth)
if !ok {
return nil
}
idx := strings.LastIndexByte(file, '/')
return file[idx+1:] + ":" + strconv.Itoa(line)
}
}