mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 12:15:52 +00:00
[feature] implement logging format flag at component-base
refactor registry and options file refactor with review comment fix gofmt and golint error run update vendor script, and refactor code add options get method remove invoke with logs, and log format not a global flag fix typo update vendor
This commit is contained in:
parent
c6147e3231
commit
c60179260c
@ -6,6 +6,7 @@ go 1.13
|
||||
|
||||
require (
|
||||
github.com/blang/semver v3.5.0+incompatible
|
||||
github.com/go-logr/logr v0.1.0
|
||||
github.com/google/go-cmp v0.4.0
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
|
||||
|
@ -7,11 +7,16 @@ load(
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["logs.go"],
|
||||
srcs = [
|
||||
"logs.go",
|
||||
"options.go",
|
||||
"registry.go",
|
||||
],
|
||||
importmap = "k8s.io/kubernetes/vendor/k8s.io/component-base/logs",
|
||||
importpath = "k8s.io/component-base/logs",
|
||||
deps = [
|
||||
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
||||
"//vendor/github.com/go-logr/logr:go_default_library",
|
||||
"//vendor/github.com/spf13/pflag:go_default_library",
|
||||
"//vendor/k8s.io/klog/v2:go_default_library",
|
||||
],
|
||||
|
67
staging/src/k8s.io/component-base/logs/options.go
Normal file
67
staging/src/k8s.io/component-base/logs/options.go
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
Copyright 2020 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package logs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/spf13/pflag"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
logFormatFlagName = "logging-format"
|
||||
defaultLogFormat = "text"
|
||||
)
|
||||
|
||||
// Options has klog format parameters
|
||||
type Options struct {
|
||||
LogFormat string
|
||||
}
|
||||
|
||||
// NewOptions return new klog options
|
||||
func NewOptions() *Options {
|
||||
return &Options{
|
||||
LogFormat: defaultLogFormat,
|
||||
}
|
||||
}
|
||||
|
||||
// Validate check LogFormat in registry or not
|
||||
func (o *Options) Validate() []error {
|
||||
if _, err := o.Get(); err != nil {
|
||||
return []error{fmt.Errorf("unsupported log format: %s", o.LogFormat)}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddFlags add logging-format flag
|
||||
func (o *Options) AddFlags(fs *pflag.FlagSet) {
|
||||
fs.StringVar(&o.LogFormat, logFormatFlagName, defaultLogFormat, "Set log format")
|
||||
}
|
||||
|
||||
// Apply set klog logger from LogFormat type
|
||||
func (o *Options) Apply() {
|
||||
// if log format not exists, use nil loggr
|
||||
loggr, _ := o.Get()
|
||||
|
||||
klog.SetLogger(loggr)
|
||||
}
|
||||
|
||||
// Get logger with LogFormat field
|
||||
func (o *Options) Get() (logr.Logger, error) {
|
||||
return logRegistry.Get(o.LogFormat)
|
||||
}
|
81
staging/src/k8s.io/component-base/logs/registry.go
Normal file
81
staging/src/k8s.io/component-base/logs/registry.go
Normal file
@ -0,0 +1,81 @@
|
||||
/*
|
||||
Copyright 2020 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package logs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
)
|
||||
|
||||
var logRegistry = NewLogFormatRegistry()
|
||||
|
||||
// LogFormatRegistry store klog format registry
|
||||
type LogFormatRegistry struct {
|
||||
registry map[string]logr.Logger
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// NewLogFormatRegistry return new init LogFormatRegistry struct
|
||||
func NewLogFormatRegistry() *LogFormatRegistry {
|
||||
return &LogFormatRegistry{
|
||||
registry: make(map[string]logr.Logger),
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
// Register new log format registry to global logRegistry
|
||||
func (lfr *LogFormatRegistry) Register(name string, logger logr.Logger) error {
|
||||
lfr.mu.Lock()
|
||||
defer lfr.mu.Unlock()
|
||||
if _, ok := lfr.registry[name]; ok {
|
||||
return fmt.Errorf("log format: %s already exists", name)
|
||||
}
|
||||
lfr.registry[name] = logger
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get specified log format logger
|
||||
func (lfr *LogFormatRegistry) Get(name string) (logr.Logger, error) {
|
||||
lfr.mu.Lock()
|
||||
defer lfr.mu.Unlock()
|
||||
re, ok := lfr.registry[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("log format: %s does not exists", name)
|
||||
}
|
||||
return re, nil
|
||||
}
|
||||
|
||||
// Set specified log format logger
|
||||
func (lfr *LogFormatRegistry) Set(name string, logger logr.Logger) {
|
||||
lfr.mu.Lock()
|
||||
defer lfr.mu.Unlock()
|
||||
lfr.registry[name] = logger
|
||||
}
|
||||
|
||||
// Delete specified log format logger
|
||||
func (lfr *LogFormatRegistry) Delete(name string) {
|
||||
lfr.mu.Lock()
|
||||
defer lfr.mu.Unlock()
|
||||
delete(lfr.registry, name)
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Text format is default klog format
|
||||
logRegistry.Register("text", nil)
|
||||
}
|
Loading…
Reference in New Issue
Block a user