-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
106 lines (91 loc) · 2.54 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package log
import (
"context"
"fmt"
)
// Logger interface for log provider
type Logger interface {
Name() string
Debugf(c context.Context, format string, args ...interface{})
Infof(c context.Context, format string, args ...interface{})
Warningf(c context.Context, format string, args ...interface{})
Errorf(c context.Context, format string, args ...interface{})
Criticalf(c context.Context, format string, args ...interface{})
}
var _loggers []Logger
// NumberOfLoggers returns number of registered loggers
func NumberOfLoggers() int {
return len(_loggers)
}
// AddLogger registers logger
func AddLogger(logger Logger) {
name := logger.Name()
for _, l := range _loggers {
if l.Name() == name {
panic(fmt.Sprintf("Duplicate logger name: [%v], len(_loggers): %d", name, len(_loggers)))
}
}
_loggers = append(_loggers, logger)
}
var loggingDisabled = "loggingDisabled" // TODO: Check core libs on how to do it proepry
// NewContextWithLoggingDisabled creates context with disabled logging
func NewContextWithLoggingDisabled(c context.Context) context.Context {
return context.WithValue(c, &loggingDisabled, true)
}
// NewContextWithLoggingEnabled creates context with enabled logging
func NewContextWithLoggingEnabled(c context.Context) context.Context {
return context.WithValue(c, &loggingDisabled, false)
}
func isDisabled(c context.Context) bool {
if disabled := c.Value(&loggingDisabled); disabled != nil {
return disabled.(bool)
}
return false
}
// Func function that do logging
type Func func(c context.Context, format string, args ...interface{})
// Debugf logs debug message
func Debugf(c context.Context, format string, args ...interface{}) {
if isDisabled(c) {
return
}
for _, l := range _loggers {
l.Debugf(c, format, args...)
}
}
// Infof logs information message
func Infof(c context.Context, format string, args ...interface{}) {
if isDisabled(c) {
return
}
for _, l := range _loggers {
l.Infof(c, format, args...)
}
}
// Warningf logs warning message
func Warningf(c context.Context, format string, args ...interface{}) {
if isDisabled(c) {
return
}
for _, l := range _loggers {
l.Warningf(c, format, args...)
}
}
// Errorf logs error message
func Errorf(c context.Context, format string, args ...interface{}) {
if isDisabled(c) {
return
}
for _, l := range _loggers {
l.Errorf(c, format, args...)
}
}
// Criticalf logs critical message
func Criticalf(c context.Context, format string, args ...interface{}) {
if isDisabled(c) {
return
}
for _, l := range _loggers {
l.Criticalf(c, format, args...)
}
}