trpc.group/trpc-go/trpc-go@v1.0.3/log/config.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package log
    15  
    16  import (
    17  	"time"
    18  
    19  	yaml "gopkg.in/yaml.v3"
    20  )
    21  
    22  // output name, default support console and file.
    23  const (
    24  	OutputConsole = "console"
    25  	OutputFile    = "file"
    26  )
    27  
    28  // Config is the log config. Each log may have multiple outputs.
    29  type Config []OutputConfig
    30  
    31  // OutputConfig is the output config, includes console, file and remote.
    32  type OutputConfig struct {
    33  	// Writer is the output of log, such as console or file.
    34  	Writer      string      `yaml:"writer"`
    35  	WriteConfig WriteConfig `yaml:"writer_config"`
    36  
    37  	// Formatter is the format of log, such as console or json.
    38  	Formatter    string       `yaml:"formatter"`
    39  	FormatConfig FormatConfig `yaml:"formatter_config"`
    40  
    41  	// RemoteConfig is the remote config. It's defined by business and should be registered by
    42  	// third-party modules.
    43  	RemoteConfig yaml.Node `yaml:"remote_config"`
    44  
    45  	// Level controls the log level, like debug, info or error.
    46  	Level string `yaml:"level"`
    47  
    48  	// CallerSkip controls the nesting depth of log function.
    49  	CallerSkip int `yaml:"caller_skip"`
    50  
    51  	// EnableColor determines if the output is colored. The default value is false.
    52  	EnableColor bool `yaml:"enable_color"`
    53  }
    54  
    55  // WriteConfig is the local file config.
    56  type WriteConfig struct {
    57  	// LogPath is the log path like /usr/local/trpc/log/.
    58  	LogPath string `yaml:"log_path"`
    59  	// Filename is the file name like trpc.log.
    60  	Filename string `yaml:"filename"`
    61  	// WriteMode is the log write mod. 1: sync, 2: async, 3: fast(maybe dropped), default as 3.
    62  	WriteMode int `yaml:"write_mode"`
    63  	// RollType is the log rolling type. Split files by size/time, default by size.
    64  	RollType string `yaml:"roll_type"`
    65  	// MaxAge is the max expire times(day).
    66  	MaxAge int `yaml:"max_age"`
    67  	// MaxBackups is the max backup files.
    68  	MaxBackups int `yaml:"max_backups"`
    69  	// Compress defines whether log should be compressed.
    70  	Compress bool `yaml:"compress"`
    71  	// MaxSize is the max size of log file(MB).
    72  	MaxSize int `yaml:"max_size"`
    73  
    74  	// TimeUnit splits files by time unit, like year/month/hour/minute, default day.
    75  	// It takes effect only when split by time.
    76  	TimeUnit TimeUnit `yaml:"time_unit"`
    77  }
    78  
    79  // FormatConfig is the log format config.
    80  type FormatConfig struct {
    81  	// TimeFmt is the time format of log output, default as "2006-01-02 15:04:05.000" on empty.
    82  	TimeFmt string `yaml:"time_fmt"`
    83  
    84  	// TimeKey is the time key of log output, default as "T".
    85  	TimeKey string `yaml:"time_key"`
    86  	// LevelKey is the level key of log output, default as "L".
    87  	LevelKey string `yaml:"level_key"`
    88  	// NameKey is the name key of log output, default as "N".
    89  	NameKey string `yaml:"name_key"`
    90  	// CallerKey is the caller key of log output, default as "C".
    91  	CallerKey string `yaml:"caller_key"`
    92  	// FunctionKey is the function key of log output, default as "", which means not to print
    93  	// function name.
    94  	FunctionKey string `yaml:"function_key"`
    95  	// MessageKey is the message key of log output, default as "M".
    96  	MessageKey string `yaml:"message_key"`
    97  	// StackTraceKey is the stack trace key of log output, default as "S".
    98  	StacktraceKey string `yaml:"stacktrace_key"`
    99  }
   100  
   101  // WriteMode is the log write mode, one of 1, 2, 3.
   102  type WriteMode int
   103  
   104  const (
   105  	// WriteSync writes synchronously.
   106  	WriteSync = 1
   107  	// WriteAsync writes asynchronously.
   108  	WriteAsync = 2
   109  	// WriteFast writes fast(may drop logs asynchronously).
   110  	WriteFast = 3
   111  )
   112  
   113  // By which log rolls.
   114  const (
   115  	// RollBySize rolls logs by file size.
   116  	RollBySize = "size"
   117  	// RollByTime rolls logs by time.
   118  	RollByTime = "time"
   119  )
   120  
   121  // Some common used time formats.
   122  const (
   123  	// TimeFormatMinute is accurate to the minute.
   124  	TimeFormatMinute = "%Y%m%d%H%M"
   125  	// TimeFormatHour is accurate to the hour.
   126  	TimeFormatHour = "%Y%m%d%H"
   127  	// TimeFormatDay is accurate to the day.
   128  	TimeFormatDay = "%Y%m%d"
   129  	// TimeFormatMonth is accurate to the month.
   130  	TimeFormatMonth = "%Y%m"
   131  	// TimeFormatYear is accurate to the year.
   132  	TimeFormatYear = "%Y"
   133  )
   134  
   135  // TimeUnit is the time unit by which files are split, one of minute/hour/day/month/year.
   136  type TimeUnit string
   137  
   138  const (
   139  	// Minute splits by the minute.
   140  	Minute = "minute"
   141  	// Hour splits by the hour.
   142  	Hour = "hour"
   143  	// Day splits by the day.
   144  	Day = "day"
   145  	// Month splits by the month.
   146  	Month = "month"
   147  	// Year splits by the year.
   148  	Year = "year"
   149  )
   150  
   151  // Format returns a string preceding with `.`. Use TimeFormatDay as default.
   152  func (t TimeUnit) Format() string {
   153  	var timeFmt string
   154  	switch t {
   155  	case Minute:
   156  		timeFmt = TimeFormatMinute
   157  	case Hour:
   158  		timeFmt = TimeFormatHour
   159  	case Day:
   160  		timeFmt = TimeFormatDay
   161  	case Month:
   162  		timeFmt = TimeFormatMonth
   163  	case Year:
   164  		timeFmt = TimeFormatYear
   165  	default:
   166  		timeFmt = TimeFormatDay
   167  	}
   168  	return "." + timeFmt
   169  }
   170  
   171  // RotationGap returns the time.Duration for time unit. Use one day as the default.
   172  func (t TimeUnit) RotationGap() time.Duration {
   173  	switch t {
   174  	case Minute:
   175  		return time.Minute
   176  	case Hour:
   177  		return time.Hour
   178  	case Day:
   179  		return time.Hour * 24
   180  	case Month:
   181  		return time.Hour * 24 * 30
   182  	case Year:
   183  		return time.Hour * 24 * 365
   184  	default:
   185  		return time.Hour * 24
   186  	}
   187  }