-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
60 lines (47 loc) · 1.21 KB
/
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
/* https://github.com/cihub/seelog/wiki/Writing-libraries-with-Seelog */
package smcroute
import (
"fmt"
"io"
"sync"
seelog "github.com/cihub/seelog"
)
var (
loggerMutex sync.RWMutex
logger seelog.LoggerInterface
)
func init() {
// Disable logger by default.
DisableLog()
}
func log() seelog.LoggerInterface {
loggerMutex.RLock()
defer loggerMutex.RUnlock()
return logger
}
// DisableLog disables all library log output.
func DisableLog() { UseLogger(seelog.Disabled) }
// UseLogger uses a specified seelog.LoggerInterface to output library log.
// Use this func if you are using Seelog logging system in your app.
func UseLogger(newLogger seelog.LoggerInterface) {
loggerMutex.Lock()
defer loggerMutex.Unlock()
logger = newLogger
}
// SetLogWriter uses a specified io.Writer to output library log.
// Use this func if you are not using Seelog logging system in your app.
func SetLogWriter(writer io.Writer) error {
if writer == nil {
return fmt.Errorf("provided writer is nil")
}
newLogger, err := seelog.LoggerFromWriterWithMinLevel(writer, seelog.TraceLvl)
if err != nil {
return err
}
UseLogger(newLogger)
return nil
}
// Call this before app shutdown
func FlushLog() {
logger.Flush()
}