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
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")
|
|
}
|