-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlogger.go
100 lines (78 loc) · 2.22 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
package logger
import "errors"
var log Logger
//Fields Type to pass when we want to call WithFields for structured logging
type Fields map[string]interface{}
const (
//Debug has verbose message
Debug = "debug"
//Info is default log level
Info = "info"
//Warn is for logging messages about possible issues
Warn = "warn"
//Error is for logging errors
Error = "error"
//Fatal is for logging fatal messages. The sytem shutsdown after logging the message.
Fatal = "fatal"
)
const (
//InstanceZapLogger will be used to create Zap instance for the logger
InstanceZapLogger int = iota
)
var (
errInvalidLoggerInstance = errors.New("Invalid logger instance")
)
//Logger is our contract for the logger
type Logger interface {
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
Panicf(format string, args ...interface{})
WithFields(keyValues Fields) Logger
}
// Configuration stores the config for the Logger
// For some loggers there can only be one level across writers, for such the level of Console is picked by default
type Configuration struct {
EnableConsole bool
ConsoleJSONFormat bool
ConsoleLevel string
EnableFile bool
FileJSONFormat bool
FileLevel string
FileLocation string
}
//NewLogger returns an instance of Logger
func NewLogger(config Configuration, loggerInstance int) error {
if loggerInstance == InstanceZapLogger {
logger, err := newZapLogger(config)
if err != nil {
return err
}
log = logger
return nil
}
return errInvalidLoggerInstance
}
func Debugf(format string, args ...interface{}) {
log.Debugf(format, args...)
}
func Infof(format string, args ...interface{}) {
log.Infof(format, args...)
}
func Warnf(format string, args ...interface{}) {
log.Warnf(format, args...)
}
func Errorf(format string, args ...interface{}) {
log.Errorf(format, args...)
}
func Fatalf(format string, args ...interface{}) {
log.Fatalf(format, args...)
}
func Panicf(format string, args ...interface{}) {
log.Panicf(format, args...)
}
func WithFields(keyValues Fields) Logger {
return log.WithFields(keyValues)
}