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

     1  package xlog
     2  
     3  import (
     4  	"go.uber.org/zap"
     5  	"go.uber.org/zap/zapcore"
     6  )
     7  
     8  type AntsXLogger struct {
     9  	logger XLogger
    10  }
    11  
    12  func (l *AntsXLogger) Printf(format string, args ...any) {
    13  	if l == nil || l.logger == nil {
    14  		return
    15  	}
    16  	l.logger.Logf(zapcore.ErrorLevel, format, args...)
    17  }
    18  
    19  func NewAntsXLogger(logger XLogger) *AntsXLogger {
    20  	l := &xLogger{}
    21  	l.logger.Store(logger.
    22  		zap().
    23  		Named("Ants").
    24  		WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
    25  			if core == nil {
    26  				panic("[XLogger] core is nil")
    27  			}
    28  			cc, ok := core.(xLogCore)
    29  			if !ok {
    30  				panic("[XLogger] core is not XLogCore")
    31  			}
    32  			var err error
    33  			if mc, ok := cc.(xLogMultiCore); ok && mc != nil {
    34  				if cc, err = WrapCores(mc, componentCoreEncoderCfg()); err != nil {
    35  					panic(err)
    36  				}
    37  			} else {
    38  				if cc, err = WrapCore(cc, componentCoreEncoderCfg()); err != nil {
    39  					panic(err)
    40  				}
    41  			}
    42  			return cc
    43  		})),
    44  	)
    45  	return &AntsXLogger{
    46  		logger: l,
    47  	}
    48  }