1 Star 0 Fork 0

llakcs/agile-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
value.go 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
llakcs 提交于 2024-01-31 16:54 . 第一次提交
package log
import (
"context"
"runtime"
"strconv"
"strings"
"time"
)
var (
// DefaultCaller is a Valuer that returns the file and line.
DefaultCaller = Caller(4)
// DefaultTimestamp is a Valuer that returns the current wallclock time.
DefaultTimestamp = Timestamp(time.RFC3339)
)
// Valuer is returns a log value.
type Valuer func(ctx context.Context) interface{}
// Value return the function value.
func Value(ctx context.Context, v interface{}) interface{} {
if v, ok := v.(Valuer); ok {
return v(ctx)
}
return v
}
// Caller returns a Valuer that returns a pkg/file:line description of the caller.
func Caller(depth int) Valuer {
return func(context.Context) interface{} {
_, file, line, _ := runtime.Caller(depth)
idx := strings.LastIndexByte(file, '/')
if idx == -1 {
return file[idx+1:] + ":" + strconv.Itoa(line)
}
idx = strings.LastIndexByte(file[:idx], '/')
return file[idx+1:] + ":" + strconv.Itoa(line)
}
}
// Timestamp returns a timestamp Valuer with a custom time format.
func Timestamp(layout string) Valuer {
return func(context.Context) interface{} {
return time.Now().Format(layout)
}
}
func bindValues(ctx context.Context, keyvals []interface{}) {
for i := 1; i < len(keyvals); i += 2 {
if v, ok := keyvals[i].(Valuer); ok {
keyvals[i] = v(ctx)
}
}
}
func containsValuer(keyvals []interface{}) bool {
for i := 1; i < len(keyvals); i += 2 {
if _, ok := keyvals[i].(Valuer); ok {
return true
}
}
return false
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/llakcs/agile-go.git
git@gitee.com:llakcs/agile-go.git
llakcs
agile-go
agile-go
v1.2.0

搜索帮助

D67c1975 1850385 1daf7b77 1850385