github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/logging/zap.go (about)

     1  package logging
     2  
     3  import (
     4  	"go.uber.org/zap"
     5  	"go.uber.org/zap/zapcore"
     6  )
     7  
     8  // NewProductionConsoleConfig is a reasonable production logging configuration.
     9  // Logging is enabled at InfoLevel and above.
    10  //
    11  // It uses a console encoder, writes to standard error, and enables sampling.
    12  // Stacktraces are automatically included on logs of ErrorLevel and above.
    13  func NewProductionConsoleConfig(suppressTimestamps bool) zap.Config {
    14  	return zap.Config{
    15  		Level:       zap.NewAtomicLevelAt(zap.InfoLevel),
    16  		Development: false,
    17  		Sampling: &zap.SamplingConfig{
    18  			Initial:    100,
    19  			Thereafter: 100,
    20  		},
    21  		Encoding:         "console",
    22  		EncoderConfig:    NewProductionConsoleEncoderConfig(suppressTimestamps),
    23  		OutputPaths:      []string{"stderr"},
    24  		ErrorOutputPaths: []string{"stderr"},
    25  	}
    26  }
    27  
    28  // NewProductionConsoleEncoderConfig returns an opinionated EncoderConfig for
    29  // production console environments.
    30  func NewProductionConsoleEncoderConfig(suppressTimestamps bool) zapcore.EncoderConfig {
    31  	var (
    32  		timeKey    string
    33  		encodeTime zapcore.TimeEncoder
    34  	)
    35  
    36  	if !suppressTimestamps {
    37  		timeKey = "T"
    38  		encodeTime = zapcore.ISO8601TimeEncoder
    39  	}
    40  
    41  	return zapcore.EncoderConfig{
    42  		TimeKey:          timeKey,
    43  		LevelKey:         "L",
    44  		NameKey:          "N",
    45  		CallerKey:        "C",
    46  		FunctionKey:      zapcore.OmitKey,
    47  		MessageKey:       "M",
    48  		StacktraceKey:    "S",
    49  		LineEnding:       zapcore.DefaultLineEnding,
    50  		EncodeLevel:      zapcore.CapitalColorLevelEncoder,
    51  		EncodeTime:       encodeTime,
    52  		EncodeDuration:   zapcore.StringDurationEncoder,
    53  		EncodeCaller:     zapcore.ShortCallerEncoder,
    54  		ConsoleSeparator: " ",
    55  	}
    56  }