forked from opencomputeproject/OSF-OSFCI
/
zlog.go
99 lines (84 loc) · 2.21 KB
/
zlog.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
/*Package base logger notes: same as zap.go*/
package base
import "errors"
// Zlog A global variable so that log functions can be directly accessed
var Zlog Logger
// Fields Type to pass when we want to call WithFields for structured logging
type Fields map[string]interface{}
const (
//Debug for verbose logging
Debug = "debug"
//Info for info logging
Info = "info"
//Warn for warning logs
Warn = "warn"
//Error for error logs
Error = "error"
//Fatal is for logging fatal messages. The system shutsdown after logging the message.
Fatal = "fatal"
)
const (
//InstanceZapLogger for instance
InstanceZapLogger int = iota
)
var (
errInvalidLoggerInstance = errors.New("Invalid logger instance")
)
// Logger interface
type Logger interface {
//Debugf level interface
Debugf(format string, args ...interface{})
//Infof level interface
Infof(format string, args ...interface{})
//Warnf level interface
Warnf(format string, args ...interface{})
//Errorf level interface
Errorf(format string, args ...interface{})
//Fatalf level interface
Fatalf(format string, args ...interface{})
//Panic level interface
Panicf(format string, args ...interface{})
}
// Configuration stores the config for the logger
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) error {
logger, err := newZapLogger(config)
if err != nil {
return err
}
Zlog = logger
return nil
}
// Debugf interface impl
func Debugf(format string, args ...interface{}) {
Zlog.Debugf(format, args...)
}
// Infof interface impl
func Infof(format string, args ...interface{}) {
Zlog.Infof(format, args...)
}
// Warnf interface impl
func Warnf(format string, args ...interface{}) {
Zlog.Warnf(format, args...)
}
// Errorf interface impl
func Errorf(format string, args ...interface{}) {
Zlog.Errorf(format, args...)
}
// Fatalf interface impl
func Fatalf(format string, args ...interface{}) {
Zlog.Fatalf(format, args...)
}
// Panicf interface impl
func Panicf(format string, args ...interface{}) {
Zlog.Panicf(format, args...)
}