Skip to content

Commit

Permalink
Fix de/ser tags on launchdarkly config (#200)
Browse files Browse the repository at this point in the history
* Add json tags on launchdarkly config

* Add yaml tags

* Handle levels in launchdarkly logger wrapper
  • Loading branch information
mraerino authored Dec 15, 2020
1 parent 74c4816 commit 5b0dd71
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
13 changes: 7 additions & 6 deletions featureflag/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
)

type Config struct {
Key string
RequestTimeout time.Duration `mapstructure:"request_timeout" split_words:"true" default:"5s"`
Enabled bool `default:"false"`
updateProcessorFactory ld.UpdateProcessorFactory
Key string `json:"key" yaml:"key"`
RequestTimeout time.Duration `json:"request_timeout" yaml:"request_timeout" mapstructure:"request_timeout" split_words:"true" default:"5s"`
Enabled bool `json:"enabled" yaml:"enabled" default:"false"`

updateProcessorFactory ld.UpdateProcessorFactory `json:"-"`

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

// Set when using the Launch Darkly Relay proxy
RelayHost string `mapstructure:"relay_host" split_words:"true"`
RelayHost string `json:"relay_host" yaml:"relay_host" mapstructure:"relay_host" split_words:"true"`
}
29 changes: 18 additions & 11 deletions featureflag/featureflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/sirupsen/logrus"

ld "gopkg.in/launchdarkly/go-server-sdk.v4"
"gopkg.in/launchdarkly/go-server-sdk.v4/ldlog"
)

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

config.Loggers.SetBaseLogger(wrapLogger(logger))
configureLogger(config.Loggers, logger)

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

func wrapLogger(logger logrus.FieldLogger) infoToDebugLogger {
if logger == nil {
func configureLogger(ldLogger ldlog.Loggers, log logrus.FieldLogger) {
if log == nil {
l := logrus.New()
l.SetOutput(ioutil.Discard)
logger = l
log = l
}
log = log.WithField("component", "launch_darkly")

return infoToDebugLogger{logger.WithField("component", "launch_darkly")}
ldLogger.SetBaseLoggerForLevel(ldlog.Debug, &wrapLog{log.Debugln, log.Debugf})
ldLogger.SetBaseLoggerForLevel(ldlog.Info, &wrapLog{log.Infoln, log.Infof})
ldLogger.SetBaseLoggerForLevel(ldlog.Warn, &wrapLog{log.Warnln, log.Warnf})
ldLogger.SetBaseLoggerForLevel(ldlog.Error, &wrapLog{log.Errorln, log.Errorf})
}

type infoToDebugLogger struct {
log logrus.FieldLogger
type wrapLog struct {
println func(values ...interface{})
printf func(format string, values ...interface{})
}

func (l infoToDebugLogger) Println(values ...interface{}) {
l.log.Debugln(values...)
func (l *wrapLog) Println(values ...interface{}) {
l.println(values...)
}
func (l infoToDebugLogger) Printf(format string, values ...interface{}) {
l.log.Debugf(format, values...)

func (l *wrapLog) Printf(format string, values ...interface{}) {
l.printf(format, values...)
}

0 comments on commit 5b0dd71

Please sign in to comment.