...

Source file src/sigs.k8s.io/controller-runtime/pkg/log/zap/flags.go

Documentation: sigs.k8s.io/controller-runtime/pkg/log/zap

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package zap contains helpers for setting up a new logr.Logger instance
    18  // using the Zap logging framework.
    19  package zap
    20  
    21  import (
    22  	"flag"
    23  	"fmt"
    24  	"strconv"
    25  	"strings"
    26  
    27  	"go.uber.org/zap"
    28  	"go.uber.org/zap/zapcore"
    29  )
    30  
    31  var levelStrings = map[string]zapcore.Level{
    32  	"debug": zap.DebugLevel,
    33  	"info":  zap.InfoLevel,
    34  	"error": zap.ErrorLevel,
    35  }
    36  
    37  var stackLevelStrings = map[string]zapcore.Level{
    38  	"info":  zap.InfoLevel,
    39  	"error": zap.ErrorLevel,
    40  	"panic": zap.PanicLevel,
    41  }
    42  
    43  type encoderFlag struct {
    44  	setFunc func(NewEncoderFunc)
    45  	value   string
    46  }
    47  
    48  var _ flag.Value = &encoderFlag{}
    49  
    50  func (ev *encoderFlag) String() string {
    51  	return ev.value
    52  }
    53  
    54  func (ev *encoderFlag) Type() string {
    55  	return "encoder"
    56  }
    57  
    58  func (ev *encoderFlag) Set(flagValue string) error {
    59  	val := strings.ToLower(flagValue)
    60  	switch val {
    61  	case "json":
    62  		ev.setFunc(newJSONEncoder)
    63  	case "console":
    64  		ev.setFunc(newConsoleEncoder)
    65  	default:
    66  		return fmt.Errorf("invalid encoder value \"%s\"", flagValue)
    67  	}
    68  	ev.value = flagValue
    69  	return nil
    70  }
    71  
    72  type levelFlag struct {
    73  	setFunc func(zapcore.LevelEnabler)
    74  	value   string
    75  }
    76  
    77  var _ flag.Value = &levelFlag{}
    78  
    79  func (ev *levelFlag) Set(flagValue string) error {
    80  	level, validLevel := levelStrings[strings.ToLower(flagValue)]
    81  	if !validLevel {
    82  		logLevel, err := strconv.Atoi(flagValue)
    83  		if err != nil {
    84  			return fmt.Errorf("invalid log level \"%s\"", flagValue)
    85  		}
    86  		if logLevel > 0 {
    87  			intLevel := -1 * logLevel
    88  			ev.setFunc(zap.NewAtomicLevelAt(zapcore.Level(int8(intLevel))))
    89  		} else {
    90  			return fmt.Errorf("invalid log level \"%s\"", flagValue)
    91  		}
    92  	} else {
    93  		ev.setFunc(zap.NewAtomicLevelAt(level))
    94  	}
    95  	ev.value = flagValue
    96  	return nil
    97  }
    98  
    99  func (ev *levelFlag) String() string {
   100  	return ev.value
   101  }
   102  
   103  func (ev *levelFlag) Type() string {
   104  	return "level"
   105  }
   106  
   107  type stackTraceFlag struct {
   108  	setFunc func(zapcore.LevelEnabler)
   109  	value   string
   110  }
   111  
   112  var _ flag.Value = &stackTraceFlag{}
   113  
   114  func (ev *stackTraceFlag) Set(flagValue string) error {
   115  	level, validLevel := stackLevelStrings[strings.ToLower(flagValue)]
   116  	if !validLevel {
   117  		return fmt.Errorf("invalid stacktrace level \"%s\"", flagValue)
   118  	}
   119  	ev.setFunc(zap.NewAtomicLevelAt(level))
   120  	ev.value = flagValue
   121  	return nil
   122  }
   123  
   124  func (ev *stackTraceFlag) String() string {
   125  	return ev.value
   126  }
   127  
   128  func (ev *stackTraceFlag) Type() string {
   129  	return "level"
   130  }
   131  
   132  type timeEncodingFlag struct {
   133  	setFunc func(zapcore.TimeEncoder)
   134  	value   string
   135  }
   136  
   137  var _ flag.Value = &timeEncodingFlag{}
   138  
   139  func (ev *timeEncodingFlag) String() string {
   140  	return ev.value
   141  }
   142  
   143  func (ev *timeEncodingFlag) Type() string {
   144  	return "time-encoding"
   145  }
   146  
   147  func (ev *timeEncodingFlag) Set(flagValue string) error {
   148  	val := strings.ToLower(flagValue)
   149  	switch val {
   150  	case "rfc3339nano":
   151  		ev.setFunc(zapcore.RFC3339NanoTimeEncoder)
   152  	case "rfc3339":
   153  		ev.setFunc(zapcore.RFC3339TimeEncoder)
   154  	case "iso8601":
   155  		ev.setFunc(zapcore.ISO8601TimeEncoder)
   156  	case "millis":
   157  		ev.setFunc(zapcore.EpochMillisTimeEncoder)
   158  	case "nanos":
   159  		ev.setFunc(zapcore.EpochNanosTimeEncoder)
   160  	case "epoch":
   161  		ev.setFunc(zapcore.EpochTimeEncoder)
   162  	default:
   163  		return fmt.Errorf("invalid time-encoding value \"%s\"", flagValue)
   164  	}
   165  
   166  	ev.value = flagValue
   167  	return nil
   168  }
   169  

View as plain text