...
1
16
17 package v1
18
19 import (
20 "fmt"
21 "sort"
22 "strings"
23 "sync"
24
25 "github.com/go-logr/logr"
26
27 "k8s.io/component-base/featuregate"
28 )
29
30 var logRegistry = newLogFormatRegistry()
31
32
33 type logFormatRegistry struct {
34 mutex sync.Mutex
35 registry map[string]logFormat
36 frozen bool
37 }
38
39 type logFormat struct {
40 factory LogFormatFactory
41 feature featuregate.Feature
42 }
43
44
45
46
47
48 type RuntimeControl struct {
49
50
51 Flush func()
52
53
54
55
56
57
58
59 SetVerbosityLevel func(v uint32) error
60 }
61
62
63
64 type LogFormatFactory interface {
65
66 Create(c LoggingConfiguration, o LoggingOptions) (logr.Logger, RuntimeControl)
67 }
68
69
70
71
72
73 func RegisterLogFormat(name string, factory LogFormatFactory, feature featuregate.Feature) error {
74 return logRegistry.register(name, logFormat{factory, feature})
75 }
76
77 func newLogFormatRegistry() *logFormatRegistry {
78 registry := &logFormatRegistry{
79 registry: make(map[string]logFormat),
80 frozen: false,
81 }
82 _ = registry.register(DefaultLogFormat, logFormat{factory: textFactory{}, feature: LoggingStableOptions})
83 return registry
84 }
85
86
87 func (lfr *logFormatRegistry) register(name string, format logFormat) error {
88 lfr.mutex.Lock()
89 defer lfr.mutex.Unlock()
90 if lfr.frozen {
91 return fmt.Errorf("log format registry is frozen, unable to register log format %s", name)
92 }
93 if _, ok := lfr.registry[name]; ok {
94 return fmt.Errorf("log format: %s already exists", name)
95 }
96 if _, ok := featureGates()[format.feature]; !ok && format.feature != LoggingStableOptions {
97 return fmt.Errorf("log format %s: unsupported feature gate %s", name, format.feature)
98 }
99 lfr.registry[name] = format
100 return nil
101 }
102
103
104 func (lfr *logFormatRegistry) get(name string) (*logFormat, error) {
105 lfr.mutex.Lock()
106 defer lfr.mutex.Unlock()
107 format, ok := lfr.registry[name]
108 if !ok {
109 return nil, fmt.Errorf("log format: %s does not exists", name)
110 }
111 return &format, nil
112 }
113
114
115 func (lfr *logFormatRegistry) list() string {
116 lfr.mutex.Lock()
117 defer lfr.mutex.Unlock()
118 formats := make([]string, 0, len(lfr.registry))
119 for name, format := range lfr.registry {
120 item := fmt.Sprintf(`"%s"`, name)
121 if format.feature != LoggingStableOptions {
122 item += fmt.Sprintf(" (gated by %s)", format.feature)
123 }
124 formats = append(formats, item)
125 }
126 sort.Strings(formats)
127 return strings.Join(formats, ", ")
128 }
129
130
131 func (lfr *logFormatRegistry) freeze() {
132 lfr.mutex.Lock()
133 defer lfr.mutex.Unlock()
134 lfr.frozen = true
135 }
136
View as plain text