-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
87 lines (80 loc) · 1.4 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
package logger
import (
"log"
)
type Logger struct {
Writer
ID string
Prefixs []Prefix
}
func (l *Logger) ReplaceWriter(w Writer) error {
if l.Writer != nil {
err := l.Writer.Close()
if err != nil {
return err
}
}
l.Writer = w
return l.Writer.Open()
}
func (l *Logger) Log(s string) {
var err error
defer func() {
if err != nil {
log.Fatalln(err.Error())
}
}()
output := getPrefixs(l, DefaultPrefixSep, l.Prefixs...)
output = output + s
err = l.Writer.WriteLine(output)
return
}
func (l *Logger) SetWriter(w Writer) *Logger {
l.Writer = w
return l
}
func (l *Logger) SetID(id string) *Logger {
l.ID = id
return l
}
func (l *Logger) SetPrefixs(p ...Prefix) *Logger {
l.Prefixs = p
return l
}
func (l *Logger) AppendPrefixs(p ...Prefix) *Logger {
l.Prefixs = append(l.Prefixs, p...)
return l
}
func (l *Logger) Clone() *Logger {
p := make([]Prefix, len(l.Prefixs))
copy(p, l.Prefixs)
return &Logger{
ID: l.ID,
Writer: l.Writer,
Prefixs: p,
}
}
func (l *Logger) SubLogger() *Logger {
logger := l.Clone()
logger.Writer = l
return logger
}
func (l *Logger) FormatLogger() *FormatLogger {
return &FormatLogger{
Logger: l,
Formatter: nil,
}
}
func NewLogger() *Logger {
return &Logger{
Writer: Null,
Prefixs: []Prefix{},
}
}
func createLogger(w Writer, id string, p ...Prefix) *Logger {
return &Logger{
ID: id,
Writer: w,
Prefixs: p,
}
}