forked from op/go-logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi.go
65 lines (57 loc) · 1.85 KB
/
multi.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
// Copyright 2013, Örjan Persson. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package logging
// TODO remove Level stuff from the multi logger. Do one thing.
// multiLogger is a log multiplexer which can be used to utilize multiple log
// backends at once.
type multiLogger struct {
backends []LeveledBackend
}
// MultiLogger creates a logger which contain multiple loggers.
func MultiLogger(backends ...Backend) LeveledBackend {
var leveledBackends []LeveledBackend
for _, backend := range backends {
leveledBackends = append(leveledBackends, AddModuleLevel(backend))
}
return &multiLogger{leveledBackends}
}
// Log passes the log record to all backends.
func (b *multiLogger) Log(level Level, calldepth int, rec *Record) (err error) {
for _, backend := range b.backends {
if backend.IsEnabledFor(level, rec.Module) {
// Shallow copy of the record for the formatted cache on Record and get the
// record formatter from the backend.
r2 := *rec
if e := backend.Log(level, calldepth+1, &r2); e != nil {
err = e
}
}
}
return
}
// GetLevel returns the highest level enabled by all backends.
func (b *multiLogger) GetLevel(module string) Level {
var level Level
for _, backend := range b.backends {
if backendLevel := backend.GetLevel(module); backendLevel > level {
level = backendLevel
}
}
return level
}
// SetLevel propagates the same level to all backends.
func (b *multiLogger) SetLevel(level Level, module string) {
for _, backend := range b.backends {
backend.SetLevel(level, module)
}
}
// IsEnabledFor returns true if any of the backends are enabled for it.
func (b *multiLogger) IsEnabledFor(level Level, module string) bool {
for _, backend := range b.backends {
if backend.IsEnabledFor(level, module) {
return true
}
}
return false
}