I created a logger with kubebuilder
, it is based on zap logger:
import (
"flag"
"github.com/gin-gonic/gin"
"net/http"
"os"
"go.uber.org/zap/zapcore"
uzap "go.uber.org/zap"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
var zapOpts []uzap.Option
zapOpts = append(zapOpts, uzap.AddCaller())
zapOpts = append(zapOpts, uzap.AddCallerSkip(1))
zapOpts = append(zapOpts, uzap.AddStacktrace(uzap.DebugLevel))
opts := zap.Options{
Development: developmentFlag,
StacktraceLevel: stacktraceLevel,
Level: level,
Encoder: encoder,
ZapOpts: zapOpts,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
Now I want to change the log level to zapcore.InfoLevel
at run time. I didn't find any SetLogLevel
or similar API.
Do I need to create new opts and then set the new level?
Also I need to set the logger with sigs.k8s.io/controller-runtime/pkg/log/zap
library. The interface of the logger is from go-logr
and it implements logr.Logger
interface. If I tried to change it to zapcore.NewCore
than I can't set the logger with ctrl.SetLogger
anymore.
I want to keep the options to update all the options of zap.Options
and also to change the log level, and still to use the zap from sigs.k8s.io/controller-runtime/pkg/log/zap
.
Is it possible to do it with
sigs.k8s.io/controller-runtime/pkg/log/zap
and sigs.k8s.io/controller-runtime
?