-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
83 lines (68 loc) · 1.53 KB
/
log.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
package log
import (
"fmt"
"os"
)
// LogLevel is a string type for log level.
type LogLevel string
// Log levels
const (
LPanic LogLevel = "PANIC"
LFatal LogLevel = "FATAL"
LError LogLevel = "ERROR"
LWarning LogLevel = "WARNING"
LInfo LogLevel = "INFO"
)
// Logger defines the interface to logger methods.
type Logger interface {
Panic(format string, args ...interface{})
Fatal(format string, args ...interface{})
Error(format string, args ...interface{})
Warning(format string, args ...interface{})
Info(format string, args ...interface{})
Name() string
}
var (
handlers = make(map[string]Logger)
)
// AddLogger adds a Logger handlers.
func AddLogger(logger Logger) error {
if logger == nil {
return fmt.Errorf("logger can't be nil.")
}
handlers[logger.Name()] = logger
return nil
}
// Panic log.
func Panic(format string, args ...interface{}) {
for _, logger := range handlers {
logger.Panic(format, args...)
}
msg := fmt.Sprintf(format, args...)
panic(msg)
}
// Fatal log.
func Fatal(format string, args ...interface{}) {
for _, logger := range handlers {
logger.Fatal(format, args...)
}
os.Exit(1)
}
// Error log.
func Error(format string, args ...interface{}) {
for _, logger := range handlers {
logger.Error(format, args...)
}
}
// Warning log.
func Warning(format string, args ...interface{}) {
for _, logger := range handlers {
logger.Warning(format, args...)
}
}
// Info log.
func Info(format string, args ...interface{}) {
for _, logger := range handlers {
logger.Info(format, args...)
}
}