github.com/lablabs/operator-sdk@v0.8.2/pkg/log/zap/flags.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  	"flag"
    19  	"fmt"
    20  	"strconv"
    21  	"strings"
    22  
    23  	"github.com/spf13/pflag"
    24  	"go.uber.org/zap"
    25  	"go.uber.org/zap/zapcore"
    26  	"k8s.io/klog"
    27  )
    28  
    29  var (
    30  	zapFlagSet *pflag.FlagSet
    31  
    32  	development bool
    33  	encoderVal  encoderValue
    34  	levelVal    levelValue
    35  	sampleVal   sampleValue
    36  )
    37  
    38  func init() {
    39  	zapFlagSet = pflag.NewFlagSet("zap", pflag.ExitOnError)
    40  	zapFlagSet.BoolVar(&development, "zap-devel", false, "Enable zap development mode (changes defaults to console encoder, debug log level, and disables sampling)")
    41  	zapFlagSet.Var(&encoderVal, "zap-encoder", "Zap log encoding ('json' or 'console')")
    42  	zapFlagSet.Var(&levelVal, "zap-level", "Zap log level (one of 'debug', 'info', 'error' or any integer value > 0)")
    43  	zapFlagSet.Var(&sampleVal, "zap-sample", "Enable zap log sampling. Sampling will be disabled for integer log levels > 1")
    44  }
    45  
    46  // FlagSet - The zap logging flagset.
    47  func FlagSet() *pflag.FlagSet {
    48  	return zapFlagSet
    49  }
    50  
    51  type encoderValue struct {
    52  	set     bool
    53  	encoder zapcore.Encoder
    54  	str     string
    55  }
    56  
    57  func (v *encoderValue) Set(e string) error {
    58  	v.set = true
    59  	switch e {
    60  	case "json":
    61  		v.encoder = jsonEncoder()
    62  	case "console":
    63  		v.encoder = consoleEncoder()
    64  	default:
    65  		return fmt.Errorf("unknown encoder \"%s\"", e)
    66  	}
    67  	v.str = e
    68  	return nil
    69  }
    70  
    71  func (v encoderValue) String() string {
    72  	return v.str
    73  }
    74  
    75  func (v encoderValue) Type() string {
    76  	return "encoder"
    77  }
    78  
    79  func jsonEncoder() zapcore.Encoder {
    80  	encoderConfig := zap.NewProductionEncoderConfig()
    81  	return zapcore.NewJSONEncoder(encoderConfig)
    82  }
    83  
    84  func consoleEncoder() zapcore.Encoder {
    85  	encoderConfig := zap.NewDevelopmentEncoderConfig()
    86  	return zapcore.NewConsoleEncoder(encoderConfig)
    87  }
    88  
    89  type levelValue struct {
    90  	set   bool
    91  	level zapcore.Level
    92  }
    93  
    94  func (v *levelValue) Set(l string) error {
    95  	v.set = true
    96  	lower := strings.ToLower(l)
    97  	var lvl int
    98  	switch lower {
    99  	case "debug":
   100  		lvl = -1
   101  	case "info":
   102  		lvl = 0
   103  	case "error":
   104  		lvl = 2
   105  	default:
   106  		i, err := strconv.Atoi(lower)
   107  		if err != nil {
   108  			return fmt.Errorf("invalid log level \"%s\"", l)
   109  		}
   110  
   111  		if i > 0 {
   112  			lvl = -1 * i
   113  		} else {
   114  			return fmt.Errorf("invalid log level \"%s\"", l)
   115  		}
   116  	}
   117  	v.level = zapcore.Level(int8(lvl))
   118  	// If log level is greater than debug, set glog/klog level to that level.
   119  	if lvl < -3 {
   120  		fs := flag.NewFlagSet("", flag.ContinueOnError)
   121  		klog.InitFlags(fs)
   122  		err := fs.Set("v", fmt.Sprintf("%v", -1*lvl))
   123  		if err != nil {
   124  			return err
   125  		}
   126  	}
   127  	return nil
   128  }
   129  
   130  func (v levelValue) String() string {
   131  	return v.level.String()
   132  }
   133  
   134  func (v levelValue) Type() string {
   135  	return "level"
   136  }
   137  
   138  type sampleValue struct {
   139  	set    bool
   140  	sample bool
   141  }
   142  
   143  func (v *sampleValue) Set(s string) error {
   144  	var err error
   145  	v.set = true
   146  	v.sample, err = strconv.ParseBool(s)
   147  	return err
   148  }
   149  
   150  func (v sampleValue) String() string {
   151  	return strconv.FormatBool(v.sample)
   152  }
   153  
   154  func (v sampleValue) IsBoolFlag() bool {
   155  	return true
   156  }
   157  
   158  func (v sampleValue) Type() string {
   159  	return "sample"
   160  }