updated vendor

This commit is contained in:
2026-06-16 08:02:19 +02:00
parent 2f7f99d3f0
commit 77299d0c64
1283 changed files with 67302 additions and 208958 deletions
-2
View File
@@ -48,8 +48,6 @@ How to use klog
- For more logging conventions (See [Logging Conventions](https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md))
- See our documentation on [pkg.go.dev/k8s.io](https://pkg.go.dev/k8s.io/klog).
**NOTE**: please use the newer go versions that support semantic import versioning in modules, ideally go 1.11.4 or greater.
### Coexisting with klog/v2
See [this example](examples/coexist_klog_v1_and_v2/) to see how to coexist with both klog/v1 and klog/v2.
+131 -111
View File
@@ -20,7 +20,9 @@ import (
"bytes"
"encoding/json"
"fmt"
"slices"
"strconv"
"strings"
"github.com/go-logr/logr"
)
@@ -51,139 +53,157 @@ func WithValues(oldKV, newKV []interface{}) []interface{} {
return kv
}
// MergeKVs deduplicates elements provided in two key/value slices.
//
// Keys in each slice are expected to be unique, so duplicates can only occur
// when the first and second slice contain the same key. When that happens, the
// key/value pair from the second slice is used. The first slice must be well-formed
// (= even key/value pairs). The second one may have a missing value, in which
// case the special "missing value" is added to the result.
func MergeKVs(first, second []interface{}) []interface{} {
maxLength := len(first) + (len(second)+1)/2*2
if maxLength == 0 {
// Nothing to do at all.
return nil
}
if len(first) == 0 && len(second)%2 == 0 {
// Nothing to be overridden, second slice is well-formed
// and can be used directly.
return second
}
// Determine which keys are in the second slice so that we can skip
// them when iterating over the first one. The code intentionally
// favors performance over completeness: we assume that keys are string
// constants and thus compare equal when the string values are equal. A
// string constant being overridden by, for example, a fmt.Stringer is
// not handled.
overrides := map[interface{}]bool{}
for i := 0; i < len(second); i += 2 {
overrides[second[i]] = true
}
merged := make([]interface{}, 0, maxLength)
for i := 0; i+1 < len(first); i += 2 {
key := first[i]
if overrides[key] {
continue
}
merged = append(merged, key, first[i+1])
}
merged = append(merged, second...)
if len(merged)%2 != 0 {
merged = append(merged, missingValue)
}
return merged
}
type Formatter struct {
AnyToStringHook AnyToStringFunc
}
type AnyToStringFunc func(v interface{}) string
// MergeKVsInto is a variant of MergeKVs which directly formats the key/value
// pairs into a buffer.
func (f Formatter) MergeAndFormatKVs(b *bytes.Buffer, first, second []interface{}) {
if len(first) == 0 && len(second) == 0 {
// Nothing to do at all.
return
}
if len(first) == 0 && len(second)%2 == 0 {
// Nothing to be overridden, second slice is well-formed
// and can be used directly.
for i := 0; i < len(second); i += 2 {
f.KVFormat(b, second[i], second[i+1])
}
return
}
// Determine which keys are in the second slice so that we can skip
// them when iterating over the first one. The code intentionally
// favors performance over completeness: we assume that keys are string
// constants and thus compare equal when the string values are equal. A
// string constant being overridden by, for example, a fmt.Stringer is
// not handled.
overrides := map[interface{}]bool{}
for i := 0; i < len(second); i += 2 {
overrides[second[i]] = true
}
for i := 0; i < len(first); i += 2 {
key := first[i]
if overrides[key] {
continue
}
f.KVFormat(b, key, first[i+1])
}
// Round down.
l := len(second)
l = l / 2 * 2
for i := 1; i < l; i += 2 {
f.KVFormat(b, second[i-1], second[i])
}
if len(second)%2 == 1 {
f.KVFormat(b, second[len(second)-1], missingValue)
}
}
func MergeAndFormatKVs(b *bytes.Buffer, first, second []interface{}) {
Formatter{}.MergeAndFormatKVs(b, first, second)
}
const missingValue = "(MISSING)"
// KVListFormat serializes all key/value pairs into the provided buffer.
// A space gets inserted before the first pair and between each pair.
func (f Formatter) KVListFormat(b *bytes.Buffer, keysAndValues ...interface{}) {
for i := 0; i < len(keysAndValues); i += 2 {
var v interface{}
k := keysAndValues[i]
if i+1 < len(keysAndValues) {
v = keysAndValues[i+1]
} else {
v = missingValue
func FormatKVs(b *bytes.Buffer, kvs ...[]interface{}) {
Formatter{}.FormatKVs(b, kvs...)
}
// FormatKVs formats all key/value pairs such that the output contains no
// duplicates ("last one wins").
func (f Formatter) FormatKVs(b *bytes.Buffer, kvs ...[]interface{}) {
// De-duplication is done by optimistically formatting all key value
// pairs and then cutting out the output of those key/value pairs which
// got overwritten later.
//
// In the common case of no duplicates, the only overhead is tracking
// previous keys. This uses a slice with a simple linear search because
// the number of entries is typically so low that allocating a map or
// keeping a sorted slice with binary search aren't justified.
//
// Using a fixed size here makes the Go compiler use the stack as
// initial backing store for the slice, which is crucial for
// performance.
existing := make([]obsoleteKV, 0, 32)
obsolete := make([]interval, 0, 32) // Sorted by start index.
for _, keysAndValues := range kvs {
for i := 0; i < len(keysAndValues); i += 2 {
var v interface{}
k := keysAndValues[i]
if i+1 < len(keysAndValues) {
v = keysAndValues[i+1]
} else {
v = missingValue
}
var e obsoleteKV
e.start = b.Len()
e.key = f.KVFormat(b, k, v)
e.end = b.Len()
i := findObsoleteEntry(existing, e.key)
if i >= 0 {
data := b.Bytes()
if bytes.Compare(data[existing[i].start:existing[i].end], data[e.start:e.end]) == 0 {
// The new entry gets obsoleted because it's identical.
// This has the advantage that key/value pairs from
// a WithValues call always come first, even if the same
// pair gets added again later. This makes different log
// entries more consistent.
//
// The new entry has a higher start index and thus can be appended.
obsolete = append(obsolete, e.interval)
} else {
// The old entry gets obsoleted because it's value is different.
//
// Sort order is not guaranteed, we have to insert at the right place.
index, _ := slices.BinarySearchFunc(obsolete, existing[i].interval, func(a, b interval) int { return a.start - b.start })
obsolete = slices.Insert(obsolete, index, existing[i].interval)
existing[i].interval = e.interval
}
} else {
// Instead of appending at the end and doing a
// linear search in findEntry, we could keep
// the slice sorted by key and do a binary search.
//
// Above:
// i, ok := slices.BinarySearchFunc(existing, e, func(a, b entry) int { return strings.Compare(a.key, b.key) })
// Here:
// existing = slices.Insert(existing, i, e)
//
// But that adds a dependency on the slices package
// and made performance slightly worse, presumably
// because the cost of shifting entries around
// did not pay of with faster lookups.
existing = append(existing, e)
}
}
f.KVFormat(b, k, v)
}
// If we need to remove some obsolete key/value pairs then move the memory.
if len(obsolete) > 0 {
// Potentially the next remaining output (might itself be obsolete).
from := obsolete[0].end
// Next obsolete entry.
nextObsolete := 1
// This is the source buffer, before truncation.
all := b.Bytes()
b.Truncate(obsolete[0].start)
for nextObsolete < len(obsolete) {
if from == obsolete[nextObsolete].start {
// Skip also the next obsolete key/value.
from = obsolete[nextObsolete].end
nextObsolete++
continue
}
// Preserve some output. Write uses copy, which
// explicitly allows source and destination to overlap.
// That could happen here.
valid := all[from:obsolete[nextObsolete].start]
b.Write(valid)
from = obsolete[nextObsolete].end
nextObsolete++
}
// Copy end of buffer.
valid := all[from:]
b.Write(valid)
}
}
func KVListFormat(b *bytes.Buffer, keysAndValues ...interface{}) {
Formatter{}.KVListFormat(b, keysAndValues...)
type obsoleteKV struct {
key string
interval
}
func KVFormat(b *bytes.Buffer, k, v interface{}) {
Formatter{}.KVFormat(b, k, v)
// interval includes the start and excludes the end.
type interval struct {
start int
end int
}
func findObsoleteEntry(entries []obsoleteKV, key string) int {
for i, entry := range entries {
if entry.key == key {
return i
}
}
return -1
}
// formatAny is the fallback formatter for a value. It supports a hook (for
// example, for YAML encoding) and itself uses JSON encoding.
func (f Formatter) formatAny(b *bytes.Buffer, v interface{}) {
b.WriteRune('=')
if f.AnyToStringHook != nil {
b.WriteString(f.AnyToStringHook(v))
str := f.AnyToStringHook(v)
if strings.Contains(str, "\n") {
// If it's multi-line, then pass it through writeStringValue to get start/end delimiters,
// which separates it better from any following key/value pair.
writeStringValue(b, str)
return
}
// Otherwise put it directly after the separator, on the same lime,
// The assumption is that the hook returns something where start/end are obvious.
b.WriteRune('=')
b.WriteString(str)
return
}
b.WriteRune('=')
formatAsJSON(b, v)
}
+7 -3
View File
@@ -28,7 +28,7 @@ import (
// KVFormat serializes one key/value pair into the provided buffer.
// A space gets inserted before the pair.
func (f Formatter) KVFormat(b *bytes.Buffer, k, v interface{}) {
func (f Formatter) KVFormat(b *bytes.Buffer, k, v interface{}) string {
// This is the version without slog support. Must be kept in sync with
// the version in keyvalues_slog.go.
@@ -37,13 +37,15 @@ func (f Formatter) KVFormat(b *bytes.Buffer, k, v interface{}) {
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/migration-to-structured-logging.md#name-arguments
// for the sake of performance. Keys with spaces,
// special characters, etc. will break parsing.
var key string
if sK, ok := k.(string); ok {
// Avoid one allocation when the key is a string, which
// normally it should be.
b.WriteString(sK)
key = sK
} else {
b.WriteString(fmt.Sprintf("%s", k))
key = fmt.Sprintf("%s", k)
}
b.WriteString(key)
// The type checks are sorted so that more frequently used ones
// come first because that is then faster in the common
@@ -94,4 +96,6 @@ func (f Formatter) KVFormat(b *bytes.Buffer, k, v interface{}) {
default:
f.formatAny(b, v)
}
return key
}
+8 -4
View File
@@ -29,8 +29,8 @@ import (
)
// KVFormat serializes one key/value pair into the provided buffer.
// A space gets inserted before the pair.
func (f Formatter) KVFormat(b *bytes.Buffer, k, v interface{}) {
// A space gets inserted before the pair. It returns the key.
func (f Formatter) KVFormat(b *bytes.Buffer, k, v interface{}) string {
// This is the version without slog support. Must be kept in sync with
// the version in keyvalues_slog.go.
@@ -39,13 +39,15 @@ func (f Formatter) KVFormat(b *bytes.Buffer, k, v interface{}) {
// https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/migration-to-structured-logging.md#name-arguments
// for the sake of performance. Keys with spaces,
// special characters, etc. will break parsing.
var key string
if sK, ok := k.(string); ok {
// Avoid one allocation when the key is a string, which
// normally it should be.
b.WriteString(sK)
key = sK
} else {
b.WriteString(fmt.Sprintf("%s", k))
key = fmt.Sprintf("%s", k)
}
b.WriteString(key)
// The type checks are sorted so that more frequently used ones
// come first because that is then faster in the common
@@ -112,6 +114,8 @@ func (f Formatter) KVFormat(b *bytes.Buffer, k, v interface{}) {
default:
f.formatAny(b, v)
}
return key
}
// generateJSON has the same preference for plain strings as KVFormat.
+303
View File
@@ -0,0 +1,303 @@
/*
Copyright 2013 Google Inc. All Rights Reserved.
Copyright 2022 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 verbosity
import (
"bytes"
"errors"
"flag"
"fmt"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
)
// New returns a struct that implements -v and -vmodule support. Changing and
// checking these settings is thread-safe, with all concurrency issues handled
// internally.
func New() *VState {
vs := new(VState)
// The two fields must have a pointer to the overal struct for their
// implementation of Set.
vs.vmodule.vs = vs
vs.verbosity.vs = vs
return vs
}
// Value is an extension that makes it possible to use the values in pflag.
type Value interface {
flag.Value
Type() string
}
func (vs *VState) V() Value {
return &vs.verbosity
}
func (vs *VState) VModule() Value {
return &vs.vmodule
}
// VState contains settings and state. Some of its fields can be accessed
// through atomic read/writes, in other cases a mutex must be held.
type VState struct {
mu sync.Mutex
// These flags are modified only under lock, although verbosity may be fetched
// safely using atomic.LoadInt32.
vmodule moduleSpec // The state of the -vmodule flag.
verbosity levelSpec // V logging level, the value of the -v flag/
// pcs is used in V to avoid an allocation when computing the caller's PC.
pcs [1]uintptr
// vmap is a cache of the V Level for each V() call site, identified by PC.
// It is wiped whenever the vmodule flag changes state.
vmap map[uintptr]Level
// filterLength stores the length of the vmodule filter chain. If greater
// than zero, it means vmodule is enabled. It may be read safely
// using sync.LoadInt32, but is only modified under mu.
filterLength int32
}
// Level must be an int32 to support atomic read/writes.
type Level int32
type levelSpec struct {
vs *VState
l Level
}
// get returns the value of the level.
func (l *levelSpec) get() Level {
return Level(atomic.LoadInt32((*int32)(&l.l)))
}
// set sets the value of the level.
func (l *levelSpec) set(val Level) {
atomic.StoreInt32((*int32)(&l.l), int32(val))
}
// String is part of the flag.Value interface.
func (l *levelSpec) String() string {
return strconv.FormatInt(int64(l.l), 10)
}
// Get is part of the flag.Getter interface. It returns the
// verbosity level as int32.
func (l *levelSpec) Get() interface{} {
return int32(l.l)
}
// Type is part of pflag.Value.
func (l *levelSpec) Type() string {
return "Level"
}
// Set is part of the flag.Value interface.
func (l *levelSpec) Set(value string) error {
v, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return err
}
l.vs.mu.Lock()
defer l.vs.mu.Unlock()
l.vs.set(Level(v), l.vs.vmodule.filter, false)
return nil
}
// moduleSpec represents the setting of the -vmodule flag.
type moduleSpec struct {
vs *VState
filter []modulePat
}
// modulePat contains a filter for the -vmodule flag.
// It holds a verbosity level and a file pattern to match.
type modulePat struct {
pattern string
literal bool // The pattern is a literal string
level Level
}
// match reports whether the file matches the pattern. It uses a string
// comparison if the pattern contains no metacharacters.
func (m *modulePat) match(file string) bool {
if m.literal {
return file == m.pattern
}
match, _ := filepath.Match(m.pattern, file)
return match
}
func (m *moduleSpec) String() string {
// Lock because the type is not atomic. TODO: clean this up.
// Empty instances don't have and don't need a lock (can
// happen when flag uses introspection).
if m.vs != nil {
m.vs.mu.Lock()
defer m.vs.mu.Unlock()
}
var b bytes.Buffer
for i, f := range m.filter {
if i > 0 {
b.WriteRune(',')
}
fmt.Fprintf(&b, "%s=%d", f.pattern, f.level)
}
return b.String()
}
// Get is part of the (Go 1.2) flag.Getter interface. It always returns nil for this flag type since the
// struct is not exported.
func (m *moduleSpec) Get() interface{} {
return nil
}
// Type is part of pflag.Value
func (m *moduleSpec) Type() string {
return "pattern=N,..."
}
var errVmoduleSyntax = errors.New("syntax error: expect comma-separated list of filename=N")
// Set will sets module value
// Syntax: -vmodule=recordio=2,file=1,gfs*=3
func (m *moduleSpec) Set(value string) error {
var filter []modulePat
for _, pat := range strings.Split(value, ",") {
if len(pat) == 0 {
// Empty strings such as from a trailing comma can be ignored.
continue
}
patLev := strings.Split(pat, "=")
if len(patLev) != 2 || len(patLev[0]) == 0 || len(patLev[1]) == 0 {
return errVmoduleSyntax
}
pattern := patLev[0]
v, err := strconv.ParseInt(patLev[1], 10, 32)
if err != nil {
return errors.New("syntax error: expect comma-separated list of filename=N")
}
if v < 0 {
return errors.New("negative value for vmodule level")
}
if v == 0 {
continue // Ignore. It's harmless but no point in paying the overhead.
}
// TODO: check syntax of filter?
filter = append(filter, modulePat{pattern, isLiteral(pattern), Level(v)})
}
m.vs.mu.Lock()
defer m.vs.mu.Unlock()
m.vs.set(m.vs.verbosity.l, filter, true)
return nil
}
// isLiteral reports whether the pattern is a literal string, that is, has no metacharacters
// that require filepath.Match to be called to match the pattern.
func isLiteral(pattern string) bool {
return !strings.ContainsAny(pattern, `\*?[]`)
}
// set sets a consistent state for V logging.
// The mutex must be held.
func (vs *VState) set(l Level, filter []modulePat, setFilter bool) {
// Turn verbosity off so V will not fire while we are in transition.
vs.verbosity.set(0)
// Ditto for filter length.
atomic.StoreInt32(&vs.filterLength, 0)
// Set the new filters and wipe the pc->Level map if the filter has changed.
if setFilter {
vs.vmodule.filter = filter
vs.vmap = make(map[uintptr]Level)
}
// Things are consistent now, so enable filtering and verbosity.
// They are enabled in order opposite to that in V.
atomic.StoreInt32(&vs.filterLength, int32(len(filter)))
vs.verbosity.set(l)
}
// Enabled checks whether logging is enabled at the given level. This must be
// called with depth=0 when the caller of enabled will do the logging and
// higher values when more stack levels need to be skipped.
//
// The mutex will be locked only if needed.
func (vs *VState) Enabled(level Level, depth int) bool {
// This function tries hard to be cheap unless there's work to do.
// The fast path is two atomic loads and compares.
// Here is a cheap but safe test to see if V logging is enabled globally.
if vs.verbosity.get() >= level {
return true
}
// It's off globally but vmodule may still be set.
// Here is another cheap but safe test to see if vmodule is enabled.
if atomic.LoadInt32(&vs.filterLength) > 0 {
// Now we need a proper lock to use the logging structure. The pcs field
// is shared so we must lock before accessing it. This is fairly expensive,
// but if V logging is enabled we're slow anyway.
vs.mu.Lock()
defer vs.mu.Unlock()
if runtime.Callers(depth+2, vs.pcs[:]) == 0 {
return false
}
// runtime.Callers returns "return PCs", but we want
// to look up the symbolic information for the call,
// so subtract 1 from the PC. runtime.CallersFrames
// would be cleaner, but allocates.
pc := vs.pcs[0] - 1
v, ok := vs.vmap[pc]
if !ok {
v = vs.setV(pc)
}
return v >= level
}
return false
}
// setV computes and remembers the V level for a given PC
// when vmodule is enabled.
// File pattern matching takes the basename of the file, stripped
// of its .go suffix, and uses filepath.Match, which is a little more
// general than the *? matching used in C++.
// Mutex is held.
func (vs *VState) setV(pc uintptr) Level {
fn := runtime.FuncForPC(pc)
file, _ := fn.FileLine(pc)
// The file is something like /a/b/c/d.go. We want just the d.
file = strings.TrimSuffix(file, ".go")
if slash := strings.LastIndex(file, "/"); slash >= 0 {
file = file[slash+1:]
}
for _, filter := range vs.vmodule.filter {
if filter.match(file) {
vs.vmap[pc] = filter.level
return filter.level
}
}
vs.vmap[pc] = 0
return 0
}
+66 -21
View File
@@ -58,15 +58,30 @@
//
// -logtostderr=true
// Logs are written to standard error instead of to files.
// This shortcuts most of the usual output routing:
// -alsologtostderr, -stderrthreshold and -log_dir have no
// effect and output redirection at runtime with SetOutput is
// ignored.
// By default, all logs are written regardless of severity
// (legacy behavior). To filter logs by severity when
// -logtostderr=true, set -legacy_stderr_threshold_behavior=false
// and use -stderrthreshold.
// With -legacy_stderr_threshold_behavior=true,
// -stderrthreshold has no effect.
//
// The following flags always have no effect:
// -alsologtostderr, -alsologtostderrthreshold, and -log_dir.
// Output redirection at runtime with SetOutput is also ignored.
// -alsologtostderr=false
// Logs are written to standard error as well as to files.
// -alsologtostderrthreshold=INFO
// Log events at or above this severity are logged to standard
// error when -alsologtostderr=true (no effect when -logtostderr=true).
// Default is INFO to maintain backward compatibility.
// -stderrthreshold=ERROR
// Log events at or above this severity are logged to standard
// error as well as to files.
// error as well as to files. When -logtostderr=true, this flag
// has no effect unless -legacy_stderr_threshold_behavior=false.
// -legacy_stderr_threshold_behavior=true
// If true, -stderrthreshold is ignored when -logtostderr=true
// (legacy behavior). If false, -stderrthreshold is honored even
// when -logtostderr=true, allowing severity-based filtering.
// -log_dir=""
// Log files will be written to this directory instead of the
// default temporary directory.
@@ -156,7 +171,7 @@ func (s *severityValue) Set(value string) error {
}
threshold = severity.Severity(v)
}
logging.stderrThreshold.set(threshold)
s.set(threshold)
return nil
}
@@ -409,6 +424,15 @@ var commandLine flag.FlagSet
// init sets up the defaults and creates command line flags.
func init() {
// Initialize severity thresholds
logging.stderrThreshold = severityValue{
Severity: severity.ErrorLog, // Default stderrThreshold is ERROR.
}
logging.alsologtostderrthreshold = severityValue{
Severity: severity.InfoLog, // Default alsologtostderrthreshold is INFO (to maintain backward compatibility).
}
logging.setVState(0, nil, false)
commandLine.StringVar(&logging.logDir, "log_dir", "", "If non-empty, write log files in this directory (no effect when -logtostderr=true)")
commandLine.StringVar(&logging.logFile, "log_file", "", "If non-empty, use this log file (no effect when -logtostderr=true)")
commandLine.Uint64Var(&logging.logFileMaxSizeMB, "log_file_max_size", 1800,
@@ -416,16 +440,14 @@ func init() {
"If the value is 0, the maximum file size is unlimited.")
commandLine.BoolVar(&logging.toStderr, "logtostderr", true, "log to standard error instead of files")
commandLine.BoolVar(&logging.alsoToStderr, "alsologtostderr", false, "log to standard error as well as files (no effect when -logtostderr=true)")
logging.setVState(0, nil, false)
commandLine.BoolVar(&logging.legacyStderrThresholdBehavior, "legacy_stderr_threshold_behavior", true, "If true, stderrthreshold is ignored when logtostderr=true (legacy behavior). If false, stderrthreshold is honored even when logtostderr=true")
commandLine.Var(&logging.verbosity, "v", "number for the log level verbosity")
commandLine.BoolVar(&logging.addDirHeader, "add_dir_header", false, "If true, adds the file directory to the header of the log messages")
commandLine.BoolVar(&logging.skipHeaders, "skip_headers", false, "If true, avoid header prefixes in the log messages")
commandLine.BoolVar(&logging.oneOutput, "one_output", false, "If true, only write logs to their native severity level (vs also writing to each lower severity level; no effect when -logtostderr=true)")
commandLine.BoolVar(&logging.skipLogHeaders, "skip_log_headers", false, "If true, avoid headers when opening log files (no effect when -logtostderr=true)")
logging.stderrThreshold = severityValue{
Severity: severity.ErrorLog, // Default stderrThreshold is ERROR.
}
commandLine.Var(&logging.stderrThreshold, "stderrthreshold", "logs at or above this threshold go to stderr when writing to files and stderr (no effect when -logtostderr=true or -alsologtostderr=true)")
commandLine.Var(&logging.stderrThreshold, "stderrthreshold", "logs at or above this threshold go to stderr when writing to files and stderr (no effect when -logtostderr=true or -alsologtostderr=true unless -legacy_stderr_threshold_behavior=false)")
commandLine.Var(&logging.alsologtostderrthreshold, "alsologtostderrthreshold", "logs at or above this threshold go to stderr when -alsologtostderr=true (no effect when -logtostderr=true)")
commandLine.Var(&logging.vmodule, "vmodule", "comma-separated list of pattern=N settings for file-filtered logging")
commandLine.Var(&logging.traceLocation, "log_backtrace_at", "when logging hits line file:N, emit a stack trace")
@@ -470,11 +492,13 @@ type settings struct {
// Boolean flags. Not handled atomically because the flag.Value interface
// does not let us avoid the =true, and that shorthand is necessary for
// compatibility. TODO: does this matter enough to fix? Seems unlikely.
toStderr bool // The -logtostderr flag.
alsoToStderr bool // The -alsologtostderr flag.
toStderr bool // The -logtostderr flag.
alsoToStderr bool // The -alsologtostderr flag.
legacyStderrThresholdBehavior bool // The -legacy_stderr_threshold_behavior flag.
// Level flag. Handled atomically.
stderrThreshold severityValue // The -stderrthreshold flag.
stderrThreshold severityValue // The -stderrthreshold flag.
alsologtostderrthreshold severityValue // The -alsologtostderrthreshold flag.
// Access to all of the following fields must be protected via a mutex.
@@ -809,16 +833,21 @@ func (l *loggingT) infoS(logger *logWriter, filter LogFilter, depth int, msg str
// printS is called from infoS and errorS if logger is not specified.
// set log severity by s
func (l *loggingT) printS(err error, s severity.Severity, depth int, msg string, keysAndValues ...interface{}) {
// Only create a new buffer if we don't have one cached.
b := buffer.GetBuffer()
// The message is always quoted, even if it contains line breaks.
// If developers want multi-line output, they should use a small, fixed
// message and put the multi-line output into a value.
b.WriteString(strconv.Quote(msg))
qMsg := make([]byte, 0, 1024)
qMsg = strconv.AppendQuote(qMsg, msg)
// Only create a new buffer if we don't have one cached.
b := buffer.GetBuffer()
b.Write(qMsg)
var errKV []interface{}
if err != nil {
serialize.KVListFormat(&b.Buffer, "err", err)
errKV = []interface{}{"err", err}
}
serialize.KVListFormat(&b.Buffer, keysAndValues...)
serialize.FormatKVs(&b.Buffer, errKV, keysAndValues)
l.printDepth(s, nil, nil, depth+1, &b.Buffer)
// Make the buffer available for reuse.
buffer.PutBuffer(b)
@@ -885,9 +914,25 @@ func (l *loggingT) output(s severity.Severity, logger *logWriter, buf *buffer.Bu
}
}
} else if l.toStderr {
os.Stderr.Write(data)
// When logging to stderr only, check if we should filter by severity.
// This is controlled by the legacy_stderr_threshold_behavior flag.
if l.legacyStderrThresholdBehavior {
// Legacy behavior: always write to stderr, ignore stderrthreshold
os.Stderr.Write(data)
} else {
// New behavior: honor stderrthreshold even when logtostderr=true
if s >= l.stderrThreshold.get() {
os.Stderr.Write(data)
}
}
} else {
if alsoToStderr || l.alsoToStderr || s >= l.stderrThreshold.get() {
// Write to stderr if any of these conditions are met:
// - alsoToStderr is set (legacy behavior)
// - alsologtostderr is set and severity meets alsologtostderrthreshold
// - alsologtostderr is not set and severity meets stderrThreshold
if alsoToStderr ||
(l.alsoToStderr && s >= l.alsologtostderrthreshold.get()) ||
(!l.alsoToStderr && s >= l.stderrThreshold.get()) {
os.Stderr.Write(data)
}
+2 -2
View File
@@ -53,7 +53,7 @@ func (l *klogger) Init(info logr.RuntimeInfo) {
}
func (l *klogger) Info(level int, msg string, kvList ...interface{}) {
merged := serialize.MergeKVs(l.values, kvList)
merged := serialize.WithValues(l.values, kvList)
// Skip this function.
VDepth(l.callDepth+1, Level(level)).InfoSDepth(l.callDepth+1, msg, merged...)
}
@@ -63,7 +63,7 @@ func (l *klogger) Enabled(level int) bool {
}
func (l *klogger) Error(err error, msg string, kvList ...interface{}) {
merged := serialize.MergeKVs(l.values, kvList)
merged := serialize.WithValues(l.values, kvList)
ErrorSDepth(l.callDepth+1, err, msg, merged...)
}
+8 -3
View File
@@ -63,12 +63,17 @@ func slogOutput(file string, line int, now time.Time, err error, s severity.Seve
}
// See printS.
qMsg := make([]byte, 0, 1024)
qMsg = strconv.AppendQuote(qMsg, msg)
b := buffer.GetBuffer()
b.WriteString(strconv.Quote(msg))
b.Write(qMsg)
var errKV []interface{}
if err != nil {
serialize.KVListFormat(&b.Buffer, "err", err)
errKV = []interface{}{"err", err}
}
serialize.KVListFormat(&b.Buffer, kvList...)
serialize.FormatKVs(&b.Buffer, errKV, kvList)
// See print + header.
buf := logging.formatHeader(s, file, line, now)
+172
View File
@@ -0,0 +1,172 @@
/*
Copyright 2021 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 textlogger
import (
"flag"
"io"
"os"
"strconv"
"time"
"k8s.io/klog/v2/internal/verbosity"
)
// Config influences logging in a text logger. To make this configurable via
// command line flags, instantiate this once per program and use AddFlags to
// bind command line flags to the instance before passing it to NewTestContext.
//
// Must be constructed with NewConfig.
type Config struct {
vstate *verbosity.VState
co configOptions
}
// Verbosity returns a value instance that can be used to query (via String) or
// modify (via Set) the verbosity threshold. This is thread-safe and can be
// done at runtime.
func (c *Config) Verbosity() flag.Value {
return c.vstate.V()
}
// VModule returns a value instance that can be used to query (via String) or
// modify (via Set) the vmodule settings. This is thread-safe and can be done
// at runtime.
func (c *Config) VModule() flag.Value {
return c.vstate.VModule()
}
// ConfigOption implements functional parameters for NewConfig.
type ConfigOption func(co *configOptions)
type configOptions struct {
verbosityFlagName string
vmoduleFlagName string
verbosityDefault int
fixedTime *time.Time
unwind func(int) (string, int)
withHeader bool
output io.Writer
}
// VerbosityFlagName overrides the default -v for the verbosity level.
func VerbosityFlagName(name string) ConfigOption {
return func(co *configOptions) {
co.verbosityFlagName = name
}
}
// VModulFlagName overrides the default -vmodule for the per-module
// verbosity levels.
func VModuleFlagName(name string) ConfigOption {
return func(co *configOptions) {
co.vmoduleFlagName = name
}
}
// Verbosity overrides the default verbosity level of 0.
// See https://github.com/kubernetes/community/blob/9406b4352fe2d5810cb21cc3cb059ce5886de157/contributors/devel/sig-instrumentation/logging.md#logging-conventions
// for log level conventions in Kubernetes.
func Verbosity(level int) ConfigOption {
return func(co *configOptions) {
co.verbosityDefault = level
}
}
// Output overrides stderr as the output stream.
func Output(output io.Writer) ConfigOption {
return func(co *configOptions) {
co.output = output
}
}
// FixedTime overrides the actual time with a fixed time. Useful only for testing.
//
// # Experimental
//
// Notice: This function is EXPERIMENTAL and may be changed or removed in a
// later release.
func FixedTime(ts time.Time) ConfigOption {
return func(co *configOptions) {
co.fixedTime = &ts
}
}
// WithHeader controls whether the header (time, source code location, etc.)
// is included in the output. The default is to include it.
//
// This can be useful in combination with redirection to a buffer to
// turn structured log parameters into a string (see example).
//
// # Experimental
//
// Notice: This function is EXPERIMENTAL and may be changed or removed in a
// later release.
func WithHeader(enabled bool) ConfigOption {
return func(co *configOptions) {
co.withHeader = enabled
}
}
// Backtrace overrides the default mechanism for determining the call site.
// The callback is invoked with the number of function calls between itself
// and the call site. It must return the file name and line number. An empty
// file name indicates that the information is unknown.
//
// # Experimental
//
// Notice: This function is EXPERIMENTAL and may be changed or removed in a
// later release.
func Backtrace(unwind func(skip int) (filename string, line int)) ConfigOption {
return func(co *configOptions) {
co.unwind = unwind
}
}
// NewConfig returns a configuration with recommended defaults and optional
// modifications. Command line flags are not bound to any FlagSet yet.
func NewConfig(opts ...ConfigOption) *Config {
c := &Config{
vstate: verbosity.New(),
co: configOptions{
verbosityFlagName: "v",
vmoduleFlagName: "vmodule",
verbosityDefault: 0,
unwind: runtimeBacktrace,
withHeader: true,
output: os.Stderr,
},
}
for _, opt := range opts {
opt(&c.co)
}
// Cannot fail for this input.
_ = c.Verbosity().Set(strconv.FormatInt(int64(c.co.verbosityDefault), 10))
return c
}
// AddFlags registers the command line flags that control the configuration.
//
// The default flag names are the same as in klog, so unless those defaults
// are changed, either klog.InitFlags or Config.AddFlags can be used for the
// same flag set, but not both.
func (c *Config) AddFlags(fs *flag.FlagSet) {
fs.Var(c.Verbosity(), c.co.verbosityFlagName, "number for the log level verbosity of the testing logger")
fs.Var(c.VModule(), c.co.vmoduleFlagName, "comma-separated list of pattern=N log level settings for files matching the patterns")
}
+200
View File
@@ -0,0 +1,200 @@
/*
Copyright 2019 The Kubernetes Authors.
Copyright 2020 Intel Corporation.
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 textlogger contains an implementation of the logr interface which is
// producing the exact same output as klog. It does not route output through
// klog (i.e. ignores [k8s.io/klog/v2.InitFlags]). Instead, all settings must be
// configured through its own [NewConfig] and [Config.AddFlags].
package textlogger
import (
"runtime"
"strconv"
"strings"
"time"
"github.com/go-logr/logr"
"k8s.io/klog/v2/internal/buffer"
"k8s.io/klog/v2/internal/serialize"
"k8s.io/klog/v2/internal/severity"
"k8s.io/klog/v2/internal/verbosity"
)
var (
// TimeNow is used to retrieve the current time. May be changed for testing.
TimeNow = time.Now
)
const (
// nameKey is used to log the `WithName` values as an additional attribute.
nameKey = "logger"
)
// NewLogger constructs a new logger.
//
// Verbosity can be modified at any time through the Config.V and
// Config.VModule API.
func NewLogger(c *Config) logr.Logger {
return logr.New(&tlogger{
values: nil,
config: c,
})
}
type tlogger struct {
callDepth int
// hasPrefix is true if the first entry in values is the special
// nameKey key/value. Such an entry gets added and later updated in
// WithName.
hasPrefix bool
values []interface{}
groups string
config *Config
}
func (l *tlogger) Init(info logr.RuntimeInfo) {
l.callDepth = info.CallDepth
}
func (l *tlogger) WithCallDepth(depth int) logr.LogSink {
newLogger := *l
newLogger.callDepth += depth
return &newLogger
}
func (l *tlogger) Enabled(level int) bool {
return l.config.vstate.Enabled(verbosity.Level(level), 1+l.callDepth)
}
func (l *tlogger) Info(_ int, msg string, kvList ...interface{}) {
l.print(nil, severity.InfoLog, msg, kvList)
}
func (l *tlogger) Error(err error, msg string, kvList ...interface{}) {
l.print(err, severity.ErrorLog, msg, kvList)
}
func (l *tlogger) print(err error, s severity.Severity, msg string, kvList []interface{}) {
var file string
var line int
var now time.Time
if l.config.co.withHeader {
// Determine caller.
// +1 for this frame, +1 for Info/Error.
skip := l.callDepth + 2
file, line = l.config.co.unwind(skip)
if file == "" {
file = "???"
line = 1
} else if slash := strings.LastIndex(file, "/"); slash >= 0 {
file = file[slash+1:]
}
now = time.Now()
}
l.printWithInfos(file, line, now, err, s, msg, kvList)
}
func runtimeBacktrace(skip int) (string, int) {
_, file, line, ok := runtime.Caller(skip + 1)
if !ok {
return "", 0
}
return file, line
}
func (l *tlogger) printWithInfos(file string, line int, now time.Time, err error, s severity.Severity, msg string, kvList []interface{}) {
// The message is always quoted, even if it contains line breaks.
// If developers want multi-line output, they should use a small, fixed
// message and put the multi-line output into a value.
qMsg := make([]byte, 0, 1024)
qMsg = strconv.AppendQuote(qMsg, msg)
// Only create a new buffer if we don't have one cached.
b := buffer.GetBuffer()
defer buffer.PutBuffer(b)
if l.config.co.withHeader {
// Format header.
if l.config.co.fixedTime != nil {
now = *l.config.co.fixedTime
}
b.FormatHeader(s, file, line, now)
}
b.Write(qMsg)
var errKV []interface{}
if err != nil {
errKV = []interface{}{"err", err}
}
serialize.FormatKVs(&b.Buffer, errKV, l.values, kvList)
if b.Len() == 0 || b.Bytes()[b.Len()-1] != '\n' {
b.WriteByte('\n')
}
_, _ = l.config.co.output.Write(b.Bytes())
}
func (l *tlogger) WriteKlogBuffer(data []byte) {
_, _ = l.config.co.output.Write(data)
}
// WithName returns a new logr.Logger with the specified name appended. klogr
// uses '/' characters to separate name elements. Callers should not pass '/'
// in the provided name string, but this library does not actually enforce that.
func (l *tlogger) WithName(name string) logr.LogSink {
clone := *l
if l.hasPrefix {
// Copy slice and modify value. No length checks and type
// assertions are needed because hasPrefix is only true if the
// first two elements exist and are key/value strings.
v := make([]interface{}, 0, len(l.values))
v = append(v, l.values...)
prefix, _ := v[1].(string)
v[1] = prefix + "." + name
clone.values = v
} else {
// Preprend new key/value pair.
v := make([]interface{}, 0, 2+len(l.values))
v = append(v, nameKey, name)
v = append(v, l.values...)
clone.values = v
clone.hasPrefix = true
}
return &clone
}
func (l *tlogger) WithValues(kvList ...interface{}) logr.LogSink {
clone := *l
clone.values = serialize.WithValues(l.values, kvList)
return &clone
}
// KlogBufferWriter is implemented by the textlogger LogSink.
type KlogBufferWriter interface {
// WriteKlogBuffer takes a pre-formatted buffer prepared by klog and
// writes it unchanged to the output stream. Can be used with
// klog.WriteKlogBuffer when setting a logger through
// klog.SetLoggerWithOptions.
WriteKlogBuffer([]byte)
}
var _ logr.LogSink = &tlogger{}
var _ logr.CallDepthLogSink = &tlogger{}
var _ KlogBufferWriter = &tlogger{}
+52
View File
@@ -0,0 +1,52 @@
//go:build go1.21
// +build go1.21
/*
Copyright 2023 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 textlogger
import (
"context"
"log/slog"
"github.com/go-logr/logr"
"k8s.io/klog/v2/internal/serialize"
"k8s.io/klog/v2/internal/sloghandler"
)
func (l *tlogger) Handle(ctx context.Context, record slog.Record) error {
return sloghandler.Handle(ctx, record, l.groups, l.printWithInfos)
}
func (l *tlogger) WithAttrs(attrs []slog.Attr) logr.SlogSink {
clone := *l
clone.values = serialize.WithValues(l.values, sloghandler.Attrs2KVList(l.groups, attrs))
return &clone
}
func (l *tlogger) WithGroup(name string) logr.SlogSink {
clone := *l
if clone.groups != "" {
clone.groups += "." + name
} else {
clone.groups = name
}
return &clone
}
var _ logr.SlogSink = &tlogger{}