-
Notifications
You must be signed in to change notification settings - Fork 10
/
output_logger.go
80 lines (68 loc) · 1.74 KB
/
output_logger.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package statsig
import (
"encoding/json"
"fmt"
"os"
"regexp"
"time"
)
type StatsigProcess string
const (
StatsigProcessInitialize StatsigProcess = "Initialize"
StatsigProcessSync StatsigProcess = "Sync"
)
type OutputLogger struct {
options OutputLoggerOptions
}
func (o *OutputLogger) Log(msg string, err error) {
if o.isInitialized() && o.options.LogCallback != nil {
o.options.LogCallback(sanitize(msg), err)
} else {
timestamp := time.Now().Format(time.RFC3339)
formatted := fmt.Sprintf("[%s][Statsig] %s", timestamp, msg)
sanitized := ""
if err != nil {
formatted += err.Error()
sanitized = sanitize(formatted)
fmt.Fprintln(os.Stderr, sanitized)
} else if msg != "" {
sanitized = sanitize(formatted)
fmt.Println(sanitized)
}
}
}
func (o *OutputLogger) Debug(any interface{}) {
bytes, _ := json.MarshalIndent(any, "", " ")
msg := fmt.Sprintf("%+v\n", string(bytes))
o.Log(msg, nil)
}
func (o *OutputLogger) LogStep(process StatsigProcess, msg string) {
if !o.isInitialized() || !o.options.EnableDebug {
return
}
if o.options.DisableInitDiagnostics && process == StatsigProcessInitialize {
return
}
if o.options.DisableSyncDiagnostics && process == StatsigProcessSync {
return
}
o.Log(fmt.Sprintf("%s: %s", process, msg), nil)
}
func (o *OutputLogger) LogError(err interface{}) {
switch errTyped := err.(type) {
case string:
o.Log(errTyped, nil)
case error:
o.Log("", errTyped)
default:
sanitized := sanitize(fmt.Sprintf("%+v", err))
fmt.Fprintln(os.Stderr, sanitized)
}
}
func (o *OutputLogger) isInitialized() bool {
return o != nil
}
func sanitize(string string) string {
keyPattern := regexp.MustCompile(`secret-[a-zA-Z0-9]+`)
return keyPattern.ReplaceAllString(string, "secret-****")
}