-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
133 lines (113 loc) · 3.17 KB
/
backend.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package klog
import (
"fmt"
"io"
"os"
"github.com/tliron/commonlog"
"github.com/tliron/kutil/util"
"k8s.io/klog/v2"
)
const (
LogFileWritePermissions = 0600
DefaultBufferSize = 1_000
)
func init() {
backend := NewBackend()
backend.Configure(0, nil)
commonlog.SetBackend(backend)
}
//
// Backend
//
type Backend struct {
BufferSize int
Buffered bool
writer io.Writer
nameHierarchy *commonlog.NameHierarchy
}
func NewBackend() *Backend {
return &Backend{
BufferSize: DefaultBufferSize,
Buffered: true,
nameHierarchy: commonlog.NewNameHierarchy(),
}
}
var flushHandle util.ExitFunctionHandle
// ([commonlog.Backend] interface)
func (self *Backend) Configure(verbosity int, path *string) {
// klog can also do its own configuration via klog.InitFlags
if flushHandle == 0 {
flushHandle = util.OnExit(klog.Flush)
}
maxLevel := commonlog.VerbosityToMaxLevel(verbosity)
if maxLevel == commonlog.None {
klog.SetOutput(io.Discard)
self.nameHierarchy.SetMaxLevel(commonlog.None)
} else {
if path != nil {
if file, err := os.OpenFile(*path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, LogFileWritePermissions); err == nil {
util.OnExitError(file.Close)
if self.Buffered {
writer := util.NewBufferedWriter(file, self.BufferSize, false)
util.OnExitError(writer.Close)
self.writer = writer
klog.SetOutput(writer)
} else {
klog.SetOutput(util.NewSyncedWriter(file))
}
} else {
util.Failf("log file error: %s", err.Error())
}
} else if self.Buffered {
writer := util.NewBufferedWriter(os.Stderr, self.BufferSize, false)
util.OnExitError(writer.Close)
self.writer = writer
klog.SetOutput(writer)
} else {
klog.SetOutput(util.NewSyncedWriter(os.Stderr))
}
self.nameHierarchy.SetMaxLevel(maxLevel)
}
}
// ([commonlog.Backend] interface)
func (self *Backend) GetWriter() io.Writer {
return self.writer
}
// ([commonlog.Backend] interface)
func (self *Backend) NewMessage(level commonlog.Level, depth int, name ...string) commonlog.Message {
if self.AllowLevel(level, name...) {
return commonlog.TraceMessage(commonlog.NewLinearMessage(func(message *commonlog.LinearMessage) {
message_ := message.StringWithPrefix(name...)
switch level {
case commonlog.Critical:
klog.ErrorDepth(depth, message_)
case commonlog.Error:
klog.ErrorDepth(depth, message_)
case commonlog.Warning:
klog.WarningDepth(depth, message_)
case commonlog.Notice:
klog.InfoDepth(depth, message_)
case commonlog.Info:
klog.InfoDepth(depth, message_)
case commonlog.Debug:
klog.InfoDepth(depth, message_)
default:
panic(fmt.Sprintf("unsupported log level: %d", level))
}
}), depth)
} else {
return nil
}
}
// ([commonlog.Backend] interface)
func (self *Backend) AllowLevel(level commonlog.Level, name ...string) bool {
return self.nameHierarchy.AllowLevel(level, name...)
}
// ([commonlog.Backend] interface)
func (self *Backend) SetMaxLevel(level commonlog.Level, name ...string) {
self.nameHierarchy.SetMaxLevel(level, name...)
}
// ([commonlog.Backend] interface)
func (self *Backend) GetMaxLevel(name ...string) commonlog.Level {
return self.nameHierarchy.GetMaxLevel(name...)
}