github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/xlog/tee_core.go (about) 1 package xlog 2 3 import ( 4 "context" 5 6 "go.uber.org/multierr" 7 "go.uber.org/zap" 8 "go.uber.org/zap/zapcore" 9 10 "github.com/benz9527/xboot/lib/infra" 11 ) 12 13 var _ xLogCore = (xLogMultiCore)(nil) 14 15 type xLogMultiCore []xLogCore 16 17 func (mc xLogMultiCore) context() context.Context { 18 return nil 19 } 20 21 func (mc xLogMultiCore) levelEncoder() zapcore.LevelEncoder { 22 return nil 23 } 24 25 func (mc xLogMultiCore) outEncoder() func(cfg zapcore.EncoderConfig) zapcore.Encoder { 26 return nil 27 } 28 29 func (mc xLogMultiCore) timeEncoder() zapcore.TimeEncoder { 30 return nil 31 } 32 33 // writeSyncer implements xLogCore. 34 func (mc xLogMultiCore) writeSyncer() zapcore.WriteSyncer { 35 return nil 36 } 37 38 func (mc xLogMultiCore) With(fields []zap.Field) zapcore.Core { 39 clone := make([]zapcore.Core, len(mc)) 40 for i := range mc { 41 clone[i] = mc[i].With(fields) 42 } 43 return zapcore.NewTee(clone...) 44 } 45 46 func (mc xLogMultiCore) Level() zapcore.Level { 47 var minLvl = zapcore.InfoLevel 48 for i := range mc { 49 if lvl := zapcore.LevelOf(mc[i]); lvl < minLvl { 50 minLvl = lvl 51 } 52 } 53 return minLvl 54 } 55 56 func (mc xLogMultiCore) Enabled(lvl zapcore.Level) bool { 57 for i := range mc { 58 if mc[i].Enabled(lvl) { 59 return true 60 } 61 } 62 return false 63 } 64 65 func (mc xLogMultiCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry { 66 for i := range mc { 67 ce = mc[i].Check(ent, ce) 68 } 69 return ce 70 } 71 72 func (mc xLogMultiCore) Write(ent zapcore.Entry, fields []zap.Field) error { 73 var err error 74 for i := range mc { 75 err = multierr.Append(err, mc[i].Write(ent, fields)) 76 } 77 return err 78 } 79 80 func (mc xLogMultiCore) Sync() error { 81 var err error 82 for i := range mc { 83 err = multierr.Append(err, mc[i].Sync()) 84 } 85 return err 86 } 87 88 func XLogTeeCore(cores ...xLogCore) xLogCore { 89 return xLogMultiCore(cores) 90 } 91 92 func WrapCores(cores []xLogCore, cfg zapcore.EncoderConfig) (xLogCore, error) { 93 if cores == nil || len(cores) == 0 { 94 return nil, infra.NewErrorStack("[XLogger] empty cores to wrap") 95 } 96 newCores := make([]xLogCore, 0, len(cores)) 97 for i := range cores { 98 newCore, err := WrapCore(cores[i], cfg) 99 if err != nil { 100 return nil, err 101 } 102 newCores = append(newCores, newCore) 103 } 104 return xLogMultiCore(newCores), nil 105 } 106 107 func WrapCoresNewLevelEnabler(cores []xLogCore, lvlEnabler zapcore.LevelEnabler, cfg zapcore.EncoderConfig) (xLogCore, error) { 108 if cores == nil || len(cores) == 0 { 109 return nil, infra.NewErrorStack("[XLogger] empty cores to wrap") 110 } 111 newCores := make([]xLogCore, 0, len(cores)) 112 for i := range cores { 113 newCore, err := WrapCoreNewLevelEnabler(cores[i], lvlEnabler, cfg) 114 if err != nil { 115 return nil, err 116 } 117 newCores = append(newCores, newCore) 118 } 119 return xLogMultiCore(newCores), nil 120 }