code.vegaprotocol.io/vega@v0.79.0/libs/zap/config.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package zap
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"time"
    23  
    24  	vgfs "code.vegaprotocol.io/vega/libs/fs"
    25  
    26  	"go.uber.org/zap"
    27  	"go.uber.org/zap/zapcore"
    28  )
    29  
    30  func DefaultConfig() zap.Config {
    31  	return zap.Config{
    32  		Level:    zap.NewAtomicLevelAt(zapcore.InfoLevel),
    33  		Encoding: "json",
    34  		EncoderConfig: zapcore.EncoderConfig{
    35  			MessageKey:     "message",
    36  			LevelKey:       "level",
    37  			TimeKey:        "@timestamp",
    38  			NameKey:        "logger",
    39  			CallerKey:      "caller",
    40  			StacktraceKey:  "stacktrace",
    41  			LineEnding:     "\n",
    42  			EncodeLevel:    zapcore.LowercaseLevelEncoder,
    43  			EncodeTime:     zapcore.ISO8601TimeEncoder,
    44  			EncodeDuration: zapcore.StringDurationEncoder,
    45  			EncodeCaller:   zapcore.ShortCallerEncoder,
    46  			EncodeName:     zapcore.FullNameEncoder,
    47  		},
    48  		OutputPaths:       []string{"stdout"},
    49  		ErrorOutputPaths:  []string{"stderr"},
    50  		DisableStacktrace: true,
    51  	}
    52  }
    53  
    54  func WithLevel(cfg zap.Config, level string) zap.Config {
    55  	parsedLevel, err := parseLevel(level)
    56  	if err != nil {
    57  		parsedLevel = zap.NewAtomicLevelAt(zapcore.InfoLevel)
    58  	}
    59  
    60  	cfg.Level = parsedLevel
    61  
    62  	return cfg
    63  }
    64  
    65  func WithFileOutputForDedicatedProcess(cfg zap.Config, dirPath string) zap.Config {
    66  	date := time.Now().UTC().Format("2006-01-02-15-04-05")
    67  	pid := os.Getpid()
    68  	logFileName := fmt.Sprintf("%s-%d.log", date, pid)
    69  	logFilePath := filepath.Join(dirPath, logFileName)
    70  
    71  	return WithFileOutput(cfg, logFilePath)
    72  }
    73  
    74  func WithFileOutput(cfg zap.Config, filePath string) zap.Config {
    75  	zapLogPath := toOSFilePath(filePath)
    76  
    77  	fileDir, _ := filepath.Split(filePath)
    78  	_ = vgfs.EnsureDir(fileDir)
    79  
    80  	cfg.OutputPaths = []string{zapLogPath}
    81  	cfg.ErrorOutputPaths = []string{zapLogPath}
    82  
    83  	return cfg
    84  }
    85  
    86  func WithStandardOutput(cfg zap.Config) zap.Config {
    87  	cfg.OutputPaths = []string{"stdout"}
    88  	cfg.ErrorOutputPaths = []string{"stderr"}
    89  
    90  	return cfg
    91  }
    92  
    93  func WithJSONFormat(cfg zap.Config) zap.Config {
    94  	cfg.EncoderConfig.EncodeLevel = zapcore.LowercaseLevelEncoder
    95  	cfg.Encoding = "json"
    96  
    97  	return cfg
    98  }
    99  
   100  func WithConsoleFormat(cfg zap.Config) zap.Config {
   101  	cfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
   102  	cfg.Encoding = "console"
   103  
   104  	return cfg
   105  }