forked from statsig-io/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_logger.go
70 lines (61 loc) · 1.4 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
package statsig
import (
"encoding/json"
"fmt"
"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(msg, err)
} else {
formatted := msg
if err != nil {
if formatted != "" {
formatted += "\n"
}
formatted += err.Error()
}
if formatted != "" {
fmt.Print(formatted)
}
}
}
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
}
timestamp := time.Now().Format(time.RFC3339)
o.Log(fmt.Sprintf("[%s][Statsig] %s: %s\n", timestamp, 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:
fmt.Print(err)
}
}
func (o *OutputLogger) isInitialized() bool {
return o != nil
}