github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/xlog/console_core.go (about) 1 package xlog 2 3 import ( 4 "context" 5 6 "go.uber.org/zap" 7 "go.uber.org/zap/zapcore" 8 ) 9 10 var _ xLogCore = (*consoleCore)(nil) 11 12 type consoleCore struct { 13 core *commonCore 14 } 15 16 func (cc *consoleCore) timeEncoder() zapcore.TimeEncoder { return cc.core.tsEnc } 17 func (cc *consoleCore) levelEncoder() zapcore.LevelEncoder { return cc.core.lvlEnc } 18 func (cc *consoleCore) writeSyncer() zapcore.WriteSyncer { return cc.core.ws } 19 func (cc *consoleCore) outEncoder() func(cfg zapcore.EncoderConfig) zapcore.Encoder { 20 return cc.core.enc 21 } 22 func (cc *consoleCore) context() context.Context { return cc.core.ctx } 23 func (cc *consoleCore) Enabled(lvl zapcore.Level) bool { return cc.core.lvlEnabler.Enabled(lvl) } 24 func (cc *consoleCore) With(fields []zap.Field) zapcore.Core { return cc.core.With(fields) } 25 func (cc *consoleCore) Sync() error { return cc.core.Sync() } 26 func (cc *consoleCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry { 27 return cc.core.Check(ent, ce) 28 } 29 30 func (cc *consoleCore) Write(ent zapcore.Entry, fields []zap.Field) error { 31 return cc.core.Write(ent, fields) 32 } 33 34 func newConsoleCore( 35 ctx context.Context, 36 lvlEnabler zapcore.LevelEnabler, 37 encoder logEncoderType, 38 lvlEnc zapcore.LevelEncoder, 39 tsEnc zapcore.TimeEncoder, 40 ) xLogCore { 41 if ctx == nil { 42 return nil 43 } 44 cc := &consoleCore{ 45 core: &commonCore{ 46 ctx: ctx, 47 lvlEnabler: lvlEnabler, 48 lvlEnc: lvlEnc, 49 tsEnc: tsEnc, 50 ws: getOutWriterByType(StdOut), 51 enc: getEncoderByType(encoder), 52 }, 53 } 54 55 cfg := defaultCoreEncoderCfg() 56 cfg.EncodeLevel = cc.core.lvlEnc 57 cfg.EncodeTime = cc.core.tsEnc 58 cc.core.core = zapcore.NewCore(cc.core.enc(cfg), cc.core.ws, cc.core.lvlEnabler) 59 return cc 60 }