github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/logger/options.go (about)

     1  package logger
     2  
     3  var _defOptions = Options{
     4  	JsonEncoding:    true,
     5  	CallerSkipCount: 2,
     6  	Level:           DebugLevel,
     7  	Rotation: Rotation{
     8  		MaxSize:    256,
     9  		MaxBackups: 300,
    10  		MaxAge:     30,
    11  		LocalTime:  true,
    12  		Compress:   true,
    13  	},
    14  }
    15  
    16  type Option func(*Options)
    17  
    18  type Options struct {
    19  	Debug           bool
    20  	DisableConsole  bool
    21  	JsonEncoding    bool
    22  	CallerSkipCount int
    23  	LogDir          string
    24  	Level           string
    25  	Fields          map[string]any
    26  	Rotation        Rotation
    27  }
    28  
    29  type Rotation struct {
    30  	MaxSize    int  // 单个文件最大尺寸,默认单位 M
    31  	MaxBackups int  // 最多保留 300 个备份
    32  	MaxAge     int  // 最大时间,默认单位 day
    33  	LocalTime  bool // 使用本地时间
    34  	Compress   bool // 是否压缩 disabled by default
    35  }
    36  
    37  func WithDebug() Option {
    38  	return func(args *Options) {
    39  		args.Debug = true
    40  	}
    41  }
    42  
    43  func WithDisableConsole() Option {
    44  	return func(args *Options) {
    45  		args.DisableConsole = true
    46  	}
    47  }
    48  
    49  func WithLogDir(dir string) Option {
    50  	return func(args *Options) {
    51  		args.LogDir = dir
    52  	}
    53  }
    54  
    55  func WithJsonEncoding() Option {
    56  	return func(args *Options) {
    57  		args.JsonEncoding = true
    58  	}
    59  }
    60  
    61  func WithFields(fields map[string]any) Option {
    62  	return func(args *Options) {
    63  		args.Fields = fields
    64  	}
    65  }
    66  
    67  func WithLevel(level string) Option {
    68  	return func(args *Options) {
    69  		args.Level = level
    70  	}
    71  }
    72  
    73  func WithCallerSkipCount(c int) Option {
    74  	return func(args *Options) {
    75  		args.CallerSkipCount = c
    76  	}
    77  }
    78  
    79  func WithRotation(r Rotation) Option {
    80  	return func(args *Options) {
    81  		args.Rotation = r
    82  	}
    83  }