-
Notifications
You must be signed in to change notification settings - Fork 384
/
logging.go
64 lines (53 loc) · 1.55 KB
/
logging.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
package testutil
import (
"flag"
"os"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"moul.io/zapring"
"berty.tech/berty/v2/go/internal/logutil"
)
const defaultLoggingFilters = "info+:bty.test* error+:*,-ipfs*"
var (
logFilters = flag.String("log-filters", defaultLoggingFilters, "log namespaces")
logFile = flag.String("log-file", "", "log to file")
logFormat = flag.String("log-format", "color", "json, console, color")
loggerInstance *zap.Logger
loggerCleanup func()
loggerInitOnce sync.Once
loggerRing *zapring.Core
)
func Logger(t testing.TB) (*zap.Logger, func()) {
t.Helper()
loggerInstance, _, loggerCleanup := LoggerWithRing(t)
return loggerInstance, loggerCleanup
}
func LoggerWithRing(t testing.TB) (*zap.Logger, *zapring.Core, func()) {
t.Helper()
loggerInitOnce.Do(func() {
if val := os.Getenv("BERTY_LOGFILTERS"); val != "" {
*logFilters = val
}
if val := os.Getenv("BERTY_LOGFILE"); val != "" {
*logFile = val
}
if val := os.Getenv("BERTY_LOGFORMAT"); val != "" {
*logFormat = val
}
*logFilters = strings.ReplaceAll(*logFilters, ":default:", defaultLoggingFilters)
var err error
loggerRing = zapring.New(10 * 1024 * 1024)
loggerInstance, loggerCleanup, err = logutil.NewLogger(
logutil.NewStdStream(*logFilters, *logFormat, *logFile),
logutil.NewRingStream(*logFilters, *logFormat, loggerRing),
)
if !assert.NoError(t, err) {
loggerInstance = zap.NewNop()
loggerCleanup = func() {}
}
})
return loggerInstance, loggerRing, loggerCleanup
}