github.com/wfusion/gofusion@v1.1.14/log/candy.go (about)

     1  package log
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"go.uber.org/zap"
     8  )
     9  
    10  func Debug(ctx context.Context, format string, args ...any) { globalLogger.Debug(ctx, format, args...) }
    11  func Info(ctx context.Context, format string, args ...any)  { globalLogger.Info(ctx, format, args...) }
    12  func Warn(ctx context.Context, format string, args ...any)  { globalLogger.Warn(ctx, format, args...) }
    13  func Error(ctx context.Context, format string, args ...any) { globalLogger.Error(ctx, format, args...) }
    14  func Panic(ctx context.Context, format string, args ...any) { globalLogger.Panic(ctx, format, args...) }
    15  func Fatal(ctx context.Context, format string, args ...any) { globalLogger.Fatal(ctx, format, args...) }
    16  
    17  func TimeElapsed(ctx context.Context, logger Loggable, fn func(), format string, args ...any) {
    18  	now := time.Now()
    19  	defer func() {
    20  		elapsed := time.Since(now).Milliseconds()
    21  		if r := recover(); r != nil {
    22  			panic(r)
    23  		}
    24  		if logger != nil {
    25  			logger.Info(ctx, format, append(args, zap.Any("latency", elapsed))...)
    26  		} else {
    27  			Info(ctx, format, append(args, zap.Any("latency", elapsed))...)
    28  		}
    29  	}()
    30  
    31  	fn()
    32  }