mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-24 03:46:37 +02:00
21557b38f6
* add log context & valuer
31 lines
581 B
Go
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)
|
|
}
|
|
}
|