-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_logger.go
91 lines (78 loc) · 1.79 KB
/
multi_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
package log
import (
"github.com/no-src/log/formatter"
)
type multiLogger struct {
loggers []Logger
}
// NewMultiLogger get a multi logger, write log to multiple loggers
func NewMultiLogger(loggers ...Logger) Logger {
logger := &multiLogger{}
for _, l := range loggers {
if l != nil {
logger.loggers = append(logger.loggers, l)
}
}
return logger
}
func (l *multiLogger) Debug(format string, args ...any) {
for _, logger := range l.loggers {
logger.Debug(format, args...)
}
}
func (l *multiLogger) Info(format string, args ...any) {
for _, logger := range l.loggers {
logger.Info(format, args...)
}
}
func (l *multiLogger) Warn(format string, args ...any) {
for _, logger := range l.loggers {
logger.Warn(format, args...)
}
}
func (l *multiLogger) Error(err error, format string, args ...any) {
for _, logger := range l.loggers {
logger.Error(err, format, args...)
}
}
func (l *multiLogger) ErrorIf(err error, format string, args ...any) error {
return errorIf(l.Error, err, format, args...)
}
func (l *multiLogger) Log(format string, args ...any) {
for _, logger := range l.loggers {
logger.Log(format, args...)
}
}
func (l *multiLogger) Close() error {
var err error
for _, logger := range l.loggers {
closeErr := logger.Close()
if closeErr != nil {
err = closeErr
}
}
return err
}
func (l *multiLogger) Write(p []byte) (n int, err error) {
n = len(p)
for _, logger := range l.loggers {
nn, writeErr := logger.Write(p)
if writeErr != nil {
err = writeErr
n = nn
}
}
return n, err
}
func (l *multiLogger) WithFormatter(f formatter.Formatter) Logger {
for _, logger := range l.loggers {
logger.WithFormatter(f)
}
return l
}
func (l *multiLogger) WithTimeFormat(f string) Logger {
for _, logger := range l.loggers {
logger.WithTimeFormat(f)
}
return l
}