Skip to content

Commit a9286a9

Browse files
Merge pull request #112 from harness/FFM-7327-use-custom-logger-for-http-client
(FFM-7327) Use custom logger for startup and http requests
2 parents 789042f + e2b1911 commit a9286a9

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

client/client.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"log"
89
"net/http"
910
"strings"
1011
"sync"
1112
"sync/atomic"
1213
"time"
1314

1415
"github.com/harness/ff-golang-server-sdk/evaluation"
16+
"github.com/harness/ff-golang-server-sdk/logger"
1517

1618
"github.com/harness/ff-golang-server-sdk/pkg/repository"
1719

@@ -36,7 +38,6 @@ import (
3638
// When an application is shutting down or no longer needs to use the CfClient instance, it
3739
// should call Close() to ensure that all of its connections and goroutines are shut down and
3840
// that any pending analytics events have been delivered.
39-
//
4041
type CfClient struct {
4142
evaluator *evaluation.Evaluator
4243
repository repository.Repository
@@ -65,7 +66,7 @@ type CfClient struct {
6566
func NewCfClient(sdkKey string, options ...ConfigOption) (*CfClient, error) {
6667

6768
// functional options for config
68-
config := newDefaultConfig()
69+
config := newDefaultConfig(getLogger(options...))
6970
for _, opt := range options {
7071
opt(config)
7172
}
@@ -497,3 +498,19 @@ func (a *atomicBool) set(value bool) {
497498
func (a *atomicBool) get() bool {
498499
return atomic.LoadInt32(&(a.flag)) != int32(0)
499500
}
501+
502+
// getLogger returns either the custom passed in logger or our default zap logger
503+
func getLogger(options ...ConfigOption) logger.Logger {
504+
dummyConfig := &config{}
505+
for _, opt := range options {
506+
opt(dummyConfig)
507+
}
508+
if dummyConfig.Logger == nil {
509+
defaultLogger, err := logger.NewZapLogger(false)
510+
if err != nil {
511+
log.Printf("Error creating zap logger instance, %v", err)
512+
}
513+
dummyConfig.Logger = defaultLogger
514+
}
515+
return dummyConfig.Logger
516+
}

client/config.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package client
22

33
import (
4-
"log"
54
"net/http"
65

76
"github.com/harness/ff-golang-server-sdk/evaluation"
@@ -28,24 +27,21 @@ type config struct {
2827
enableAnalytics bool
2928
}
3029

31-
func newDefaultConfig() *config {
32-
defaultLogger, err := logger.NewZapLogger(false)
33-
if err != nil {
34-
log.Printf("Error creating zap logger instance, %v", err)
35-
}
36-
defaultCache, _ := cache.NewLruCache(10000, defaultLogger) // size of cache
37-
defaultStore := storage.NewFileStore("defaultProject", storage.GetHarnessDir(), defaultLogger)
30+
func newDefaultConfig(log logger.Logger) *config {
31+
defaultCache, _ := cache.NewLruCache(10000, log) // size of cache
32+
defaultStore := storage.NewFileStore("defaultProject", storage.GetHarnessDir(), log)
3833

3934
retryClient := retryablehttp.NewClient()
4035
retryClient.RetryMax = 10
36+
retryClient.Logger = logger.NewRetryableLogger(log)
4137

4238
return &config{
4339
url: "https://config.ff.harness.io/api/1.0",
4440
eventsURL: "https://events.ff.harness.io/api/1.0",
4541
pullInterval: 60,
4642
Cache: defaultCache,
4743
Store: defaultStore,
48-
Logger: defaultLogger,
44+
Logger: log,
4945
httpClient: retryClient.StandardClient(),
5046
enableStream: true,
5147
enableStore: true,

logger/retryablelogger.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package logger
2+
3+
// RetryableLogger implements the Logger interface required by the go-retryablehttp client and wraps our internal logger
4+
type RetryableLogger struct {
5+
logger Logger
6+
}
7+
8+
// NewRetryableLogger creates a RetryableLogger instance.
9+
func NewRetryableLogger(logger Logger) RetryableLogger {
10+
s := RetryableLogger{}
11+
s.logger = logger
12+
return s
13+
}
14+
15+
// Printf - logs any printf messages from the http client as debug logs
16+
func (s RetryableLogger) Printf(string string, args ...interface{}) {
17+
s.logger.Debugf(string, args...)
18+
}

0 commit comments

Comments
 (0)