github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/log/encoder.go (about)

     1  package log
     2  
     3  import "go.uber.org/zap/zapcore"
     4  
     5  type encoderBuilder struct {
     6  	cfg zapcore.EncoderConfig
     7  }
     8  
     9  func EncoderBuilder() *encoderBuilder {
    10  	return &encoderBuilder{
    11  		cfg: zapcore.EncoderConfig{
    12  			TimeKey:        "",
    13  			LevelKey:       "",
    14  			NameKey:        "",
    15  			CallerKey:      "",
    16  			MessageKey:     "",
    17  			StacktraceKey:  "stacktrace",
    18  			LineEnding:     zapcore.DefaultLineEnding,
    19  			EncodeTime:     timeEncoder,
    20  			EncodeLevel:    zapcore.CapitalLevelEncoder,
    21  			EncodeDuration: zapcore.StringDurationEncoder,
    22  			EncodeCaller:   zapcore.ShortCallerEncoder,
    23  		},
    24  	}
    25  }
    26  
    27  func (eb *encoderBuilder) WithTimeKey(key string) *encoderBuilder {
    28  	eb.cfg.TimeKey = key
    29  
    30  	return eb
    31  }
    32  
    33  func (eb *encoderBuilder) WithLevelKey(key string) *encoderBuilder {
    34  	eb.cfg.LevelKey = key
    35  
    36  	return eb
    37  }
    38  
    39  func (eb *encoderBuilder) WithNameKey(key string) *encoderBuilder {
    40  	eb.cfg.NameKey = key
    41  
    42  	return eb
    43  }
    44  
    45  func (eb *encoderBuilder) WithMessageKey(key string) *encoderBuilder {
    46  	eb.cfg.MessageKey = key
    47  
    48  	return eb
    49  }
    50  
    51  func (eb *encoderBuilder) WithCallerKey(key string) *encoderBuilder {
    52  	eb.cfg.CallerKey = key
    53  
    54  	return eb
    55  }
    56  
    57  func (eb *encoderBuilder) ConsoleEncoder() Encoder {
    58  	return zapcore.NewConsoleEncoder(eb.cfg)
    59  }
    60  
    61  func (eb *encoderBuilder) JsonEncoder() Encoder {
    62  	return zapcore.NewJSONEncoder(eb.cfg)
    63  }