-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
116 lines (98 loc) · 2.54 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
package logo
import (
"fmt"
"io"
"os"
)
// Logger holds all Receivers
type Logger struct {
Receivers []*Receiver
Active bool
}
// NewLogger returns a new Logger filled with given Receivers
func NewLogger(recs ...*Receiver) *Logger {
l := &Logger{
Active: true, // Every logger is active by default
Receivers: recs,
}
return l
}
// NewSimpleLogger returns a logger with one simple Receiver
func NewSimpleLogger(w io.Writer, lvl Level, prefix string, color bool) *Logger {
l := &Logger{}
r := NewReceiver(w, prefix)
r.Color = color
r.Level = lvl
l.Receivers = []*Receiver{r}
l.Active = true
return l
}
// SetLevel sets the log level of ALL receivers
func (l *Logger) SetLevel(lvl Level) {
for _, r := range l.Receivers {
r.Level = lvl
}
}
// SetPrefix sets the prefix of ALL receivers
func (l *Logger) SetPrefix(prefix string) {
for _, r := range l.Receivers {
r.SetPrefix(prefix)
}
}
// Write to all Receivers
func (l *Logger) logAll(opt *levelOptions, s string) {
// Skip everything if logger is disabled
if !l.Active {
return
}
// Log to all receivers
for _, r := range l.Receivers {
r.log(opt, s)
}
}
// Debug logs arguments
func (l *Logger) Debug(a ...interface{}) {
l.logAll(optDebug, fmt.Sprint(a...))
}
// Info logs arguments
func (l *Logger) Info(a ...interface{}) {
l.logAll(optInfo, fmt.Sprint(a...))
}
// Warn logs arguments
func (l *Logger) Warn(a ...interface{}) {
l.logAll(optWarn, fmt.Sprint(a...))
}
// Error logs arguments
func (l *Logger) Error(a ...interface{}) {
l.logAll(optError, fmt.Sprint(a...))
}
// Fatal logs arguments
func (l *Logger) Fatal(a ...interface{}) {
l.logAll(optFatal, fmt.Sprint(a...))
os.Exit(1)
}
// Debugf logs formated arguments
func (l *Logger) Debugf(format string, a ...interface{}) {
l.logAll(optDebug, fmt.Sprintf(format, a...))
}
// Infof logs formated arguments
func (l *Logger) Infof(format string, a ...interface{}) {
l.logAll(optInfo, fmt.Sprintf(format, a...))
}
// Warnf logs formated arguments
func (l *Logger) Warnf(format string, a ...interface{}) {
l.logAll(optWarn, fmt.Sprintf(format, a...))
}
// Errorf logs formated arguments
func (l *Logger) Errorf(format string, a ...interface{}) {
l.logAll(optError, fmt.Sprintf(format, a...))
}
// Fatalf logs formated arguments
func (l *Logger) Fatalf(format string, a ...interface{}) {
l.logAll(optFatal, fmt.Sprintf(format, a...))
os.Exit(1)
}
// Open is a short function to open a file with needed options
func Open(path string) (*os.File, error) {
return os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0660)
}