-
Notifications
You must be signed in to change notification settings - Fork 384
/
logging.go
51 lines (42 loc) · 1.18 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
package testutil
import (
"flag"
"os"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"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
)
func Logger(t testing.TB) (*zap.Logger, 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
loggerInstance, loggerCleanup, err = logutil.NewLogger(*logFilters, *logFormat, *logFile)
if !assert.NoError(t, err) {
loggerInstance = zap.NewNop()
loggerCleanup = func() {}
}
})
return loggerInstance, loggerCleanup
}