2018-11-30 23:50:23 +00:00
|
|
|
package mlog
|
|
|
|
|
2019-02-05 20:18:17 +00:00
|
|
|
import "context"
|
2018-11-30 23:50:23 +00:00
|
|
|
|
|
|
|
type ctxKey int
|
|
|
|
|
2019-02-05 20:18:17 +00:00
|
|
|
// Set returns the Context with the Logger carried by it.
|
|
|
|
func Set(ctx context.Context, l *Logger) context.Context {
|
|
|
|
return context.WithValue(ctx, ctxKey(0), l)
|
2018-11-30 23:50:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 20:18:17 +00:00
|
|
|
// DefaultLogger is an instance of Logger which is returned by From when a
|
|
|
|
// Logger hasn't been previously Set on the Context passed in.
|
|
|
|
var DefaultLogger = NewLogger()
|
|
|
|
|
|
|
|
// From returns the Logger carried by this Context, or DefaultLogger if none is
|
|
|
|
// being carried.
|
|
|
|
func From(ctx context.Context) *Logger {
|
|
|
|
if l, _ := ctx.Value(ctxKey(0)).(*Logger); l != nil {
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
return DefaultLogger
|
2019-01-30 21:06:24 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 20:18:17 +00:00
|
|
|
// Debug is a shortcut for
|
|
|
|
// mlog.From(ctx).Debug(ctx, descr, kvs...)
|
|
|
|
func Debug(ctx context.Context, descr string, kvs ...KVer) {
|
|
|
|
From(ctx).Debug(ctx, descr, kvs...)
|
2018-11-30 23:50:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 20:18:17 +00:00
|
|
|
// Info is a shortcut for
|
|
|
|
// mlog.From(ctx).Info(ctx, descr, kvs...)
|
|
|
|
func Info(ctx context.Context, descr string, kvs ...KVer) {
|
|
|
|
From(ctx).Info(ctx, descr, kvs...)
|
2018-11-30 23:50:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-05 20:18:17 +00:00
|
|
|
// Warn is a shortcut for
|
|
|
|
// mlog.From(ctx).Warn(ctx, descr, kvs...)
|
|
|
|
func Warn(ctx context.Context, descr string, kvs ...KVer) {
|
|
|
|
From(ctx).Warn(ctx, descr, kvs...)
|
|
|
|
}
|
2018-11-30 23:50:23 +00:00
|
|
|
|
2019-02-05 20:18:17 +00:00
|
|
|
// Error is a shortcut for
|
|
|
|
// mlog.From(ctx).Error(ctx, descr, kvs...)
|
|
|
|
func Error(ctx context.Context, descr string, kvs ...KVer) {
|
|
|
|
From(ctx).Error(ctx, descr, kvs...)
|
|
|
|
}
|
2018-11-30 23:50:23 +00:00
|
|
|
|
2019-02-05 20:18:17 +00:00
|
|
|
// Fatal is a shortcut for
|
|
|
|
// mlog.From(ctx).Fatal(ctx, descr, kvs...)
|
|
|
|
func Fatal(ctx context.Context, descr string, kvs ...KVer) {
|
|
|
|
From(ctx).Fatal(ctx, descr, kvs...)
|
2018-11-30 23:50:23 +00:00
|
|
|
}
|