-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
159 lines (143 loc) · 4.33 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package logger
import (
"os"
"path/filepath"
"strings"
"time"
)
type (
// KV is a type for logging with more information
// this used by ...WithFields function
KV = map[string]interface{}
// Logger interface
Logger interface {
SetLevel(level Level)
Debug(args ...interface{})
Debugln(args ...interface{})
Debugf(format string, args ...interface{})
DebugWithFields(msg string, KV KV)
Info(args ...interface{})
Infoln(args ...interface{})
Infof(format string, args ...interface{})
InfoWithFields(msg string, KV KV)
Warn(args ...interface{})
Warnln(args ...interface{})
Warnf(format string, args ...interface{})
WarnWithFields(msg string, KV KV)
Error(args ...interface{})
Errorln(args ...interface{})
Errorf(format string, args ...interface{})
ErrorWithFields(msg string, KV KV)
Errors(err error)
Fatal(args ...interface{})
Fatalln(args ...interface{})
Fatalf(format string, args ...interface{})
FatalWithFields(msg string, KV KV)
IsValid() bool // IsValid check if Logger is created using constructor
StdTrace(requestID string, contextID string, err error, metadata interface{}, message string)
StdTracef(requestID string, contextID string, err error, metadata interface{}, format string, args ...interface{})
StdDebug(requestID string, contextID string, err error, metadata interface{}, message string)
StdDebugf(requestID string, contextID string, err error, metadata interface{}, format string, args ...interface{})
StdInfo(requestID string, contextID string, err error, metadata interface{}, message string)
StdInfof(requestID string, contextID string, err error, metadata interface{}, format string, args ...interface{})
StdWarn(requestID string, contextID string, err error, metadata interface{}, message string)
StdWarnf(requestID string, contextID string, err error, metadata interface{}, format string, args ...interface{})
StdError(requestID string, contextID string, err error, metadata interface{}, message string)
StdErrorf(requestID string, contextID string, err error, metadata interface{}, format string, args ...interface{})
StdFatal(requestID string, contextID string, err error, metadata interface{}, message string)
StdFatalf(requestID string, contextID string, err error, metadata interface{}, format string, args ...interface{})
}
// Level of log
Level int
// Engine of logger
Engine string
)
// list of log level
const (
TraceLevel Level = iota
DebugLevel
InfoLevel
WarnLevel
ErrorLevel
FatalLevel
)
// Log level
const (
TraceLevelString = "trace"
DebugLevelString = "debug"
InfoLevelString = "info"
WarnLevelString = "warn"
ErrorLevelString = "error"
FatalLevelString = "fatal"
)
// DefaultTimeFormat of logger
const DefaultTimeFormat = time.RFC3339
// Logger engine option
const (
Logrus Engine = "logrus"
Zerolog Engine = "zerolog"
)
// StringToLevel to set string to level
func StringToLevel(level string) Level {
switch strings.ToLower(level) {
case TraceLevelString:
return TraceLevel
case DebugLevelString:
return DebugLevel
case InfoLevelString:
return InfoLevel
case WarnLevelString:
return WarnLevel
case ErrorLevelString:
return ErrorLevel
case FatalLevelString:
return FatalLevel
default:
// TODO: make this more informative when happened
return InfoLevel
}
}
// LevelToString convert log level to readable string
func LevelToString(l Level) string {
switch l {
case TraceLevel:
return TraceLevelString
case DebugLevel:
return DebugLevelString
case InfoLevel:
return InfoLevelString
case WarnLevel:
return WarnLevelString
case ErrorLevel:
return ErrorLevelString
case FatalLevel:
return FatalLevelString
default:
return InfoLevelString
}
}
// Config of logger
type Config struct {
Level Level
AppName string
LogFile string
TimeFormat string
CallerSkip int
Caller bool
UseColor bool
UseJSON bool
StdLog bool
Debug bool
}
// OpenLogFile tries to open the log file (creates it if not exists) in write-only/append mode and return it
// Note: the func return nil for both *os.File and error if the file name is empty string
func (c *Config) OpenLogFile() (*os.File, error) {
if c.LogFile == "" {
return nil, nil
}
err := os.MkdirAll(filepath.Dir(c.LogFile), 0755)
if err != nil && err != os.ErrExist {
return nil, err
}
return os.OpenFile(c.LogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
}