-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_posix.go
59 lines (53 loc) · 1.36 KB
/
logger_posix.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
//go:build !windows
package logger
import (
"context"
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// NewLogger creates a new logr.Logger and its associated flush function.
func newLogger(ctx context.Context, enabLevel zapcore.Level, encoder zapcore.Encoder, writeSyncer zapcore.WriteSyncer) (logr.Logger, func(), error) {
enab := zap.NewAtomicLevel()
enab.SetLevel(enabLevel)
core := zapcore.NewCore(encoder, writeSyncer, enab)
stackTraceLevel := zap.NewAtomicLevel()
stackTraceLevel.SetLevel(zapcore.ErrorLevel)
l := zap.New(core, zap.WithCaller(true), zap.AddStacktrace(stackTraceLevel))
if ctx != nil {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1, syscall.SIGUSR2)
go func(ctx context.Context) {
defer signal.Stop(c)
for {
select {
case sig := <-c:
if sig == syscall.SIGUSR1 {
if klogV < 127 {
klogV += 1
if err := klogFS.Set("v", strconv.Itoa(klogV)); err != nil {
fmt.Println(err)
}
}
} else {
if klogV > 0 {
klogV -= 1
if err := klogFS.Set("v", strconv.Itoa(klogV)); err != nil {
fmt.Println(err)
}
}
}
case <-ctx.Done():
return
}
}
}(ctx)
}
return zapr.NewLoggerWithOptions(l), func() { _ = l.Sync() }, nil
}