forked from bdr08349/ladder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
149 lines (120 loc) · 3.36 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
package log
import (
"io"
"os"
"sync"
"github.com/Sirupsen/logrus"
)
var (
// Logger is the app logger
Logger *Log
lMutex = &sync.Mutex{}
)
// Values of the logger
var (
lOut io.Writer
lDebug bool
lFormatter logrus.Formatter
lFields = Fields{}
)
// Set default logger
func init() {
Setup(os.Stderr, Fields{}, false, false)
Logger.Info("Finished setting up default logger")
}
// Setup configures defaulot values of the application logger, this values will be used to create new loggers and global loggers
func Setup(out io.Writer, fields Fields, json bool, debug bool) {
lMutex.Lock()
defer lMutex.Unlock()
// Set package globals
lOut = out
lDebug = debug
lFields = fields
if json {
lFormatter = new(logrus.JSONFormatter)
} else {
lFormatter = new(logrus.TextFormatter)
}
Logger = New()
Logger.Info("Logger set up")
}
// New creates a logger based on the global logger settings
func New() *Log {
lvl := logrus.InfoLevel
if lDebug {
lvl = logrus.DebugLevel
}
l := &Log{
Logger: logrus.Logger{
Out: lOut,
Formatter: lFormatter,
Hooks: make(logrus.LevelHooks),
Level: lvl,
},
fields: lFields,
}
return l
}
// Fields is a custom value for logger fields
type Fields map[string]interface{}
// Log is the abstraction layer type used for logging on application
type Log struct {
logrus.Logger
fields Fields
}
// WithFields Returns a logger based on the global one with one field
func WithFields(fields Fields) *Log {
lg := New()
// Custom fields
lg.fields = fields
// Add default fields
lg.WithFields(lFields)
return lg
}
// WithField returns logger based on the global on with multiple fields
func WithField(key string, value interface{}) *Log {
f := Fields{key: value}
return WithFields(f)
}
// WithFields Returns a logger based on the global one with one field
func (l *Log) WithFields(fields map[string]interface{}) {
for k, v := range fields {
l.fields[k] = v
}
}
// WithField returns logger based on the global on with multiple fields
func (l *Log) WithField(key string, value interface{}) {
l.fields[key] = value
}
// Debugf prints debug log line
func (l *Log) Debugf(format string, args ...interface{}) {
l.Logger.WithFields(logrus.Fields(l.fields)).Debugf(format, args...)
}
// Infof prints info log line
func (l *Log) Infof(format string, args ...interface{}) {
l.Logger.WithFields(logrus.Fields(l.fields)).Infof(format, args...)
}
// Printf prints log line
func (l *Log) Printf(format string, args ...interface{}) {
l.Logger.WithFields(logrus.Fields(l.fields)).Printf(format, args...)
}
// Warnf prints warning log line
func (l *Log) Warnf(format string, args ...interface{}) {
l.Logger.WithFields(logrus.Fields(l.fields)).Warnf(format, args...)
}
// Warningf prints warning log line
func (l *Log) Warningf(format string, args ...interface{}) {
l.Logger.WithFields(logrus.Fields(l.fields)).Warningf(format, args...)
}
// Errorf prints error log line
func (l *Log) Errorf(format string, args ...interface{}) {
l.Logger.WithFields(logrus.Fields(l.fields)).Errorf(format, args...)
}
// Fatalf prints fatal log line
func (l *Log) Fatalf(format string, args ...interface{}) {
l.Logger.WithFields(logrus.Fields(l.fields)).Fatalf(format, args...)
}
// Panicf prints panic log line
func (l *Log) Panicf(format string, args ...interface{}) {
l.Logger.WithFields(logrus.Fields(l.fields)).Panicf(format, args...)
}