-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
58 lines (48 loc) · 1.2 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package log
import "context"
type (
// LoggerKey define context key for logger
LoggerKey struct{}
)
var _logger = newDefaultZap()
// Logger interface
type Logger interface {
Err(err error) Logger
Fields(fields map[string]interface{}) Logger
Field(key string, val interface{}) Logger
Debug(msg string)
Info(msg string)
Warn(msg string)
Error(msg string)
Fatal(msg string)
Debugf(msg string, args ...interface{})
Infof(msg string, args ...interface{})
Warnf(msg string, args ...interface{})
Errorf(msg string, args ...interface{})
Fatalf(msg string, args ...interface{})
Caller(stack int) Logger
Attach(ctx context.Context) context.Context
Printf(msg string, args ...interface{})
}
// SetGlobal set global logger
func SetGlobal(logger Logger) Logger {
_logger = logger
return _logger
}
// Get get global logger
func Get() Logger {
return _logger
}
// Attach attach logger instance into context
func Attach(ctx context.Context, logger Logger) context.Context {
return context.WithValue(ctx, LoggerKey{}, logger)
}
// Ctx get logger instacne from context
func Ctx(ctx context.Context) Logger {
val := ctx.Value(LoggerKey{})
logger, ok := val.(Logger)
if !ok {
return Get()
}
return logger
}