mediocre-go-lib/mlog/ctx.go

59 lines
1.5 KiB
Go
Raw Normal View History

2018-11-30 23:50:23 +00:00
package mlog
import (
"context"
)
2018-11-30 23:50:23 +00:00
type ctxKey int
// WithLogger returns the Context with the Logger carried by it.
func WithLogger(ctx context.Context, l *Logger) context.Context {
return context.WithValue(ctx, ctxKey(0), l)
2018-11-30 23:50:23 +00:00
}
// DefaultLogger is an instance of Logger which is returned by From when a
// Logger hasn't been previously WithLogger on the Contexts passed in.
var DefaultLogger = NewLogger()
// From looks at each context and returns the Logger from the first Context
// which carries one via a WithLogger call. If none carry a Logger than
// DefaultLogger is returned.
func From(ctxs ...context.Context) *Logger {
for _, ctx := range ctxs {
if l, _ := ctx.Value(ctxKey(0)).(*Logger); l != nil {
return l
}
}
return DefaultLogger
}
// Debug is a shortcut for
// mlog.From(ctxs...).Debug(desc, ctxs...)
func Debug(descr string, ctxs ...context.Context) {
From(ctxs...).Debug(descr, ctxs...)
2018-11-30 23:50:23 +00:00
}
// Info is a shortcut for
// mlog.From(ctxs...).Info(desc, ctxs...)
func Info(descr string, ctxs ...context.Context) {
From(ctxs...).Info(descr, ctxs...)
2018-11-30 23:50:23 +00:00
}
// Warn is a shortcut for
// mlog.From(ctxs...).Warn(desc, ctxs...)
func Warn(descr string, ctxs ...context.Context) {
From(ctxs...).Warn(descr, ctxs...)
}
2018-11-30 23:50:23 +00:00
// Error is a shortcut for
// mlog.From(ctxs...).Error(desc, ctxs...)
func Error(descr string, ctxs ...context.Context) {
From(ctxs...).Error(descr, ctxs...)
}
2018-11-30 23:50:23 +00:00
// Fatal is a shortcut for
// mlog.From(ctxs...).Fatal(desc, ctxs...)
func Fatal(descr string, ctxs ...context.Context) {
From(ctxs...).Fatal(descr, ctxs...)
2018-11-30 23:50:23 +00:00
}