-
Notifications
You must be signed in to change notification settings - Fork 24
/
logger.go
69 lines (55 loc) · 1.43 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
61
62
63
64
65
66
67
68
69
// Copyright 2019-2024 go-tcap authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package tcap
import (
"io"
"log"
"os"
"sync"
)
var (
logger = log.New(os.Stderr, "", log.LstdFlags)
logMu sync.Mutex
)
// SetLogger replaces the standard logger with arbitrary *log.Logger.
//
// This package prints just informational logs from goroutines working background
// that might help developers test the program but can be ignored safely. More
// important ones that needs any action by caller would be returned as errors.
func SetLogger(l *log.Logger) {
if l == nil {
log.Println("Don't pass nil to SetLogger: use DisableLogging instead.")
}
setLogger(l)
}
// EnableLogging enables the logging from the package.
// If l is nil, it uses default logger provided by the package.
// Logging is enabled by default.
//
// See also: SetLogger.
func EnableLogging(l *log.Logger) {
logMu.Lock()
defer logMu.Unlock()
setLogger(l)
}
// DisableLogging disables the logging from the package.
// Logging is enabled by default.
func DisableLogging() {
logMu.Lock()
defer logMu.Unlock()
logger.SetOutput(io.Discard)
}
func setLogger(l *log.Logger) {
if l == nil {
l = log.New(os.Stderr, "", log.LstdFlags)
}
logMu.Lock()
defer logMu.Unlock()
logger = l
}
func logf(format string, v ...interface{}) {
logMu.Lock()
defer logMu.Unlock()
logger.Printf(format, v...)
}