/
wrapper.go
71 lines (65 loc) · 2.39 KB
/
wrapper.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
package sdk
import (
sdklog "log"
"github.com/echocat/slf4g/level"
log "github.com/echocat/slf4g"
)
// Configure configures the standard SDK logging framework to use slf4g with
// its root Logger and logs everything printed using Print(), Printf() and
// Println() on level.Info.
//
// Limitations
//
// 1# Stuff logged using Fatal*() and Panic*() are on logged on the same Level
// as everything else.
//
// 2# ATTENTION! Fatal*() and Panic*() will still exit the whole application or
// panics afterwards. This cannot be prevented when you use the logger
// directly. If possible use NewLogger() to use an SDK compatible interface.
func Configure(customizer ...func(*log.LoggingWriter)) {
ConfigureWith(log.GetRootLogger(), level.Info, customizer...)
}
// ConfigureWith configures the standard SDK logging framework to use slf4g with
// it's the given Logger and logs everything printed using Print(), Printf() and
// Println() on given level.Level.
//
// Limitations
//
// 1# Stuff logged using Fatal*() and Panic*() are on logged on the same Level
// as everything else.
//
// 2# ATTENTION! Fatal*() and Panic*() will still exit the whole application or
// panics afterwards. This cannot be prevented when you use the logger
// directly. If possible use NewLogger() to use an SDK compatible interface.
func ConfigureWith(target log.CoreLogger, logAs level.Level, customizer ...func(*log.LoggingWriter)) {
w := &log.LoggingWriter{
Logger: target,
LevelExtractor: level.FixedLevelExtractor(logAs),
SkipFrames: 2, // of the SDK based log
}
for _, c := range customizer {
c(w)
}
sdklog.SetOutput(w)
sdklog.SetPrefix("")
sdklog.SetFlags(0)
}
// NewWrapper creates an SDK Logger which is use slf4g the provided Logger and
// logs everything printed using Print(), Printf() and Println() on given
// level.Level.
//
// Limitations
//
// 1# Stuff logged using Fatal*() and Panic*() are on logged on the same Level
// as everything else.
//
// 2# ATTENTION! Fatal*() and Panic*() will still exit the whole application or
// panics afterwards. This cannot be prevented when you use the logger
// directly. If possible use NewLogger() to use an SDK compatible interface.
func NewWrapper(target log.CoreLogger, logAs level.Level) *sdklog.Logger {
return sdklog.New(&log.LoggingWriter{
Logger: target,
LevelExtractor: level.FixedLevelExtractor(logAs),
SkipFrames: 2, // of the SDK based log
}, "", 0)
}