Skip to content

Commit 45a230a

Browse files
authored
Merge pull request kubernetes#89683 from yuzhiquan/feature-implement-logging-format-flag
Implement logging format flag at component-base
2 parents d9f09b9 + c601792 commit 45a230a

File tree

4 files changed

+155
-1
lines changed

4 files changed

+155
-1
lines changed

staging/src/k8s.io/component-base/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ go 1.13
66

77
require (
88
github.com/blang/semver v3.5.0+incompatible
9+
github.com/go-logr/logr v0.1.0
910
github.com/google/go-cmp v0.4.0
1011
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
1112
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect

staging/src/k8s.io/component-base/logs/BUILD

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ load(
77

88
go_library(
99
name = "go_default_library",
10-
srcs = ["logs.go"],
10+
srcs = [
11+
"logs.go",
12+
"options.go",
13+
"registry.go",
14+
],
1115
importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/logs",
1216
importpath = "k8s.io/component-base/logs",
1317
deps = [
1418
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
19+
"//vendor/github.com/go-logr/logr:go_default_library",
1520
"//vendor/github.com/spf13/pflag:go_default_library",
1621
"//vendor/k8s.io/klog/v2:go_default_library",
1722
],
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package logs
18+
19+
import (
20+
"fmt"
21+
"github.com/go-logr/logr"
22+
"github.com/spf13/pflag"
23+
"k8s.io/klog/v2"
24+
)
25+
26+
const (
27+
logFormatFlagName = "logging-format"
28+
defaultLogFormat = "text"
29+
)
30+
31+
// Options has klog format parameters
32+
type Options struct {
33+
LogFormat string
34+
}
35+
36+
// NewOptions return new klog options
37+
func NewOptions() *Options {
38+
return &Options{
39+
LogFormat: defaultLogFormat,
40+
}
41+
}
42+
43+
// Validate check LogFormat in registry or not
44+
func (o *Options) Validate() []error {
45+
if _, err := o.Get(); err != nil {
46+
return []error{fmt.Errorf("unsupported log format: %s", o.LogFormat)}
47+
}
48+
return nil
49+
}
50+
51+
// AddFlags add logging-format flag
52+
func (o *Options) AddFlags(fs *pflag.FlagSet) {
53+
fs.StringVar(&o.LogFormat, logFormatFlagName, defaultLogFormat, "Set log format")
54+
}
55+
56+
// Apply set klog logger from LogFormat type
57+
func (o *Options) Apply() {
58+
// if log format not exists, use nil loggr
59+
loggr, _ := o.Get()
60+
61+
klog.SetLogger(loggr)
62+
}
63+
64+
// Get logger with LogFormat field
65+
func (o *Options) Get() (logr.Logger, error) {
66+
return logRegistry.Get(o.LogFormat)
67+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package logs
18+
19+
import (
20+
"fmt"
21+
"sync"
22+
23+
"github.com/go-logr/logr"
24+
)
25+
26+
var logRegistry = NewLogFormatRegistry()
27+
28+
// LogFormatRegistry store klog format registry
29+
type LogFormatRegistry struct {
30+
registry map[string]logr.Logger
31+
mu sync.Mutex
32+
}
33+
34+
// NewLogFormatRegistry return new init LogFormatRegistry struct
35+
func NewLogFormatRegistry() *LogFormatRegistry {
36+
return &LogFormatRegistry{
37+
registry: make(map[string]logr.Logger),
38+
mu: sync.Mutex{},
39+
}
40+
}
41+
42+
// Register new log format registry to global logRegistry
43+
func (lfr *LogFormatRegistry) Register(name string, logger logr.Logger) error {
44+
lfr.mu.Lock()
45+
defer lfr.mu.Unlock()
46+
if _, ok := lfr.registry[name]; ok {
47+
return fmt.Errorf("log format: %s already exists", name)
48+
}
49+
lfr.registry[name] = logger
50+
return nil
51+
}
52+
53+
// Get specified log format logger
54+
func (lfr *LogFormatRegistry) Get(name string) (logr.Logger, error) {
55+
lfr.mu.Lock()
56+
defer lfr.mu.Unlock()
57+
re, ok := lfr.registry[name]
58+
if !ok {
59+
return nil, fmt.Errorf("log format: %s does not exists", name)
60+
}
61+
return re, nil
62+
}
63+
64+
// Set specified log format logger
65+
func (lfr *LogFormatRegistry) Set(name string, logger logr.Logger) {
66+
lfr.mu.Lock()
67+
defer lfr.mu.Unlock()
68+
lfr.registry[name] = logger
69+
}
70+
71+
// Delete specified log format logger
72+
func (lfr *LogFormatRegistry) Delete(name string) {
73+
lfr.mu.Lock()
74+
defer lfr.mu.Unlock()
75+
delete(lfr.registry, name)
76+
}
77+
78+
func init() {
79+
// Text format is default klog format
80+
logRegistry.Register("text", nil)
81+
}

0 commit comments

Comments
 (0)