github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/log/inner.go (about)

     1  package log
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/alphadose/haxmap"
     7  	"github.com/getsentry/sentry-go"
     8  	"github.com/rs/zerolog"
     9  )
    10  
    11  func fatalf(ctx context.Context, err error, format string, fields *haxmap.Map[string, any], args ...any) {
    12  	args = argsValidate(args)
    13  	reportToSentry(ctx, sentry.LevelFatal, err, format, args...)
    14  	f := globalLogger.Fatal()
    15  	wrap(f, fields).Err(err).Msgf(format, args...)
    16  }
    17  
    18  func warnf(_ context.Context, format string, fields *haxmap.Map[string, any], args ...any) {
    19  	args = argsValidate(args)
    20  	f := globalLogger.Warn()
    21  	wrap(f, fields).Msgf(format, args...)
    22  }
    23  
    24  func infof(_ context.Context, format string, fields *haxmap.Map[string, any], args ...any) {
    25  	args = argsValidate(args)
    26  	f := globalLogger.Info()
    27  	wrap(f, fields).Msgf(format, args...)
    28  }
    29  
    30  func debugf(_ context.Context, format string, fields *haxmap.Map[string, any], args ...any) {
    31  	args = argsValidate(args)
    32  	f := globalLogger.Debug()
    33  	wrap(f, fields).Msgf(format, args...)
    34  }
    35  
    36  func errorf(ctx context.Context, err error, format string, fields *haxmap.Map[string, any], args ...any) {
    37  	if err == nil {
    38  		return
    39  	}
    40  	args = argsValidate(args)
    41  	reportToSentry(ctx, sentry.LevelError, err, format, args...)
    42  	f := globalLogger.Error()
    43  	wrap(f, fields).Stack().Err(err).Msgf(format, args...)
    44  }
    45  
    46  func argsValidate(args []any) []any {
    47  	if len(args) > 0 {
    48  		return args
    49  	}
    50  	return []any{""}
    51  }
    52  
    53  func wrap(f *zerolog.Event, kv *haxmap.Map[string, any]) *zerolog.Event {
    54  	if kv == nil {
    55  		return f
    56  	}
    57  	kv.ForEach(func(k string, v any) bool {
    58  		f = f.Interface(k, v)
    59  		return true
    60  	})
    61  	return f
    62  }