k3s/vendor/k8s.io/component-base/logs/options.go

67 lines
1.7 KiB
Go
Raw Normal View History

2020-08-10 17:43:49 +00:00
/*
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 (
"github.com/spf13/pflag"
"k8s.io/klog/v2"
"k8s.io/component-base/config"
"k8s.io/component-base/config/v1alpha1"
"k8s.io/component-base/logs/sanitization"
2020-08-10 17:43:49 +00:00
)
// Options has klog format parameters
type Options struct {
Config config.LoggingConfiguration
2020-08-10 17:43:49 +00:00
}
// NewOptions return new klog options
func NewOptions() *Options {
c := v1alpha1.LoggingConfiguration{}
v1alpha1.RecommendedLoggingConfiguration(&c)
o := &Options{}
v1alpha1.Convert_v1alpha1_LoggingConfiguration_To_config_LoggingConfiguration(&c, &o.Config, nil)
return o
2020-08-10 17:43:49 +00:00
}
// Validate verifies if any unsupported flag is set
// for non-default logging format
func (o *Options) Validate() []error {
errs := ValidateLoggingConfiguration(&o.Config, nil)
if len(errs) != 0 {
return errs.ToAggregate().Errors()
2020-08-10 17:43:49 +00:00
}
return nil
2020-08-10 17:43:49 +00:00
}
// AddFlags add logging-format flag
func (o *Options) AddFlags(fs *pflag.FlagSet) {
BindLoggingFlags(&o.Config, fs)
2020-08-10 17:43:49 +00:00
}
// Apply set klog logger from LogFormat type
func (o *Options) Apply() {
// if log format not exists, use nil loggr
loggr, _ := LogRegistry.Get(o.Config.Format)
2020-08-10 17:43:49 +00:00
klog.SetLogger(loggr)
if o.Config.Sanitization {
klog.SetLogFilter(&sanitization.SanitizingFilter{})
}
2020-08-10 17:43:49 +00:00
}