-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.go
57 lines (47 loc) · 1.01 KB
/
global.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
package ilog
import (
"log"
"os"
"sync"
)
//nolint:gochecknoglobals
var (
_globalLogger Logger = NewBuilder(DebugLevel, os.Stdout).Build() //nolint:revive
_globalLoggerMu sync.RWMutex
)
func L() Logger { //nolint:ireturn
_globalLoggerMu.RLock()
l := _globalLogger
_globalLoggerMu.RUnlock()
return l
}
func Global() Logger { //nolint:ireturn
return L()
}
func SetGlobal(logger Logger) (rollback func()) {
_globalLoggerMu.Lock()
backup := _globalLogger
_globalLogger = logger
_globalLoggerMu.Unlock()
return func() {
SetGlobal(backup)
}
}
//nolint:gochecknoglobals
var stdLoggerMu sync.Mutex
func SetStdLogger(l Logger) (rollback func()) {
stdLoggerMu.Lock()
defer stdLoggerMu.Unlock()
backupFlags := log.Flags()
backupPrefix := log.Prefix()
backupWriter := log.Writer()
log.SetFlags(0)
log.SetPrefix("")
const skipForStgLogger = 2
log.SetOutput(l.Copy().AddCallerSkip(skipForStgLogger))
return func() {
log.SetFlags(backupFlags)
log.SetPrefix(backupPrefix)
log.SetOutput(backupWriter)
}
}