github.xiaoq7.com/operator-framework/operator-sdk@v0.8.2/pkg/log/zap/logger.go (about)

     1  // Copyright 2019 The Operator-SDK Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package zap
    16  
    17  import (
    18  	"io"
    19  	"os"
    20  	"time"
    21  
    22  	"github.com/go-logr/logr"
    23  	"github.com/go-logr/zapr"
    24  	"go.uber.org/zap"
    25  	"go.uber.org/zap/zapcore"
    26  	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
    27  )
    28  
    29  func Logger() logr.Logger {
    30  	return LoggerTo(os.Stderr)
    31  }
    32  
    33  func LoggerTo(destWriter io.Writer) logr.Logger {
    34  	syncer := zapcore.AddSync(destWriter)
    35  	conf := getConfig()
    36  
    37  	conf.encoder = &logf.KubeAwareEncoder{Encoder: conf.encoder, Verbose: conf.level.Level() < 0}
    38  	if conf.sample {
    39  		conf.opts = append(conf.opts, zap.WrapCore(func(core zapcore.Core) zapcore.Core {
    40  			return zapcore.NewSampler(core, time.Second, 100, 100)
    41  		}))
    42  	}
    43  	conf.opts = append(conf.opts, zap.AddCallerSkip(1), zap.ErrorOutput(syncer))
    44  	log := zap.New(zapcore.NewCore(conf.encoder, syncer, conf.level))
    45  	log = log.WithOptions(conf.opts...)
    46  	return zapr.NewLogger(log)
    47  }
    48  
    49  type config struct {
    50  	encoder zapcore.Encoder
    51  	level   zap.AtomicLevel
    52  	sample  bool
    53  	opts    []zap.Option
    54  }
    55  
    56  func getConfig() config {
    57  	var c config
    58  
    59  	// Set the defaults depending on the log mode (development vs. production)
    60  	if development {
    61  		c.encoder = consoleEncoder()
    62  		c.level = zap.NewAtomicLevelAt(zap.DebugLevel)
    63  		c.opts = append(c.opts, zap.Development(), zap.AddStacktrace(zap.ErrorLevel))
    64  		c.sample = false
    65  	} else {
    66  		c.encoder = jsonEncoder()
    67  		c.level = zap.NewAtomicLevelAt(zap.InfoLevel)
    68  		c.opts = append(c.opts, zap.AddStacktrace(zap.WarnLevel))
    69  		c.sample = true
    70  	}
    71  
    72  	// Override the defaults if the flags were set explicitly on the command line
    73  	if encoderVal.set {
    74  		c.encoder = encoderVal.encoder
    75  	}
    76  	if levelVal.set {
    77  		c.level = zap.NewAtomicLevelAt(levelVal.level)
    78  	}
    79  	if sampleVal.set {
    80  		c.sample = sampleVal.sample
    81  	}
    82  
    83  	// Disable sampling when we are in debug mode. Otherwise, this will
    84  	// cause index out of bounds errors in the sampling code.
    85  	if c.level.Level() < -1 {
    86  		c.sample = false
    87  	}
    88  	return c
    89  }