mirror of
https://github.com/1Password/onepassword-operator.git
synced 2025-10-23 07:58:04 +00:00
Clear repo
To be able to perform the migration, we need to start from an empty directory/repo.
This commit is contained in:
137
vendor/sigs.k8s.io/controller-runtime/pkg/log/deleg.go
generated
vendored
137
vendor/sigs.k8s.io/controller-runtime/pkg/log/deleg.go
generated
vendored
@@ -1,137 +0,0 @@
|
||||
/*
|
||||
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
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
)
|
||||
|
||||
// loggerPromise knows how to populate a concrete logr.Logger
|
||||
// with options, given an actual base logger later on down the line.
|
||||
type loggerPromise struct {
|
||||
logger *DelegatingLogger
|
||||
childPromises []*loggerPromise
|
||||
promisesLock sync.Mutex
|
||||
|
||||
name *string
|
||||
tags []interface{}
|
||||
}
|
||||
|
||||
// WithName provides a new Logger with the name appended
|
||||
func (p *loggerPromise) WithName(l *DelegatingLogger, name string) *loggerPromise {
|
||||
res := &loggerPromise{
|
||||
logger: l,
|
||||
name: &name,
|
||||
promisesLock: sync.Mutex{},
|
||||
}
|
||||
|
||||
p.promisesLock.Lock()
|
||||
defer p.promisesLock.Unlock()
|
||||
p.childPromises = append(p.childPromises, res)
|
||||
return res
|
||||
}
|
||||
|
||||
// WithValues provides a new Logger with the tags appended
|
||||
func (p *loggerPromise) WithValues(l *DelegatingLogger, tags ...interface{}) *loggerPromise {
|
||||
res := &loggerPromise{
|
||||
logger: l,
|
||||
tags: tags,
|
||||
promisesLock: sync.Mutex{},
|
||||
}
|
||||
|
||||
p.promisesLock.Lock()
|
||||
defer p.promisesLock.Unlock()
|
||||
p.childPromises = append(p.childPromises, res)
|
||||
return res
|
||||
}
|
||||
|
||||
// Fulfill instantiates the Logger with the provided logger
|
||||
func (p *loggerPromise) Fulfill(parentLogger logr.Logger) {
|
||||
var logger = parentLogger
|
||||
if p.name != nil {
|
||||
logger = logger.WithName(*p.name)
|
||||
}
|
||||
|
||||
if p.tags != nil {
|
||||
logger = logger.WithValues(p.tags...)
|
||||
}
|
||||
|
||||
p.logger.Logger = logger
|
||||
p.logger.promise = nil
|
||||
|
||||
for _, childPromise := range p.childPromises {
|
||||
childPromise.Fulfill(logger)
|
||||
}
|
||||
}
|
||||
|
||||
// DelegatingLogger is a logr.Logger that delegates to another logr.Logger.
|
||||
// If the underlying promise is not nil, it registers calls to sub-loggers with
|
||||
// the logging factory to be populated later, and returns a new delegating
|
||||
// logger. It expects to have *some* logr.Logger set at all times (generally
|
||||
// a no-op logger before the promises are fulfilled).
|
||||
type DelegatingLogger struct {
|
||||
logr.Logger
|
||||
promise *loggerPromise
|
||||
}
|
||||
|
||||
// WithName provides a new Logger with the name appended
|
||||
func (l *DelegatingLogger) WithName(name string) logr.Logger {
|
||||
if l.promise == nil {
|
||||
return l.Logger.WithName(name)
|
||||
}
|
||||
|
||||
res := &DelegatingLogger{Logger: l.Logger}
|
||||
promise := l.promise.WithName(res, name)
|
||||
res.promise = promise
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// WithValues provides a new Logger with the tags appended
|
||||
func (l *DelegatingLogger) WithValues(tags ...interface{}) logr.Logger {
|
||||
if l.promise == nil {
|
||||
return l.Logger.WithValues(tags...)
|
||||
}
|
||||
|
||||
res := &DelegatingLogger{Logger: l.Logger}
|
||||
promise := l.promise.WithValues(res, tags...)
|
||||
res.promise = promise
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// Fulfill switches the logger over to use the actual logger
|
||||
// provided, instead of the temporary initial one, if this method
|
||||
// has not been previously called.
|
||||
func (l *DelegatingLogger) Fulfill(actual logr.Logger) {
|
||||
if l.promise != nil {
|
||||
l.promise.Fulfill(actual)
|
||||
}
|
||||
}
|
||||
|
||||
// NewDelegatingLogger constructs a new DelegatingLogger which uses
|
||||
// the given logger before it's promise is fulfilled.
|
||||
func NewDelegatingLogger(initial logr.Logger) *DelegatingLogger {
|
||||
l := &DelegatingLogger{
|
||||
Logger: initial,
|
||||
promise: &loggerPromise{promisesLock: sync.Mutex{}},
|
||||
}
|
||||
l.promise.logger = l
|
||||
return l
|
||||
}
|
48
vendor/sigs.k8s.io/controller-runtime/pkg/log/log.go
generated
vendored
48
vendor/sigs.k8s.io/controller-runtime/pkg/log/log.go
generated
vendored
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
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.
|
||||
//
|
||||
// The Log Handle
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Logr
|
||||
//
|
||||
// All logging in controller-runtime is structured, using a set of interfaces
|
||||
// defined by a package called logr
|
||||
// (https://godoc.org/github.com/go-logr/logr). The sub-package zap provides
|
||||
// helpers for setting up logr backed by Zap (go.uber.org/zap).
|
||||
package log
|
||||
|
||||
import (
|
||||
"github.com/go-logr/logr"
|
||||
)
|
||||
|
||||
// SetLogger sets a concrete logging implementation for all deferred Loggers.
|
||||
func SetLogger(l logr.Logger) {
|
||||
Log.Fulfill(l)
|
||||
}
|
||||
|
||||
// Log is the base logger used by kubebuilder. It delegates
|
||||
// to another logr.Logger. You *must* call SetLogger to
|
||||
// get any actual logging.
|
||||
var Log = NewDelegatingLogger(NullLogger{})
|
60
vendor/sigs.k8s.io/controller-runtime/pkg/log/null.go
generated
vendored
60
vendor/sigs.k8s.io/controller-runtime/pkg/log/null.go
generated
vendored
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
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
|
||||
|
||||
import (
|
||||
"github.com/go-logr/logr"
|
||||
)
|
||||
|
||||
// NB: this is the same as the null logger logr/testing,
|
||||
// but avoids accidentally adding the testing flags to
|
||||
// all binaries.
|
||||
|
||||
// NullLogger is a logr.Logger that does nothing.
|
||||
type NullLogger struct{}
|
||||
|
||||
var _ logr.Logger = NullLogger{}
|
||||
|
||||
// Info implements logr.InfoLogger
|
||||
func (NullLogger) Info(_ string, _ ...interface{}) {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
// Enabled implements logr.InfoLogger
|
||||
func (NullLogger) Enabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Error implements logr.Logger
|
||||
func (NullLogger) Error(_ error, _ string, _ ...interface{}) {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
// V implements logr.Logger
|
||||
func (log NullLogger) V(_ int) logr.InfoLogger {
|
||||
return log
|
||||
}
|
||||
|
||||
// WithName implements logr.Logger
|
||||
func (log NullLogger) WithName(_ string) logr.Logger {
|
||||
return log
|
||||
}
|
||||
|
||||
// WithValues implements logr.Logger
|
||||
func (log NullLogger) WithValues(_ ...interface{}) logr.Logger {
|
||||
return log
|
||||
}
|
140
vendor/sigs.k8s.io/controller-runtime/pkg/log/zap/flags.go
generated
vendored
140
vendor/sigs.k8s.io/controller-runtime/pkg/log/zap/flags.go
generated
vendored
@@ -1,140 +0,0 @@
|
||||
/*
|
||||
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 zap contains helpers for setting up a new logr.Logger instance
|
||||
// using the Zap logging framework.
|
||||
package zap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
var levelStrings = map[string]zapcore.Level{
|
||||
"debug": zap.DebugLevel,
|
||||
"-1": zap.DebugLevel,
|
||||
"info": zap.InfoLevel,
|
||||
"0": zap.InfoLevel,
|
||||
"error": zap.ErrorLevel,
|
||||
"2": zap.ErrorLevel,
|
||||
"dpanic": zap.DPanicLevel,
|
||||
"panic": zap.PanicLevel,
|
||||
"warn": zap.WarnLevel,
|
||||
"fatal": zap.FatalLevel,
|
||||
}
|
||||
|
||||
type encoderFlag struct {
|
||||
setFunc func(zapcore.Encoder)
|
||||
value string
|
||||
}
|
||||
|
||||
var _ pflag.Value = &encoderFlag{}
|
||||
|
||||
func (ev *encoderFlag) String() string {
|
||||
return ev.value
|
||||
}
|
||||
|
||||
func (ev *encoderFlag) Type() string {
|
||||
return "encoder"
|
||||
}
|
||||
|
||||
func (ev *encoderFlag) Set(flagValue string) error {
|
||||
val := strings.ToLower(flagValue)
|
||||
switch val {
|
||||
case "json":
|
||||
ev.setFunc(newJSONEncoder())
|
||||
case "console":
|
||||
ev.setFunc(newConsoleEncoder())
|
||||
default:
|
||||
return fmt.Errorf("invalid encoder value \"%s\"", flagValue)
|
||||
}
|
||||
ev.value = flagValue
|
||||
return nil
|
||||
}
|
||||
|
||||
func newJSONEncoder() zapcore.Encoder {
|
||||
encoderConfig := zap.NewProductionEncoderConfig()
|
||||
return zapcore.NewJSONEncoder(encoderConfig)
|
||||
}
|
||||
|
||||
func newConsoleEncoder() zapcore.Encoder {
|
||||
encoderConfig := zap.NewDevelopmentEncoderConfig()
|
||||
return zapcore.NewConsoleEncoder(encoderConfig)
|
||||
}
|
||||
|
||||
type levelFlag struct {
|
||||
setFunc func(zapcore.LevelEnabler)
|
||||
value string
|
||||
}
|
||||
|
||||
var _ pflag.Value = &levelFlag{}
|
||||
|
||||
func (ev *levelFlag) Set(flagValue string) error {
|
||||
level, validLevel := levelStrings[strings.ToLower(flagValue)]
|
||||
if !validLevel {
|
||||
logLevel, err := strconv.Atoi(flagValue)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid log level \"%s\"", flagValue)
|
||||
}
|
||||
if logLevel > 0 {
|
||||
intLevel := -1 * logLevel
|
||||
ev.setFunc(zap.NewAtomicLevelAt(zapcore.Level(int8(intLevel))))
|
||||
} else {
|
||||
return fmt.Errorf("invalid log level \"%s\"", flagValue)
|
||||
}
|
||||
}
|
||||
ev.setFunc(zap.NewAtomicLevelAt(level))
|
||||
ev.value = flagValue
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ev *levelFlag) String() string {
|
||||
return ev.value
|
||||
}
|
||||
|
||||
func (ev *levelFlag) Type() string {
|
||||
return "level"
|
||||
}
|
||||
|
||||
type stackTraceFlag struct {
|
||||
setFunc func(zapcore.LevelEnabler)
|
||||
value string
|
||||
}
|
||||
|
||||
var _ pflag.Value = &stackTraceFlag{}
|
||||
|
||||
func (ev *stackTraceFlag) Set(flagValue string) error {
|
||||
level, validLevel := levelStrings[strings.ToLower(flagValue)]
|
||||
if !validLevel {
|
||||
return fmt.Errorf("invalid stacktrace level \"%s\"", flagValue)
|
||||
}
|
||||
ev.setFunc(zap.NewAtomicLevelAt(level))
|
||||
ev.value = flagValue
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ev *stackTraceFlag) String() string {
|
||||
return ev.value
|
||||
}
|
||||
|
||||
func (ev *stackTraceFlag) Type() string {
|
||||
return "level"
|
||||
}
|
129
vendor/sigs.k8s.io/controller-runtime/pkg/log/zap/kube_helpers.go
generated
vendored
129
vendor/sigs.k8s.io/controller-runtime/pkg/log/zap/kube_helpers.go
generated
vendored
@@ -1,129 +0,0 @@
|
||||
/*
|
||||
Copyright 2019 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 zap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.uber.org/zap/buffer"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
)
|
||||
|
||||
// KubeAwareEncoder is a Kubernetes-aware Zap Encoder.
|
||||
// Instead of trying to force Kubernetes objects to implement
|
||||
// ObjectMarshaller, we just implement a wrapper around a normal
|
||||
// ObjectMarshaller that checks for Kubernetes objects.
|
||||
type KubeAwareEncoder struct {
|
||||
// Encoder is the zapcore.Encoder that this encoder delegates to
|
||||
zapcore.Encoder
|
||||
|
||||
// Verbose controls whether or not the full object is printed.
|
||||
// If false, only name, namespace, api version, and kind are printed.
|
||||
// Otherwise, the full object is logged.
|
||||
Verbose bool
|
||||
}
|
||||
|
||||
// namespacedNameWrapper is a zapcore.ObjectMarshaler for Kubernetes NamespacedName
|
||||
type namespacedNameWrapper struct {
|
||||
types.NamespacedName
|
||||
}
|
||||
|
||||
func (w namespacedNameWrapper) MarshalLogObject(enc zapcore.ObjectEncoder) error {
|
||||
if w.Namespace != "" {
|
||||
enc.AddString("namespace", w.Namespace)
|
||||
}
|
||||
|
||||
enc.AddString("name", w.Name)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// kubeObjectWrapper is a zapcore.ObjectMarshaler for Kubernetes objects.
|
||||
type kubeObjectWrapper struct {
|
||||
obj runtime.Object
|
||||
}
|
||||
|
||||
// MarshalLogObject implements zapcore.ObjectMarshaler
|
||||
func (w kubeObjectWrapper) MarshalLogObject(enc zapcore.ObjectEncoder) error {
|
||||
// TODO(directxman12): log kind and apiversion if not set explicitly (common case)
|
||||
// -- needs an a scheme to convert to the GVK.
|
||||
gvk := w.obj.GetObjectKind().GroupVersionKind()
|
||||
if gvk.Version != "" {
|
||||
enc.AddString("apiVersion", gvk.GroupVersion().String())
|
||||
enc.AddString("kind", gvk.Kind)
|
||||
}
|
||||
|
||||
objMeta, err := meta.Accessor(w.obj)
|
||||
if err != nil {
|
||||
return fmt.Errorf("got runtime.Object without object metadata: %v", w.obj)
|
||||
}
|
||||
|
||||
ns := objMeta.GetNamespace()
|
||||
if ns != "" {
|
||||
enc.AddString("namespace", ns)
|
||||
}
|
||||
enc.AddString("name", objMeta.GetName())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NB(directxman12): can't just override AddReflected, since the encoder calls AddReflected on itself directly
|
||||
|
||||
// Clone implements zapcore.Encoder
|
||||
func (k *KubeAwareEncoder) Clone() zapcore.Encoder {
|
||||
return &KubeAwareEncoder{
|
||||
Encoder: k.Encoder.Clone(),
|
||||
}
|
||||
}
|
||||
|
||||
// EncodeEntry implements zapcore.Encoder
|
||||
func (k *KubeAwareEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
|
||||
if k.Verbose {
|
||||
// Kubernetes objects implement fmt.Stringer, so if we
|
||||
// want verbose output, just delegate to that.
|
||||
return k.Encoder.EncodeEntry(entry, fields)
|
||||
}
|
||||
|
||||
for i, field := range fields {
|
||||
// intercept stringer fields that happen to be Kubernetes runtime.Object or
|
||||
// types.NamespacedName values (Kubernetes runtime.Objects commonly
|
||||
// implement String, apparently).
|
||||
// *unstructured.Unstructured does NOT implement fmt.Striger interface.
|
||||
// We have handle it specially.
|
||||
if field.Type == zapcore.StringerType || field.Type == zapcore.ReflectType {
|
||||
switch val := field.Interface.(type) {
|
||||
case runtime.Object:
|
||||
fields[i] = zapcore.Field{
|
||||
Type: zapcore.ObjectMarshalerType,
|
||||
Key: field.Key,
|
||||
Interface: kubeObjectWrapper{obj: val},
|
||||
}
|
||||
case types.NamespacedName:
|
||||
fields[i] = zapcore.Field{
|
||||
Type: zapcore.ObjectMarshalerType,
|
||||
Key: field.Key,
|
||||
Interface: namespacedNameWrapper{NamespacedName: val},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return k.Encoder.EncodeEntry(entry, fields)
|
||||
}
|
260
vendor/sigs.k8s.io/controller-runtime/pkg/log/zap/zap.go
generated
vendored
260
vendor/sigs.k8s.io/controller-runtime/pkg/log/zap/zap.go
generated
vendored
@@ -1,260 +0,0 @@
|
||||
/*
|
||||
Copyright 2019 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 zap contains helpers for setting up a new logr.Logger instance
|
||||
// using the Zap logging framework.
|
||||
package zap
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/go-logr/zapr"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
// New returns a brand new Logger configured with Opts. It
|
||||
// uses KubeAwareEncoder which adds Type information and
|
||||
// Namespace/Name to the log.
|
||||
func New(opts ...Opts) logr.Logger {
|
||||
return zapr.NewLogger(NewRaw(opts...))
|
||||
}
|
||||
|
||||
// Logger is a Logger implementation.
|
||||
// If development is true, a Zap development config will be used
|
||||
// (stacktraces on warnings, no sampling), otherwise a Zap production
|
||||
// config will be used (stacktraces on errors, sampling).
|
||||
//
|
||||
// Deprecated: use New() and the functional opts pattern instead:
|
||||
//
|
||||
// New(UseDevMode(development))
|
||||
func Logger(development bool) logr.Logger {
|
||||
return LoggerTo(os.Stderr, development)
|
||||
}
|
||||
|
||||
// LoggerTo returns a new Logger implementation using Zap which logs
|
||||
// to the given destination, instead of stderr. It otherwise behaves like
|
||||
// ZapLogger.
|
||||
//
|
||||
// Deprecated: use New() and the functional opts pattern instead:
|
||||
//
|
||||
// New(UseDevMode(development), WriteTo(writer))
|
||||
func LoggerTo(destWriter io.Writer, development bool) logr.Logger {
|
||||
return zapr.NewLogger(RawLoggerTo(destWriter, development))
|
||||
}
|
||||
|
||||
// RawLoggerTo returns a new zap.Logger configured with KubeAwareEncoder
|
||||
// which logs to a given destination
|
||||
//
|
||||
// Deprecated: use NewRaw() and the functional opts pattern instead:
|
||||
//
|
||||
// NewRaw(UseDevMode(development), WriteTo(destWriter), RawZapOpts(opts...))
|
||||
func RawLoggerTo(destWriter io.Writer, development bool, opts ...zap.Option) *zap.Logger {
|
||||
return NewRaw(UseDevMode(development), WriteTo(destWriter), RawZapOpts(opts...))
|
||||
}
|
||||
|
||||
// Opts allows to manipulate Options
|
||||
type Opts func(*Options)
|
||||
|
||||
// UseDevMode sets the logger to use (or not use) development mode (more
|
||||
// human-readable output, extra stack traces and logging information, etc).
|
||||
// See Options.Development
|
||||
func UseDevMode(enabled bool) Opts {
|
||||
return func(o *Options) {
|
||||
o.Development = enabled
|
||||
}
|
||||
}
|
||||
|
||||
// WriteTo configures the logger to write to the given io.Writer, instead of standard error.
|
||||
// See Options.DestWritter
|
||||
func WriteTo(out io.Writer) Opts {
|
||||
return func(o *Options) {
|
||||
o.DestWritter = out
|
||||
}
|
||||
}
|
||||
|
||||
// Encoder configures how the logger will encode the output e.g JSON or console.
|
||||
// See Options.Encoder
|
||||
func Encoder(encoder zapcore.Encoder) func(o *Options) {
|
||||
return func(o *Options) {
|
||||
o.Encoder = encoder
|
||||
}
|
||||
}
|
||||
|
||||
// Level sets the the minimum enabled logging level e.g Debug, Info
|
||||
// See Options.Level
|
||||
func Level(level zapcore.LevelEnabler) func(o *Options) {
|
||||
return func(o *Options) {
|
||||
o.Level = level
|
||||
}
|
||||
}
|
||||
|
||||
// StacktraceLevel configures the logger to record a stack trace for all messages at
|
||||
// or above a given level.
|
||||
// See Options.StacktraceLevel
|
||||
func StacktraceLevel(stacktraceLevel zapcore.LevelEnabler) func(o *Options) {
|
||||
return func(o *Options) {
|
||||
o.StacktraceLevel = stacktraceLevel
|
||||
}
|
||||
}
|
||||
|
||||
// RawZapOpts allows appending arbitrary zap.Options to configure the underlying zap logger.
|
||||
// See Options.ZapOpts
|
||||
func RawZapOpts(zapOpts ...zap.Option) func(o *Options) {
|
||||
return func(o *Options) {
|
||||
o.ZapOpts = append(o.ZapOpts, zapOpts...)
|
||||
}
|
||||
}
|
||||
|
||||
// Options contains all possible settings
|
||||
type Options struct {
|
||||
// Development configures the logger to use a Zap development config
|
||||
// (stacktraces on warnings, no sampling), otherwise a Zap production
|
||||
// config will be used (stacktraces on errors, sampling).
|
||||
Development bool
|
||||
// Encoder configures how Zap will encode the output. Defaults to
|
||||
// console when Development is true and JSON otherwise
|
||||
Encoder zapcore.Encoder
|
||||
// DestWritter controls the destination of the log output. Defaults to
|
||||
// os.Stderr.
|
||||
DestWritter io.Writer
|
||||
// Level configures the verbosity of the logging. Defaults to Debug when
|
||||
// Development is true and Info otherwise
|
||||
Level zapcore.LevelEnabler
|
||||
// StacktraceLevel is the level at and above which stacktraces will
|
||||
// be recorded for all messages. Defaults to Warn when Development
|
||||
// is true and Error otherwise
|
||||
StacktraceLevel zapcore.LevelEnabler
|
||||
// ZapOpts allows passing arbitrary zap.Options to configure on the
|
||||
// underlying Zap logger.
|
||||
ZapOpts []zap.Option
|
||||
}
|
||||
|
||||
// addDefaults adds defaults to the Options
|
||||
func (o *Options) addDefaults() {
|
||||
if o.DestWritter == nil {
|
||||
o.DestWritter = os.Stderr
|
||||
}
|
||||
|
||||
if o.Development {
|
||||
if o.Encoder == nil {
|
||||
encCfg := zap.NewDevelopmentEncoderConfig()
|
||||
o.Encoder = zapcore.NewConsoleEncoder(encCfg)
|
||||
}
|
||||
if o.Level == nil {
|
||||
lvl := zap.NewAtomicLevelAt(zap.DebugLevel)
|
||||
o.Level = &lvl
|
||||
}
|
||||
if o.StacktraceLevel == nil {
|
||||
lvl := zap.NewAtomicLevelAt(zap.WarnLevel)
|
||||
o.StacktraceLevel = &lvl
|
||||
}
|
||||
o.ZapOpts = append(o.ZapOpts, zap.Development())
|
||||
|
||||
} else {
|
||||
if o.Encoder == nil {
|
||||
encCfg := zap.NewProductionEncoderConfig()
|
||||
o.Encoder = zapcore.NewJSONEncoder(encCfg)
|
||||
}
|
||||
if o.Level == nil {
|
||||
lvl := zap.NewAtomicLevelAt(zap.InfoLevel)
|
||||
o.Level = &lvl
|
||||
}
|
||||
if o.StacktraceLevel == nil {
|
||||
lvl := zap.NewAtomicLevelAt(zap.ErrorLevel)
|
||||
o.StacktraceLevel = &lvl
|
||||
}
|
||||
o.ZapOpts = append(o.ZapOpts,
|
||||
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
|
||||
return zapcore.NewSampler(core, time.Second, 100, 100)
|
||||
}))
|
||||
}
|
||||
|
||||
o.ZapOpts = append(o.ZapOpts, zap.AddStacktrace(o.StacktraceLevel))
|
||||
}
|
||||
|
||||
// NewRaw returns a new zap.Logger configured with the passed Opts
|
||||
// or their defaults. It uses KubeAwareEncoder which adds Type
|
||||
// information and Namespace/Name to the log.
|
||||
func NewRaw(opts ...Opts) *zap.Logger {
|
||||
o := &Options{}
|
||||
for _, opt := range opts {
|
||||
opt(o)
|
||||
}
|
||||
o.addDefaults()
|
||||
|
||||
// this basically mimics New<type>Config, but with a custom sink
|
||||
sink := zapcore.AddSync(o.DestWritter)
|
||||
|
||||
o.ZapOpts = append(o.ZapOpts, zap.AddCallerSkip(1), zap.ErrorOutput(sink))
|
||||
log := zap.New(zapcore.NewCore(&KubeAwareEncoder{Encoder: o.Encoder, Verbose: o.Development}, sink, o.Level))
|
||||
log = log.WithOptions(o.ZapOpts...)
|
||||
return log
|
||||
}
|
||||
|
||||
// BindFlags will parse the given flagset for zap option flags and set the log options accordingly
|
||||
// zap-devel: Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn)
|
||||
// Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)
|
||||
// zap-encoder: Zap log encoding ('json' or 'console')
|
||||
// zap-log-level: Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error',
|
||||
// or any integer value > 0 which corresponds to custom debug levels of increasing verbosity")
|
||||
// zap-stacktrace-level: Zap Level at and above which stacktraces are captured (one of 'warn' or 'error')
|
||||
func (o *Options) BindFlags(fs *flag.FlagSet) {
|
||||
|
||||
// Set Development mode value
|
||||
fs.BoolVar(&o.Development, "zap-devel", false,
|
||||
"Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn). "+
|
||||
"Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)")
|
||||
|
||||
// Set Encoder value
|
||||
var encVal encoderFlag
|
||||
encVal.setFunc = func(fromFlag zapcore.Encoder) {
|
||||
o.Encoder = fromFlag
|
||||
}
|
||||
fs.Var(&encVal, "zap-encoder", "Zap log encoding ('json' or 'console')")
|
||||
|
||||
// Set the Log Level
|
||||
var levelVal levelFlag
|
||||
levelVal.setFunc = func(fromFlag zapcore.LevelEnabler) {
|
||||
o.Level = fromFlag
|
||||
}
|
||||
fs.Var(&levelVal, "zap-log-level",
|
||||
"Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error', "+
|
||||
"or any integer value > 0 which corresponds to custom debug levels of increasing verbosity")
|
||||
|
||||
// Set the StrackTrace Level
|
||||
var stackVal stackTraceFlag
|
||||
stackVal.setFunc = func(fromFlag zapcore.LevelEnabler) {
|
||||
o.StacktraceLevel = fromFlag
|
||||
}
|
||||
fs.Var(&stackVal, "zap-stacktrace-level",
|
||||
"Zap Level at and above which stacktraces are captured (one of 'warn' or 'error')")
|
||||
}
|
||||
|
||||
// UseFlagOptions configures the logger to use the Options set by parsing zap option flags from the CLI.
|
||||
// opts := zap.Options{}
|
||||
// opts.BindFlags(flag.CommandLine)
|
||||
// log := zap.New(zap.UseFlagOptions(&opts))
|
||||
func UseFlagOptions(in *Options) Opts {
|
||||
return func(o *Options) {
|
||||
*o = *in
|
||||
o.addDefaults()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user