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