2020-10-21 05:49:41 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 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 log contains utilities for fetching a new logger
|
|
|
|
// when one is not already available.
|
|
|
|
//
|
2023-02-01 17:06:36 +00:00
|
|
|
// # The Log Handle
|
2020-10-21 05:49:41 +00:00
|
|
|
//
|
|
|
|
// This package contains a root logr.Logger Log. It may be used to
|
|
|
|
// get a handle to whatever the root logging implementation is. By
|
|
|
|
// default, no implementation exists, and the handle returns "promises"
|
|
|
|
// to loggers. When the implementation is set using SetLogger, these
|
|
|
|
// "promises" will be converted over to real loggers.
|
|
|
|
//
|
2023-02-01 17:06:36 +00:00
|
|
|
// # Logr
|
2020-10-21 05:49:41 +00:00
|
|
|
//
|
|
|
|
// All logging in controller-runtime is structured, using a set of interfaces
|
|
|
|
// defined by a package called logr
|
2023-02-01 17:06:36 +00:00
|
|
|
// (https://pkg.go.dev/github.com/go-logr/logr). The sub-package zap provides
|
2020-10-21 05:49:41 +00:00
|
|
|
// helpers for setting up logr backed by Zap (go.uber.org/zap).
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2023-08-28 20:44:55 +00:00
|
|
|
"bytes"
|
2021-06-25 05:02:01 +00:00
|
|
|
"context"
|
2023-06-01 17:01:19 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime/debug"
|
|
|
|
"sync/atomic"
|
2021-06-25 05:02:01 +00:00
|
|
|
"time"
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
"github.com/go-logr/logr"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SetLogger sets a concrete logging implementation for all deferred Loggers.
|
|
|
|
func SetLogger(l logr.Logger) {
|
2023-06-01 17:01:19 +00:00
|
|
|
logFullfilled.Store(true)
|
|
|
|
rootLog.Fulfill(l.GetSink())
|
2020-10-21 05:49:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:01:19 +00:00
|
|
|
func eventuallyFulfillRoot() {
|
|
|
|
if logFullfilled.Load() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if time.Since(rootLogCreated).Seconds() >= 30 {
|
|
|
|
if logFullfilled.CompareAndSwap(false, true) {
|
2023-08-28 20:44:55 +00:00
|
|
|
stack := debug.Stack()
|
|
|
|
stackLines := bytes.Count(stack, []byte{'\n'})
|
|
|
|
sep := []byte{'\n', '\t', '>', ' ', ' '}
|
|
|
|
|
|
|
|
fmt.Fprintf(os.Stderr,
|
|
|
|
"[controller-runtime] log.SetLogger(...) was never called; logs will not be displayed.\nDetected at:%s%s", sep,
|
|
|
|
// prefix every line, so it's clear this is a stack trace related to the above message
|
|
|
|
bytes.Replace(stack, []byte{'\n'}, sep, stackLines-1),
|
|
|
|
)
|
2023-06-01 17:01:19 +00:00
|
|
|
SetLogger(logr.New(NullLogSink{}))
|
2021-06-25 05:02:01 +00:00
|
|
|
}
|
2023-06-01 17:01:19 +00:00
|
|
|
}
|
2021-06-25 05:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2023-06-01 17:01:19 +00:00
|
|
|
logFullfilled atomic.Bool
|
2021-06-25 05:02:01 +00:00
|
|
|
)
|
|
|
|
|
2020-10-21 05:49:41 +00:00
|
|
|
// Log is the base logger used by kubebuilder. It delegates
|
2021-06-25 05:02:01 +00:00
|
|
|
// to another logr.Logger. You *must* call SetLogger to
|
|
|
|
// get any actual logging. If SetLogger is not called within
|
|
|
|
// the first 30 seconds of a binaries lifetime, it will get
|
2021-12-08 13:50:47 +00:00
|
|
|
// set to a NullLogSink.
|
|
|
|
var (
|
2023-06-01 17:01:19 +00:00
|
|
|
rootLog, rootLogCreated = func() (*delegatingLogSink, time.Time) {
|
|
|
|
return newDelegatingLogSink(NullLogSink{}), time.Now()
|
|
|
|
}()
|
|
|
|
Log = logr.New(rootLog)
|
2021-12-08 13:50:47 +00:00
|
|
|
)
|
2021-06-25 05:02:01 +00:00
|
|
|
|
|
|
|
// FromContext returns a logger with predefined values from a context.Context.
|
|
|
|
func FromContext(ctx context.Context, keysAndValues ...interface{}) logr.Logger {
|
2021-12-08 13:50:47 +00:00
|
|
|
log := Log
|
2021-06-25 05:02:01 +00:00
|
|
|
if ctx != nil {
|
2021-12-08 13:50:47 +00:00
|
|
|
if logger, err := logr.FromContext(ctx); err == nil {
|
2021-06-25 05:02:01 +00:00
|
|
|
log = logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return log.WithValues(keysAndValues...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IntoContext takes a context and sets the logger as one of its values.
|
|
|
|
// Use FromContext function to retrieve the logger.
|
|
|
|
func IntoContext(ctx context.Context, log logr.Logger) context.Context {
|
|
|
|
return logr.NewContext(ctx, log)
|
|
|
|
}
|