forked from growthbook/growthbook-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
142 lines (121 loc) · 4.44 KB
/
logging.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package growthbook
import (
"fmt"
"os"
)
// Message constants used for logging.
const (
ErrJSONFailedToParse = "failed parsing JSON input"
ErrJSONInvalidType = "invalid JSON data type"
ErrCtxJSONInvalidURL = "invalid URL in JSON context data"
ErrExpJSONInvalidCondition = "invalid condition in JSON experiment data"
ErrCondJSONNot = "invalid $not in JSON condition data"
ErrCondJSONSequence = "something wrong in condition sequence"
ErrCondJSONSequenceElement = "something wrong in condition sequence element"
WarnJSONUnknownKey = "unknown key in JSON data"
WarnCondCompareTypeMismatch = "types don't match in condition comparison operation"
WarnExpJSONKeyNotSet = "key not set in JSON experiment data"
WarnExpCoverageMustBePositive = "Experiment coverage must be greater than or equal to 0"
WarnExpCoverageMustBeFraction = "Experiment coverage must be less than or equal to 1"
WarnExpWeightsWrongLength = "Experiment weights and variations arrays must be the same length"
WarnExpWeightsWrongTotal = "Experiment weights must add up to 1"
WarnRuleSkipHashAttributeType = "Skip rule because of non-string hash attribute"
InfoRuleSkipCondition = "Skip rule because of condition"
InfoRuleSkipNoHashAttribute = "Skip rule because of missing hash attribute"
InfoRuleSkipEmptyHashAttribute = "Skip rule because of empty hash attribute"
InfoRuleSkipCoverage = "Skip rule because of coverage"
InfoRuleSkipUserNotInExp = "Skip rule because user not in experiment"
)
// Logger is a common interface for logging information and warning
// messages (errors are returned directly by SDK functions, but there
// is some useful "out of band" data that's provided via this
// interface).
type Logger interface {
Error(msg string, args ...interface{})
Errorf(format string, args ...interface{})
Warn(msg string, args ...interface{})
Warnf(format string, args ...interface{})
Info(msg string, args ...interface{})
Infof(format string, args ...interface{})
}
// SetLogger sets up the logging interface used throughout. The idea
// here is to provide developers with the option of handling errors
// and warnings in a strict way during development and a lenient way
// in production. For example, in development, setting a logger that
// prints a message for all logged output and panics on any logged
// warning or error might be appropriate, while in production, it
// would make more sense to log only warnings and errors and to
// proceed without halting. All GrowthBook SDK functions leave values
// in a sensible default state after errors, so production systems can
// essentially ignore any errors.
func SetLogger(userLogger Logger) {
logger = userLogger
}
// Global private logging interface.
var logger Logger
// Internal logging functions wired up to logging interface.
func logError(msg string, args ...interface{}) {
if logger != nil {
logger.Error(msg, args...)
}
}
func logErrorf(format string, args ...interface{}) {
if logger != nil {
logger.Errorf(format, args...)
}
}
func logWarn(msg string, args ...interface{}) {
if logger != nil {
logger.Warn(msg, args...)
}
}
func logWarnf(format string, args ...interface{}) {
if logger != nil {
logger.Warnf(format, args...)
}
}
func logInfo(msg string, args ...interface{}) {
if logger != nil {
logger.Info(msg, args...)
}
}
func logInfof(format string, args ...interface{}) {
if logger != nil {
logger.Infof(format, args...)
}
}
// DevLogger is a logger instance suitable for use in development. It
// prints all logged messages to standard output, and exits on errors.
type DevLogger struct{}
func (log *DevLogger) Error(msg string, args ...interface{}) {
fmt.Print("[ERROR] ", msg)
if len(args) > 0 {
fmt.Print(": ")
fmt.Println(args...)
}
os.Exit(1)
}
func (log *DevLogger) Errorf(format string, args ...interface{}) {
fmt.Printf("[ERROR] "+format+"\n", args...)
os.Exit(1)
}
func (log *DevLogger) Warn(msg string, args ...interface{}) {
fmt.Print("[WARNING] ", msg)
if len(args) > 0 {
fmt.Print(": ")
fmt.Println(args...)
}
}
func (log *DevLogger) Warnf(format string, args ...interface{}) {
fmt.Printf("[WARNING] "+format+"\n", args...)
}
func (log *DevLogger) Info(msg string, args ...interface{}) {
fmt.Print("[INFO] ", msg)
if len(args) > 0 {
fmt.Print(": ")
fmt.Println(args...)
}
}
func (log *DevLogger) Infof(format string, args ...interface{}) {
fmt.Printf("[INFO] "+format+"\n", args...)
}