github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/xlog/common_core.go (about)

     1  package xlog
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/benz9527/xboot/lib/infra"
     7  	"go.uber.org/zap"
     8  	"go.uber.org/zap/zapcore"
     9  )
    10  
    11  var _ xLogCore = (*commonCore)(nil)
    12  
    13  type commonCore struct {
    14  	ctx        context.Context
    15  	lvlEnabler zapcore.LevelEnabler
    16  	lvlEnc     zapcore.LevelEncoder
    17  	tsEnc      zapcore.TimeEncoder
    18  	ws         zapcore.WriteSyncer
    19  	enc        func(cfg zapcore.EncoderConfig) zapcore.Encoder
    20  	core       zapcore.Core
    21  }
    22  
    23  func (cc *commonCore) timeEncoder() zapcore.TimeEncoder                            { return cc.tsEnc }
    24  func (cc *commonCore) levelEncoder() zapcore.LevelEncoder                          { return cc.lvlEnc }
    25  func (cc *commonCore) writeSyncer() zapcore.WriteSyncer                            { return cc.ws }
    26  func (cc *commonCore) outEncoder() func(cfg zapcore.EncoderConfig) zapcore.Encoder { return cc.enc }
    27  func (cc *commonCore) context() context.Context                                    { return cc.ctx }
    28  func (cc *commonCore) Enabled(lvl zapcore.Level) bool {
    29  	return cc.lvlEnabler.Enabled(lvl)
    30  }
    31  
    32  func (cc *commonCore) With(fields []zap.Field) zapcore.Core {
    33  	return cc.core.With(fields)
    34  }
    35  
    36  func (cc *commonCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
    37  	return cc.core.Check(ent, ce)
    38  }
    39  
    40  func (cc *commonCore) Write(ent zapcore.Entry, fields []zap.Field) error {
    41  	return cc.core.Write(ent, fields)
    42  }
    43  
    44  func (cc *commonCore) Sync() error {
    45  	return cc.core.Sync()
    46  }
    47  
    48  func WrapCore(core xLogCore, cfg zapcore.EncoderConfig) (xLogCore, error) {
    49  	if core == nil {
    50  		return nil, infra.NewErrorStack("[XLogger] empty core to wrap")
    51  	}
    52  	cfg.EncodeLevel = core.levelEncoder()
    53  	cfg.EncodeTime = core.timeEncoder()
    54  
    55  	cc := &commonCore{
    56  		ctx: core.context(),
    57  		ws:  core.writeSyncer(),
    58  		enc: core.outEncoder(),
    59  		lvlEnabler: zap.LevelEnablerFunc(func(l zapcore.Level) bool {
    60  			return core.Enabled(l)
    61  		}),
    62  		lvlEnc: core.levelEncoder(),
    63  		tsEnc:  core.timeEncoder(),
    64  	}
    65  	cc.core = zapcore.NewCore(core.outEncoder()(cfg), core.writeSyncer(), cc.lvlEnabler)
    66  	return cc, nil
    67  }
    68  
    69  func WrapCoreNewLevelEnabler(core xLogCore, lvlEnabler zapcore.LevelEnabler, cfg zapcore.EncoderConfig) (xLogCore, error) {
    70  	if core == nil {
    71  		return nil, infra.NewErrorStack("[XLogger] empty core to wrap")
    72  	}
    73  	cfg.EncodeLevel = core.levelEncoder()
    74  	cfg.EncodeTime = core.timeEncoder()
    75  
    76  	cc := &commonCore{
    77  		ws:  core.writeSyncer(),
    78  		enc: core.outEncoder(),
    79  		lvlEnabler: zap.LevelEnablerFunc(func(l zapcore.Level) bool {
    80  			return lvlEnabler.Enabled(l)
    81  		}),
    82  		lvlEnc: core.levelEncoder(),
    83  		tsEnc:  core.timeEncoder(),
    84  	}
    85  	cc.core = zapcore.NewCore(core.outEncoder()(cfg), core.writeSyncer(), cc.lvlEnabler)
    86  	return cc, nil
    87  }
    88  
    89  var (
    90  	componentCoreEncoderCfg = func() zapcore.EncoderConfig {
    91  		return zapcore.EncoderConfig{
    92  			MessageKey:    "msg",
    93  			LevelKey:      "lvl",
    94  			TimeKey:       "ts",
    95  			CallerKey:     coreKeyIgnored,
    96  			EncodeCaller:  zapcore.ShortCallerEncoder,
    97  			FunctionKey:   coreKeyIgnored,
    98  			NameKey:       "component",
    99  			EncodeName:    zapcore.FullNameEncoder,
   100  			StacktraceKey: coreKeyIgnored,
   101  		}
   102  	}
   103  	defaultCoreEncoderCfg = func() zapcore.EncoderConfig {
   104  		return zapcore.EncoderConfig{
   105  			MessageKey:    "msg",
   106  			LevelKey:      "lvl",
   107  			TimeKey:       "ts",
   108  			CallerKey:     "callAt",
   109  			EncodeCaller:  zapcore.ShortCallerEncoder,
   110  			FunctionKey:   "fn",
   111  			NameKey:       coreKeyIgnored,
   112  			EncodeName:    zapcore.FullNameEncoder,
   113  			StacktraceKey: coreKeyIgnored,
   114  		}
   115  	}
   116  )