-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslog.go
62 lines (50 loc) · 1.3 KB
/
slog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package slog
import (
goslog "log/slog"
"os"
"strings"
"sync"
"go.k6.io/k6/js/modules"
)
func init() {
var s Slog
logFormat := os.Getenv("K6_LOG_FORMAT")
if strings.EqualFold(logFormat, "json") {
s.logger = goslog.New(goslog.NewJSONHandler(os.Stdout, nil))
} else {
s.logger = goslog.New(goslog.NewTextHandler(os.Stdout, nil))
}
modules.Register("k6/x/slog", &s)
}
type Slog struct {
logger *goslog.Logger
extraFields sync.Map
}
// SetPersistentField adds a field to the logs that stay for all the logs
// Useful to add test run id or tagging metadata
func (s *Slog) SetPersistentField(fieldName string, value string) {
if _, ok := s.extraFields.Load(fieldName); !ok {
s.logger = s.logger.With(fieldName, value)
s.extraFields.Store(fieldName, struct{}{})
}
}
// Log creates an info log
func (s *Slog) Log(msg string, args ...any) {
s.logger.Info(msg, args...)
}
// Debug creates a debug log
func (s *Slog) Debug(msg string, args ...any) {
s.logger.Debug(msg, args...)
}
// Info creates an info log
func (s *Slog) Info(msg string, args ...any) {
s.logger.Info(msg, args...)
}
// Warn creates a warn log
func (s *Slog) Warn(msg string, args ...any) {
s.logger.Warn(msg, args...)
}
// Error creates an error log
func (s *Slog) Error(msg string, args ...any) {
s.logger.Error(msg, args...)
}