mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
Merge pull request #92177 from serathius/help
Update logging format flag help
This commit is contained in:
commit
e784d0d3a7
@ -17,9 +17,13 @@ limitations under the License.
|
||||
package logs
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
@ -28,6 +32,12 @@ const (
|
||||
defaultLogFormat = "text"
|
||||
)
|
||||
|
||||
// List of logs (k8s.io/klog + k8s.io/component-base/logs) flags supported by all logging formats
|
||||
var supportedLogsFlags = map[string]struct{}{
|
||||
"v": {},
|
||||
"vmodule": {},
|
||||
}
|
||||
|
||||
// Options has klog format parameters
|
||||
type Options struct {
|
||||
LogFormat string
|
||||
@ -50,7 +60,9 @@ func (o *Options) Validate() []error {
|
||||
|
||||
// AddFlags add logging-format flag
|
||||
func (o *Options) AddFlags(fs *pflag.FlagSet) {
|
||||
fs.StringVar(&o.LogFormat, logFormatFlagName, defaultLogFormat, "Set log format")
|
||||
unsupportedFlags := fmt.Sprintf("--%s", strings.Join(unsupportedLoggingFlags(), ", --"))
|
||||
formats := fmt.Sprintf(`"%s"`, strings.Join(logRegistry.List(), `", "`))
|
||||
fs.StringVar(&o.LogFormat, logFormatFlagName, defaultLogFormat, fmt.Sprintf("Sets the log format. Permitted formats: %s.\nNon-default formats don't honor these flags: %s.\nNon-default choices are currently alpha and subject to change without warning.", formats, unsupportedFlags))
|
||||
}
|
||||
|
||||
// Apply set klog logger from LogFormat type
|
||||
@ -65,3 +77,26 @@ func (o *Options) Apply() {
|
||||
func (o *Options) Get() (logr.Logger, error) {
|
||||
return logRegistry.Get(o.LogFormat)
|
||||
}
|
||||
|
||||
func unsupportedLoggingFlags() []string {
|
||||
allFlags := []string{}
|
||||
|
||||
// k8s.io/klog flags
|
||||
fs := &flag.FlagSet{}
|
||||
klog.InitFlags(fs)
|
||||
fs.VisitAll(func(flag *flag.Flag) {
|
||||
if _, found := supportedLogsFlags[flag.Name]; !found {
|
||||
allFlags = append(allFlags, flag.Name)
|
||||
}
|
||||
})
|
||||
|
||||
// k8s.io/component-base/logs flags
|
||||
pfs := &pflag.FlagSet{}
|
||||
AddFlags(pfs)
|
||||
pfs.VisitAll(func(flag *pflag.Flag) {
|
||||
if _, found := supportedLogsFlags[flag.Name]; !found {
|
||||
allFlags = append(allFlags, flag.Name)
|
||||
}
|
||||
})
|
||||
return allFlags
|
||||
}
|
||||
|
@ -80,6 +80,17 @@ func (lfr *LogFormatRegistry) Delete(name string) {
|
||||
delete(lfr.registry, name)
|
||||
}
|
||||
|
||||
// List names of registered log formats
|
||||
func (lfr *LogFormatRegistry) List() []string {
|
||||
lfr.mu.Lock()
|
||||
defer lfr.mu.Unlock()
|
||||
formats := make([]string, 0, len(lfr.registry))
|
||||
for f := range lfr.registry {
|
||||
formats = append(formats, f)
|
||||
}
|
||||
return formats
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Text format is default klog format
|
||||
logRegistry.Register(defaultLogFormat, nil)
|
||||
|
Loading…
Reference in New Issue
Block a user