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

     1  package xlog
     2  
     3  import (
     4  	"go.uber.org/fx/fxevent"
     5  	"go.uber.org/zap"
     6  	"go.uber.org/zap/zapcore"
     7  )
     8  
     9  type FxXLogger struct {
    10  	logger XLogger
    11  }
    12  
    13  func (l *FxXLogger) LogEvent(event fxevent.Event) {
    14  	if l == nil || l.logger == nil {
    15  		return
    16  	}
    17  
    18  	switch e := event.(type) {
    19  	case *fxevent.OnStartExecuting:
    20  		l.logger.Debug("HOOK OnStart",
    21  			zap.String("function", e.FunctionName),
    22  			zap.String("caller", e.CallerName),
    23  		)
    24  	case *fxevent.OnStartExecuted:
    25  		if e.Err != nil {
    26  			l.logger.Error(e.Err, "HOOK OnStart failed",
    27  				zap.String("function", e.FunctionName),
    28  				zap.String("caller", e.CallerName),
    29  				zap.Int64("in", int64(e.Runtime)),
    30  			)
    31  		} else {
    32  			l.logger.Debug("HOOK OnStart successfully",
    33  				zap.String("function", e.FunctionName),
    34  				zap.String("caller", e.CallerName),
    35  				zap.Int64("in", int64(e.Runtime)),
    36  			)
    37  		}
    38  	case *fxevent.OnStopExecuting:
    39  		l.logger.Info("Hook OnStop executing",
    40  			zap.String("function", e.FunctionName),
    41  			zap.String("caller", e.CallerName),
    42  		)
    43  	case *fxevent.OnStopExecuted:
    44  		if e.Err != nil {
    45  			l.logger.Error(e.Err, "HOOK OnStop executed failed",
    46  				zap.String("function", e.FunctionName),
    47  				zap.String("caller", e.CallerName),
    48  				zap.Int64("in", int64(e.Runtime)),
    49  			)
    50  		} else {
    51  			l.logger.Info("Hook OnStop executed ran successfully",
    52  				zap.String("function", e.FunctionName),
    53  				zap.String("caller", e.CallerName),
    54  				zap.Int64("in", int64(e.Runtime)),
    55  			)
    56  		}
    57  	case *fxevent.Supplied:
    58  		if e.Err != nil {
    59  			l.logger.Error(e.Err, "SUPPLY ERROR",
    60  				zap.String("type", e.TypeName),
    61  				zap.Strings("stacktrace", e.StackTrace),
    62  			)
    63  		} else if e.ModuleName != "" {
    64  			l.logger.Debug("SUPPLY type from module",
    65  				zap.String("type", e.TypeName),
    66  				zap.String("module", e.ModuleName),
    67  			)
    68  		} else {
    69  			l.logger.Debug("SUPPLY type only",
    70  				zap.String("type", e.TypeName),
    71  			)
    72  		}
    73  	case *fxevent.Provided:
    74  		for _, rtype := range e.OutputTypeNames {
    75  			if e.ModuleName != "" {
    76  				l.logger.Debug("PROVIDE rtype from module",
    77  					zap.Bool("PRIVATE", e.Private),
    78  					zap.String("rtype", rtype),
    79  					zap.String("constructor", e.ConstructorName),
    80  					zap.String("module", e.ModuleName),
    81  				)
    82  			} else {
    83  				l.logger.Debug("PROVIDE rtype from constructor",
    84  					zap.Bool("PRIVATE", e.Private),
    85  					zap.String("rtype", rtype),
    86  					zap.String("constructor", e.ConstructorName),
    87  				)
    88  			}
    89  		}
    90  		if e.Err != nil {
    91  			l.logger.Error(e.Err, "Error after options were applied",
    92  				zap.Strings("stacktrace", e.StackTrace),
    93  			)
    94  		}
    95  	case *fxevent.Replaced:
    96  		for _, rtype := range e.OutputTypeNames {
    97  			if e.ModuleName != "" {
    98  				l.logger.Debug("REPLACE rtype from module",
    99  					zap.String("rtype", rtype),
   100  					zap.String("module", e.ModuleName),
   101  				)
   102  			} else {
   103  				l.logger.Debug("REPLACE rtype",
   104  					zap.String("rtype", rtype),
   105  				)
   106  			}
   107  		}
   108  		if e.Err != nil {
   109  			l.logger.Error(e.Err, "ERROR Failed to replace",
   110  				zap.Strings("stacktrace", e.StackTrace),
   111  			)
   112  		}
   113  	case *fxevent.Decorated:
   114  		for _, rtype := range e.OutputTypeNames {
   115  			if e.ModuleName != "" {
   116  				l.logger.Debug("DECORATE rtype from module",
   117  					zap.String("rtype", rtype),
   118  					zap.String("decorate", e.DecoratorName),
   119  					zap.String("module", e.ModuleName),
   120  				)
   121  			} else {
   122  				l.logger.Debug("DECORATE rtype",
   123  					zap.String("rtype", rtype),
   124  					zap.String("decorate", e.DecoratorName),
   125  				)
   126  			}
   127  		}
   128  		if e.Err != nil {
   129  			l.logger.Error(e.Err, "Error after decorated options were applied",
   130  				zap.Strings("stacktrace", e.StackTrace),
   131  			)
   132  		}
   133  	case *fxevent.Invoking:
   134  		if e.ModuleName != "" {
   135  			l.logger.Debug("INVOKING function from module",
   136  				zap.String("function", e.FunctionName),
   137  				zap.String("module", e.ModuleName),
   138  			)
   139  		} else {
   140  			l.logger.Debug("INVOKING", zap.String("function", e.FunctionName))
   141  		}
   142  	case *fxevent.Invoked:
   143  		if e.Err != nil {
   144  			l.logger.Error(e.Err, "Error fx.Invoke",
   145  				zap.String("function", e.FunctionName),
   146  				zap.String("trace", e.Trace),
   147  			)
   148  		}
   149  	case *fxevent.Stopping:
   150  		l.logger.Info("STOPPING", zap.String("signal", e.Signal.String()))
   151  	case *fxevent.Stopped:
   152  		if e.Err != nil {
   153  			l.logger.Error(e.Err, "Failed to stop cleanly")
   154  		}
   155  	case *fxevent.RollingBack:
   156  		l.logger.Warn("Start failed, rolling back",
   157  			zap.Error(e.StartErr),
   158  		)
   159  	case *fxevent.RolledBack:
   160  		if e.Err != nil {
   161  			l.logger.Error(e.Err, "Couldn't roll back cleanly")
   162  		}
   163  	case *fxevent.Started:
   164  		if e.Err != nil {
   165  			l.logger.Error(e.Err, "Failed to start")
   166  		} else {
   167  			l.logger.Debug("RUNNING")
   168  		}
   169  	case *fxevent.LoggerInitialized:
   170  		if e.Err != nil {
   171  			l.logger.Error(e.Err, "Failed to initialize custom logger")
   172  		} else {
   173  			l.logger.Debug("LOGGER Initialized custom logger", zap.String("constructor", e.ConstructorName))
   174  		}
   175  	}
   176  }
   177  
   178  func NewFxXLogger(logger XLogger) *FxXLogger {
   179  	l := &xLogger{}
   180  	l.logger.Store(logger.
   181  		zap().
   182  		Named("Fx").
   183  		WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
   184  			if core == nil {
   185  				panic("[XLogger] core is nil")
   186  			}
   187  			cc, ok := core.(xLogCore)
   188  			if !ok {
   189  				panic("[XLogger] core is not XLogCore")
   190  			}
   191  			var err error
   192  			if mc, ok := cc.(xLogMultiCore); ok && mc != nil {
   193  				if cc, err = WrapCores(mc, componentCoreEncoderCfg()); err != nil {
   194  					panic(err)
   195  				}
   196  			} else {
   197  				if cc, err = WrapCore(cc, componentCoreEncoderCfg()); err != nil {
   198  					panic(err)
   199  				}
   200  			}
   201  			return cc
   202  		})),
   203  	)
   204  	return &FxXLogger{logger: l}
   205  }