package logger import ( "context" "io" ) type Option func(*Options) type Options struct { // 日志记录级别,默认值为`InfoLevel` Level Level // 记录的字段 Fields map[string]interface{} // 通常将其设置为文件, 默认值 `os.Stderr` Out io.Writer // 调用方跳过文件的帧数:行信息 CallerSkipCount int Context context.Context // Name logger 名称 Name string } // WithFields 设置记录器的默认字段 func WithFields(fields map[string]interface{}) Option { return func(args *Options) { args.Fields = fields } } // WithLevel 设置记录器的默认级别 func WithLevel(level Level) Option { return func(args *Options) { args.Level = level } } // WithOutput 设置记录器的默认输出编写器 func WithOutput(out io.Writer) Option { return func(args *Options) { args.Out = out } } // WithCallerSkipCount 设置帧跳过数 func WithCallerSkipCount(c int) Option { return func(args *Options) { args.CallerSkipCount = c } } // WithName 设置记录器的默认名称 func WithName(name string) Option { return func(args *Options) { args.Name = name } } func SetOption(k, v interface{}) Option { return func(o *Options) { if o.Context == nil { o.Context = context.Background() } o.Context = context.WithValue(o.Context, k, v) } }