Log Configuration

Log Overview

LogConf for our log configuration,logx.MustSetupprovides the basic configuration capability for our log, using the following simple methods:

  1. var c logc.LogConf
  2. logc.MustSetup(c)
  3. logc.Info(context.Background(), "log")
  4. // do your job

Our log is referenced by serviceConf and will be automatically initialized when the service is started.

Definition of parameters

LogConf configuration definition below:

  1. package logx
  2. // A LogConf is a logging config.
  3. type LogConf struct {
  4. ServiceName string `json:",optional"`
  5. Mode string `json:",default=console,options=[console,file,volume]"`
  6. Encoding string `json:",default=json,options=[json,plain]"`
  7. TimeFormat string `json:",optional"`
  8. Path string `json:",default=logs"`
  9. Level string `json:",default=info,options=[debug,info,error,severe]"`
  10. MaxContentLength uint32 `json:",optional"`
  11. Compress bool `json:",optional"`
  12. Stat bool `json:",default=true"` // go-zero 版本 >= 1.5.0 才支持
  13. KeepDays int `json:",optional"`
  14. StackCooldownMillis int `json:",default=100"`
  15. // MaxBackups represents how many backup log files will be kept. 0 means all files will be kept forever.
  16. // Only take effect when RotationRuleType is `size`.
  17. // Even thougth `MaxBackups` sets 0, log files will still be removed
  18. // if the `KeepDays` limitation is reached.
  19. MaxBackups int `json:",default=0"`
  20. // MaxSize represents how much space the writing log file takes up. 0 means no limit. The unit is `MB`.
  21. // Only take effect when RotationRuleType is `size`
  22. MaxSize int `json:",default=0"`
  23. // RotationRuleType represents the type of log rotation rule. Default is `daily`.
  24. // daily: daily rotation.
  25. // size: size limited rotation.
  26. Rotation string `json:",default=daily,options=[daily,size]"`
  27. }
ParamsDataTypeDefault valueNoteEnum Values
ServiceNamestringService Name
ModestringconsoleLog Printing Mode, console Consolefile, console
EncodingstringjsonLog format, json format or plain plain textjson, plain
TimeFormatstringDate Format
PathstringlogsLog output path in file output mode
LevelstringinfoLog output leveldebug,info,error,severe
MaxContentLengthuint320Log length limiting. Logs will be cropped when printing delayed logs, only crop content
CompressboolfalseWhether to compress logs
StatbooltrueWhether to disable stat log, go-zero version is greater than 1.5.0
KeepDaysint0Log number of days left, only in file mode
StackCooldownMillisint100Stack print cooldown time
MaxBackupsint0File output mode with maximum number of files remaining when split by size
MaxSizeint0File output mode, single file size when split by size
RotationstringdailyFile split mode, daily by datedaily,size