Skip to content

Commit 5b0dd71

Browse files
authored
Fix de/ser tags on launchdarkly config (#200)
* Add json tags on launchdarkly config * Add yaml tags * Handle levels in launchdarkly logger wrapper
1 parent 74c4816 commit 5b0dd71

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

featureflag/config.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import (
77
)
88

99
type Config struct {
10-
Key string
11-
RequestTimeout time.Duration `mapstructure:"request_timeout" split_words:"true" default:"5s"`
12-
Enabled bool `default:"false"`
13-
updateProcessorFactory ld.UpdateProcessorFactory
10+
Key string `json:"key" yaml:"key"`
11+
RequestTimeout time.Duration `json:"request_timeout" yaml:"request_timeout" mapstructure:"request_timeout" split_words:"true" default:"5s"`
12+
Enabled bool `json:"enabled" yaml:"enabled" default:"false"`
13+
14+
updateProcessorFactory ld.UpdateProcessorFactory `json:"-"`
1415

1516
// Drop telemetry events (not needed in local-dev/CI environments)
16-
DisableEvents bool `mapstructure:"disable_events" split_words:"true"`
17+
DisableEvents bool `json:"disable_events" yaml:"disable_events" mapstructure:"disable_events" split_words:"true"`
1718

1819
// Set when using the Launch Darkly Relay proxy
19-
RelayHost string `mapstructure:"relay_host" split_words:"true"`
20+
RelayHost string `json:"relay_host" yaml:"relay_host" mapstructure:"relay_host" split_words:"true"`
2021
}

featureflag/featureflag.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/sirupsen/logrus"
77

88
ld "gopkg.in/launchdarkly/go-server-sdk.v4"
9+
"gopkg.in/launchdarkly/go-server-sdk.v4/ldlog"
910
)
1011

1112
type Client interface {
@@ -37,7 +38,7 @@ func NewClient(cfg *Config, logger logrus.FieldLogger) (Client, error) {
3738
config.SendEvents = false
3839
}
3940

40-
config.Loggers.SetBaseLogger(wrapLogger(logger))
41+
configureLogger(config.Loggers, logger)
4142

4243
if cfg.RelayHost != "" {
4344
config.BaseUri = cfg.RelayHost
@@ -97,23 +98,29 @@ func (c *ldClient) AllEnabledFlags(key string) []string {
9798
return flags
9899
}
99100

100-
func wrapLogger(logger logrus.FieldLogger) infoToDebugLogger {
101-
if logger == nil {
101+
func configureLogger(ldLogger ldlog.Loggers, log logrus.FieldLogger) {
102+
if log == nil {
102103
l := logrus.New()
103104
l.SetOutput(ioutil.Discard)
104-
logger = l
105+
log = l
105106
}
107+
log = log.WithField("component", "launch_darkly")
106108

107-
return infoToDebugLogger{logger.WithField("component", "launch_darkly")}
109+
ldLogger.SetBaseLoggerForLevel(ldlog.Debug, &wrapLog{log.Debugln, log.Debugf})
110+
ldLogger.SetBaseLoggerForLevel(ldlog.Info, &wrapLog{log.Infoln, log.Infof})
111+
ldLogger.SetBaseLoggerForLevel(ldlog.Warn, &wrapLog{log.Warnln, log.Warnf})
112+
ldLogger.SetBaseLoggerForLevel(ldlog.Error, &wrapLog{log.Errorln, log.Errorf})
108113
}
109114

110-
type infoToDebugLogger struct {
111-
log logrus.FieldLogger
115+
type wrapLog struct {
116+
println func(values ...interface{})
117+
printf func(format string, values ...interface{})
112118
}
113119

114-
func (l infoToDebugLogger) Println(values ...interface{}) {
115-
l.log.Debugln(values...)
120+
func (l *wrapLog) Println(values ...interface{}) {
121+
l.println(values...)
116122
}
117-
func (l infoToDebugLogger) Printf(format string, values ...interface{}) {
118-
l.log.Debugf(format, values...)
123+
124+
func (l *wrapLog) Printf(format string, values ...interface{}) {
125+
l.printf(format, values...)
119126
}

0 commit comments

Comments
 (0)