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

     1  package log
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/alphadose/haxmap"
     7  )
     8  
     9  // Fields is a wrapper for zerolog.Entry
    10  // we need to insert some sentry captures here
    11  type Fields struct {
    12  	kv *haxmap.Map[string, any]
    13  }
    14  
    15  // WithFunc is short for WithField
    16  func WithFunc(fname string) *Fields {
    17  	return WithField("func", fname)
    18  }
    19  
    20  // WithField add kv into log entry
    21  func WithField(key string, value any) *Fields {
    22  	r := haxmap.New[string, any]()
    23  	r.Set(key, value)
    24  	return &Fields{
    25  		kv: r,
    26  	}
    27  }
    28  
    29  // WithField .
    30  func (f *Fields) WithField(key string, value any) *Fields {
    31  	f.kv.Set(key, value)
    32  	return f
    33  }
    34  
    35  // Fatalf forwards to sentry
    36  func (f Fields) Fatalf(ctx context.Context, err error, format string, args ...any) {
    37  	fatalf(ctx, err, format, f.kv, args...)
    38  }
    39  
    40  // Warnf is Warnf
    41  func (f Fields) Warnf(ctx context.Context, format string, args ...any) {
    42  	warnf(ctx, format, f.kv, args...)
    43  }
    44  
    45  // Warn is Warn
    46  func (f Fields) Warn(ctx context.Context, args ...any) {
    47  	f.Warnf(ctx, "%+v", args...)
    48  }
    49  
    50  // Infof is Infof
    51  func (f Fields) Infof(ctx context.Context, format string, args ...any) {
    52  	infof(ctx, format, f.kv, args...)
    53  }
    54  
    55  // Info is Info
    56  func (f Fields) Info(ctx context.Context, args ...any) {
    57  	f.Infof(ctx, "%+v", args...)
    58  }
    59  
    60  // Debugf is Debugf
    61  func (f Fields) Debugf(ctx context.Context, format string, args ...any) {
    62  	debugf(ctx, format, f.kv, args...)
    63  }
    64  
    65  // Debug is Debug
    66  func (f Fields) Debug(ctx context.Context, args ...any) {
    67  	f.Debugf(ctx, "%+v", args...)
    68  }
    69  
    70  // Errorf forwards to sentry
    71  func (f Fields) Errorf(ctx context.Context, err error, format string, args ...any) {
    72  	errorf(ctx, err, format, f.kv, args...)
    73  }
    74  
    75  // Error forwards to sentry
    76  func (f Fields) Error(ctx context.Context, err error, args ...any) {
    77  	f.Errorf(ctx, err, "%+v", args...)
    78  }