Skip to content

Commit a9bba97

Browse files
committed
make log thread safe
1 parent 8191d74 commit a9bba97

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

log/field.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package log
22

3-
import "context"
3+
import (
4+
"context"
5+
6+
"github.com/alphadose/haxmap"
7+
)
48

59
// Fields is a wrapper for zerolog.Entry
610
// we need to insert some sentry captures here
711
type Fields struct {
8-
kv map[string]interface{}
12+
kv *haxmap.Map[string, interface{}]
913
}
1014

1115
// WithFunc is short for WithField
@@ -15,14 +19,16 @@ func WithFunc(fname string) *Fields {
1519

1620
// WithField add kv into log entry
1721
func WithField(key string, value interface{}) *Fields {
22+
r := haxmap.New[string, interface{}]()
23+
r.Set(key, value)
1824
return &Fields{
19-
kv: map[string]interface{}{key: value},
25+
kv: r,
2026
}
2127
}
2228

2329
// WithField .
2430
func (f *Fields) WithField(key string, value interface{}) *Fields {
25-
f.kv[key] = value
31+
f.kv.Set(key, value)
2632
return f
2733
}
2834

log/inner.go

+28-10
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,44 @@ package log
33
import (
44
"context"
55

6+
"github.com/alphadose/haxmap"
67
"github.com/getsentry/sentry-go"
8+
"github.com/rs/zerolog"
79
)
810

9-
func fatalf(ctx context.Context, err error, format string, fields map[string]interface{}, args ...interface{}) {
11+
func fatalf(ctx context.Context, err error, format string, fields *haxmap.Map[string, interface{}], args ...interface{}) {
1012
args = argsValidate(args)
1113
reportToSentry(ctx, sentry.LevelFatal, err, format, args...)
12-
globalLogger.Fatal().Fields(fields).Err(err).Msgf(format, args...)
14+
f := globalLogger.Fatal()
15+
wrap(f, fields).Err(err).Msgf(format, args...)
1316
}
1417

15-
func warnf(_ context.Context, format string, fields map[string]interface{}, args ...interface{}) {
18+
func warnf(_ context.Context, format string, fields *haxmap.Map[string, interface{}], args ...interface{}) {
1619
args = argsValidate(args)
17-
globalLogger.Warn().Fields(fields).Msgf(format, args...)
20+
f := globalLogger.Warn()
21+
wrap(f, fields).Msgf(format, args...)
1822
}
1923

20-
func infof(_ context.Context, format string, fields map[string]interface{}, args ...interface{}) {
24+
func infof(_ context.Context, format string, fields *haxmap.Map[string, interface{}], args ...interface{}) {
2125
args = argsValidate(args)
22-
globalLogger.Info().Fields(fields).Msgf(format, args...)
26+
f := globalLogger.Info()
27+
wrap(f, fields).Msgf(format, args...)
2328
}
2429

25-
func debugf(_ context.Context, format string, fields map[string]interface{}, args ...interface{}) {
30+
func debugf(_ context.Context, format string, fields *haxmap.Map[string, interface{}], args ...interface{}) {
2631
args = argsValidate(args)
27-
globalLogger.Debug().Fields(fields).Msgf(format, args...)
32+
f := globalLogger.Debug()
33+
wrap(f, fields).Msgf(format, args...)
2834
}
2935

30-
func errorf(ctx context.Context, err error, format string, fields map[string]interface{}, args ...interface{}) {
36+
func errorf(ctx context.Context, err error, format string, fields *haxmap.Map[string, interface{}], args ...interface{}) {
3137
if err == nil {
3238
return
3339
}
3440
args = argsValidate(args)
3541
reportToSentry(ctx, sentry.LevelError, err, format, args...)
36-
globalLogger.Error().Fields(fields).Stack().Err(err).Msgf(format, args...)
42+
f := globalLogger.Error()
43+
wrap(f, fields).Stack().Err(err).Msgf(format, args...)
3744
}
3845

3946
func argsValidate(args []interface{}) []interface{} {
@@ -42,3 +49,14 @@ func argsValidate(args []interface{}) []interface{} {
4249
}
4350
return []interface{}{""}
4451
}
52+
53+
func wrap(f *zerolog.Event, kv *haxmap.Map[string, interface{}]) *zerolog.Event {
54+
if kv == nil {
55+
return f
56+
}
57+
kv.ForEach(func(k string, v interface{}) bool {
58+
f = f.Interface(k, v)
59+
return true
60+
})
61+
return f
62+
}

0 commit comments

Comments
 (0)