Skip to content

Commit

Permalink
feat: Support levelled logs using slog in go module. (#1955)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspoignant authored Jun 11, 2024
1 parent a2e12be commit 306b117
Show file tree
Hide file tree
Showing 56 changed files with 1,193 additions and 432 deletions.
4 changes: 2 additions & 2 deletions cmd/relayproxy/controller/all_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/metric"
"io"
"log"
"log/slog"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -81,7 +81,7 @@ func Test_all_flag_Handler(t *testing.T) {
// init go-feature-flag
goFF, _ := ffclient.New(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &fileretriever.Retriever{
Path: tt.args.configFlagsLocation,
Expand Down
4 changes: 2 additions & 2 deletions cmd/relayproxy/controller/collect_eval_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package controller_test
import (
"context"
"io"
"log"
"log/slog"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -96,7 +96,7 @@ func Test_collect_eval_data_Handler(t *testing.T) {
// init go-feature-flag
goFF, _ := ffclient.New(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &fileretriever.Retriever{
Path: configFlagsLocation,
Expand Down
4 changes: 2 additions & 2 deletions cmd/relayproxy/controller/flag_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/metric"
"io"
"io/ioutil"
"log"
"log/slog"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -149,7 +149,7 @@ func Test_flag_eval_Handler(t *testing.T) {
// init go-feature-flag
goFF, _ := ffclient.New(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &fileretriever.Retriever{
Path: configFlagsLocation,
Expand Down
2 changes: 0 additions & 2 deletions cmd/relayproxy/ofrep/configuration_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ofrep_test

import (
"fmt"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
ffclient "github.com/thomaspoignant/go-feature-flag"
Expand Down Expand Up @@ -63,7 +62,6 @@ func Test_Configuration(t *testing.T) {
req := httptest.NewRequest(echo.GET, "/ofrep/v1/configuration", nil)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
e.ServeHTTP(rec, req)
fmt.Println(rec.Body.String())
assert.Equal(t, tt.want.httpCode, rec.Code, "Invalid HTTP Code")
assert.JSONEq(t, tt.want.response, rec.Body.String(), "Invalid response wantBody")
})
Expand Down
9 changes: 3 additions & 6 deletions cmd/relayproxy/ofrep/evaluate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package ofrep_test

import (
"context"
"fmt"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/metric"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/ofrep"
"io"
"log"
"log/slog"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -92,7 +91,7 @@ func Test_Bulk_Evaluation(t *testing.T) {
// init go-feature-flag
goFF, _ := ffclient.New(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &fileretriever.Retriever{
Path: tt.args.configFlagsLocation,
Expand Down Expand Up @@ -138,8 +137,6 @@ func Test_Bulk_Evaluation(t *testing.T) {

wantBody, err := os.ReadFile(tt.want.bodyFile)

fmt.Println(rec.Header())

assert.NoError(t, err, "Impossible the expected wantBody file %s", tt.want.bodyFile)
assert.Equal(t, tt.want.httpCode, rec.Code, "Invalid HTTP Code")
assert.JSONEq(t, string(wantBody), rec.Body.String(), "Invalid response wantBody")
Expand Down Expand Up @@ -242,7 +239,7 @@ func Test_Evaluate(t *testing.T) {
// init go-feature-flag
goFF, _ := ffclient.New(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &fileretriever.Retriever{
Path: tt.args.configFlagsLocation,
Expand Down
6 changes: 4 additions & 2 deletions cmd/relayproxy/service/gofeatureflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package service

import (
"fmt"
"log/slog"
"time"

awsConf "github.com/aws/aws-sdk-go-v2/config"
slogzap "github.com/samber/slog-zap/v2"
ffclient "github.com/thomaspoignant/go-feature-flag"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/config"
"github.com/thomaspoignant/go-feature-flag/exporter"
Expand Down Expand Up @@ -53,7 +55,7 @@ func NewGoFeatureFlagClient(
}
}

// Manage if we have more than 1 retriver
// Manage if we have more than 1 retriever
retrievers := make([]retriever.Retriever, 0)
if proxyConf.Retrievers != nil {
for _, r := range *proxyConf.Retrievers {
Expand Down Expand Up @@ -85,7 +87,7 @@ func NewGoFeatureFlagClient(

f := ffclient.Config{
PollingInterval: time.Duration(proxyConf.PollingInterval) * time.Millisecond,
Logger: zap.NewStdLog(logger),
LeveledLogger: slog.New(slogzap.Option{Level: slog.LevelDebug, Logger: logger}.NewZapHandler()),
Context: context.Background(),
Retriever: mainRetriever,
Retrievers: retrievers,
Expand Down
13 changes: 12 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package ffclient
import (
"context"
"errors"
"github.com/thomaspoignant/go-feature-flag/utils/fflog"
"log"
"log/slog"
"sync"
"time"

Expand All @@ -27,15 +29,20 @@ type Config struct {
// Default: false
EnablePollingJitter bool

// Deprecated: Use LeveledLogger instead
// Logger (optional) logger use by the library
// Default: No log
Logger *log.Logger

// LeveledLogger (optional) logger use by the library
// Default: No log
LeveledLogger *slog.Logger

// Context (optional) used to call other services (HTTP, S3 ...)
// Default: context.Background()
Context context.Context

// Environment (optional), can be checked in feature flag rules
// Environment (optional) can be checked in feature flag rules
// Default: ""
Environment string

Expand Down Expand Up @@ -81,6 +88,10 @@ type Config struct {

// offlineMutex is a mutex to protect the Offline field.
offlineMutex *sync.RWMutex

// internalLogger is the logger used by the library everywhere
// this logger is a superset of the logging system to be able to migrate easily to slog.
internalLogger *fflog.FFLogger
}

// GetRetrievers returns a retriever.Retriever configure with the retriever available in the config.
Expand Down
4 changes: 2 additions & 2 deletions examples/data_export_file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"context"
"log"
"os"
"log/slog"
"time"

"github.com/thomaspoignant/go-feature-flag/ffcontext"
Expand All @@ -18,7 +18,7 @@ func main() {
// Init ffclient with a file retriever.
err := ffclient.Init(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &fileretriever.Retriever{
Path: "examples/data_export_file/flags.goff.yaml",
Expand Down
4 changes: 2 additions & 2 deletions examples/data_export_googlecloudstorage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"context"
"log"
"os"
"log/slog"
"time"

"github.com/thomaspoignant/go-feature-flag/ffcontext"
Expand All @@ -26,7 +26,7 @@ func main() {
*/
err := ffclient.Init(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &fileretriever.Retriever{
Path: "examples/data_export_s3/flags.goff.yaml",
Expand Down
4 changes: 2 additions & 2 deletions examples/data_export_kafka/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"github.com/thomaspoignant/go-feature-flag/exporter/kafkaexporter"
"log"
"os"
"log/slog"
"time"

ffclient "github.com/thomaspoignant/go-feature-flag"
Expand All @@ -22,7 +22,7 @@ func main() {
*/
err := ffclient.Init(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &fileretriever.Retriever{
Path: "examples/data_export_s3/flags.goff.yaml",
Expand Down
4 changes: 2 additions & 2 deletions examples/data_export_s3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"context"
"log"
"os"
"log/slog"
"time"

"github.com/aws/aws-sdk-go-v2/config"
Expand All @@ -25,7 +25,7 @@ func main() {
awsConfig, _ := config.LoadDefaultConfig(context.Background())
err := ffclient.Init(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &fileretriever.Retriever{
Path: "examples/data_export_s3/flags.goff.yaml",
Expand Down
4 changes: 2 additions & 2 deletions examples/retriever_configmap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"github.com/thomaspoignant/go-feature-flag/ffcontext"
"log"
"log/slog"
"net/http"
"os"
"time"

"github.com/thomaspoignant/go-feature-flag/retriever/k8sretriever"
Expand All @@ -24,7 +24,7 @@ func main() {

err = ffclient.Init(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &k8sretriever.Retriever{
Namespace: "default",
Expand Down
4 changes: 2 additions & 2 deletions examples/retriever_file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"github.com/thomaspoignant/go-feature-flag/ffcontext"
"log"
"os"
"log/slog"
"time"

"github.com/thomaspoignant/go-feature-flag/exporter/fileexporter"
Expand All @@ -18,7 +18,7 @@ func main() {
// Init ffclient with a file retriever.
err := ffclient.Init(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &fileretriever.Retriever{
Path: "examples/retriever_file/flags.goff.yaml",
Expand Down
4 changes: 2 additions & 2 deletions examples/retriever_github/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"github.com/thomaspoignant/go-feature-flag/ffcontext"
"log"
"os"
"log/slog"
"time"

"github.com/thomaspoignant/go-feature-flag/retriever/githubretriever"
Expand All @@ -17,7 +17,7 @@ func main() {
// Init ffclient with a GitHub retriever.
err := ffclient.Init(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &githubretriever.Retriever{
RepositorySlug: "thomaspoignant/go-feature-flag",
Expand Down
4 changes: 2 additions & 2 deletions examples/retriever_http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"github.com/thomaspoignant/go-feature-flag/ffcontext"
"log"
"os"
"log/slog"
"time"

"github.com/thomaspoignant/go-feature-flag/retriever/httpretriever"
Expand All @@ -17,7 +17,7 @@ func main() {
// Init ffclient with a HTTP retriever.
err := ffclient.Init(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &httpretriever.Retriever{
URL: "https://raw.githubusercontent.com/thomaspoignant/go-feature-flag/main/examples/retriever_http/flags.goff.yaml",
Expand Down
8 changes: 4 additions & 4 deletions examples/retriever_mongodb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"
"log"
"os"
"log/slog"
"time"

"github.com/thomaspoignant/go-feature-flag/ffcontext"
Expand All @@ -19,12 +19,12 @@ func main() {
// Init ffclient with a file retriever.
err := ffclient.Init(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &mongodbretriever.Retriever{
Collection: "featureFlags",
Database: "appConfig",
URI: "mongodb://root:[email protected]:27017/",
Database: "appConfig",
URI: "mongodb://root:[email protected]:27017/",
},
DataExporter: ffclient.DataExporter{
FlushInterval: 1 * time.Second,
Expand Down
4 changes: 2 additions & 2 deletions examples/retriever_multiple_config_files/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/thomaspoignant/go-feature-flag/ffcontext"
"github.com/thomaspoignant/go-feature-flag/retriever"
"log"
"os"
"log/slog"
"time"

"github.com/thomaspoignant/go-feature-flag/exporter/fileexporter"
Expand All @@ -19,7 +19,7 @@ func main() {
// Init ffclient with multiple file retrievers.
err := ffclient.Init(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retrievers: []retriever.Retriever{
&fileretriever.Retriever{
Expand Down
4 changes: 2 additions & 2 deletions examples/retriever_s3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/thomaspoignant/go-feature-flag/ffcontext"
"github.com/thomaspoignant/go-feature-flag/retriever/s3retrieverv2"
"log"
"os"
"log/slog"
"time"

ffclient "github.com/thomaspoignant/go-feature-flag"
Expand All @@ -22,7 +22,7 @@ func main() {
}
err = ffclient.Init(ffclient.Config{
PollingInterval: 10 * time.Second,
Logger: log.New(os.Stdout, "", 0),
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &s3retrieverv2.Retriever{
Bucket: "goff-test",
Expand Down
Loading

0 comments on commit 306b117

Please sign in to comment.