| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- 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)
- }
- }
|