基于 `zap` 包封装。除了实现 `Go` 日志包的基本功能外,还实现了很多高级功能 本包基于`github.com/tkestack/tke/pkg/util/log`裁剪
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
741 B

package log
import (
"context"
)
type key int
const (
logContextKey key = iota
)
// WithContext 把命令行输出logger保存到context中.可以使用FromContext(ctx)获取绑定的logger
func WithContext(ctx context.Context) context.Context {
return std.WithContext(ctx)
}
// WithContext 把指定的logger保存到context中.可以使用FromContext(ctx)获取绑定的logger
func (l *zapLogger) WithContext(ctx context.Context) context.Context {
return context.WithValue(ctx, logContextKey, l)
}
// FromContext 从ctx中获取logger
func FromContext(ctx context.Context) Logger {
if ctx != nil {
logger := ctx.Value(logContextKey)
if logger != nil {
return logger.(Logger)
}
}
return WithName("Unknown-Context")
}