/
backend.go
136 lines (115 loc) · 3.02 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
134
135
136
package klog
import (
"fmt"
"io"
"os"
"strings"
"github.com/tliron/kutil/logging"
"github.com/tliron/kutil/util"
"k8s.io/klog/v2"
)
const LOG_FILE_WRITE_PERMISSIONS = 0600
const BUFFER_SIZE = 10000
func init() {
backend := NewBackend()
backend.Configure(0, nil)
logging.SetBackend(backend)
}
//
// Backend
//
type Backend struct {
Buffered bool
writer io.Writer
hierarchy *logging.Hierarchy
}
func NewBackend() *Backend {
return &Backend{
Buffered: true,
hierarchy: logging.NewMaxLevelHierarchy(),
}
}
var flushHandle util.ExitFunctionHandle
// logging.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 := logging.VerbosityToMaxLevel(verbosity)
if maxLevel == logging.None {
klog.SetOutput(io.Discard)
self.hierarchy.SetMaxLevel(nil, logging.None)
} else {
if path != nil {
if file, err := os.OpenFile(*path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, LOG_FILE_WRITE_PERMISSIONS); err == nil {
util.OnExitError(file.Close)
if self.Buffered {
writer := util.NewBufferedWriter(file, BUFFER_SIZE)
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, BUFFER_SIZE)
util.OnExitError(writer.Close)
self.writer = writer
klog.SetOutput(writer)
} else {
klog.SetOutput(util.NewSyncedWriter(os.Stderr))
}
}
self.hierarchy.SetMaxLevel(nil, maxLevel)
}
}
// logging.Backend interface
func (self *Backend) GetWriter() io.Writer {
return self.writer
}
// logging.Backend interface
func (self *Backend) NewMessage(name []string, level logging.Level, depth int) logging.Message {
if self.AllowLevel(name, level) {
depth += 2
return logging.NewUnstructuredMessage(func(message string) {
if name := strings.Join(name, "."); len(name) > 0 {
message = name + ": " + message
}
switch level {
case logging.Critical:
klog.ErrorDepth(depth, message)
case logging.Error:
klog.ErrorDepth(depth, message)
case logging.Warning:
klog.WarningDepth(depth, message)
case logging.Notice:
klog.InfoDepth(depth, message)
case logging.Info:
klog.InfoDepth(depth, message)
case logging.Debug:
klog.InfoDepth(depth, message)
default:
panic(fmt.Sprintf("unsupported level: %d", level))
}
})
} else {
return nil
}
}
// logging.Backend interface
func (self *Backend) AllowLevel(name []string, level logging.Level) bool {
return self.hierarchy.AllowLevel(name, level)
}
// logging.Backend interface
func (self *Backend) SetMaxLevel(name []string, level logging.Level) {
self.hierarchy.SetMaxLevel(name, level)
}
// logging.Backend interface
func (self *Backend) GetMaxLevel(name []string) logging.Level {
return self.hierarchy.GetMaxLevel(name)
}