Add some comments to pkg/loggerctx
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
This commit is contained in:
parent
d86776feb1
commit
177960431c
|
@ -6,12 +6,16 @@ import (
|
||||||
"go.elara.ws/logger"
|
"go.elara.ws/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// loggerCtxKey is used as the context key for loggers
|
||||||
type loggerCtxKey struct{}
|
type loggerCtxKey struct{}
|
||||||
|
|
||||||
|
// With returns a copy of ctx containing log
|
||||||
func With(ctx context.Context, log logger.Logger) context.Context {
|
func With(ctx context.Context, log logger.Logger) context.Context {
|
||||||
return context.WithValue(ctx, loggerCtxKey{}, log)
|
return context.WithValue(ctx, loggerCtxKey{}, log)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// From attempts to get a logger from ctx. If ctx doesn't
|
||||||
|
// contain a logger, it returns a nop logger.
|
||||||
func From(ctx context.Context) logger.Logger {
|
func From(ctx context.Context) logger.Logger {
|
||||||
if val := ctx.Value(loggerCtxKey{}); val != nil {
|
if val := ctx.Value(loggerCtxKey{}); val != nil {
|
||||||
if log, ok := val.(logger.Logger); ok && log != nil {
|
if log, ok := val.(logger.Logger); ok && log != nil {
|
||||||
|
|
Loading…
Reference in New Issue