/
grouplogger.go
77 lines (67 loc) · 2.32 KB
/
grouplogger.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
package log4go
import "context"
// GroupLogger multi logger
type GroupLogger struct {
loggers []Logger
}
// NewGroupLogger new multi logger
func NewGroupLogger(logger ...Logger) *GroupLogger {
return &GroupLogger{
loggers: logger,
}
}
// Info logs a message at InfoLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (g *GroupLogger) Info(ctx context.Context, msg string, fields ...Field) {
for _, l := range g.loggers {
l.Info(ctx, msg, fields...)
}
}
// Debug logs a message at DebugLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (g *GroupLogger) Debug(ctx context.Context, msg string, fields ...Field) {
for _, l := range g.loggers {
l.Debug(ctx, msg, fields...)
}
}
// Warn logs a message at WarnLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (g *GroupLogger) Warn(ctx context.Context, msg string, fields ...Field) {
for _, l := range g.loggers {
l.Warn(ctx, msg, fields...)
}
}
// Error logs a message at ErrorLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func (g *GroupLogger) Error(ctx context.Context, msg string, fields ...Field) {
for _, l := range g.loggers {
l.Error(ctx, msg, fields...)
}
}
// Panic logs a message at PanicLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then panics, even if logging at PanicLevel is disabled.
func (g *GroupLogger) Panic(ctx context.Context, msg string, fields ...Field) {
for _, l := range g.loggers {
l.Panic(ctx, msg, fields...)
}
}
// Fatal logs a message at FatalLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then calls os.Exit(1), even if logging at FatalLevel is
// disabled.
func (g *GroupLogger) Fatal(ctx context.Context, msg string, fields ...Field) {
for _, l := range g.loggers {
l.Fatal(ctx, msg, fields...)
}
}
// Sync flushing any buffered log entries.
//
// Applications should take care to call Sync before exiting.
func (g *GroupLogger) Sync(ctx context.Context) {
for _, l := range g.loggers {
l.Sync(ctx)
}
}