Skip to content

Commit

Permalink
refactor: make logging on clients accessible from config
Browse files Browse the repository at this point in the history
  • Loading branch information
ispiroglu committed Sep 20, 2024
1 parent 92dabfb commit 1bbe30a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/client-server-with-otel/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type exampleClient struct {

func newClient(f *client.Factory) *exampleClient {
return &exampleClient{
Base: f.Get("example-client", client.DefaultErrDecoder),
Base: f.Get("example-client"),
}
}

Expand Down
2 changes: 1 addition & 1 deletion example/client-server/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type exampleClient struct {

func newClient(f *client.Factory) *exampleClient {
return &exampleClient{
Base: f.Get("example-client", client.DefaultErrDecoder),
Base: f.Get("example-client"),
}
}

Expand Down
3 changes: 3 additions & 0 deletions example/client-server/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Trendyol/chaki/modules/server"
"github.com/Trendyol/chaki/modules/server/controller"
"github.com/Trendyol/chaki/modules/server/route"
"github.com/Trendyol/chaki/modules/swagger"
)

func main() {
Expand All @@ -17,6 +18,8 @@ func main() {
app.Use(
client.Module(),
server.Module(),

swagger.Module(),
)

app.Provide(
Expand Down
19 changes: 17 additions & 2 deletions modules/client/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@ type driverBuilder struct {
}

func newDriverBuilder(cfg *config.Config) *driverBuilder {
setDefaults(cfg)

d := resty.New().
SetBaseURL(cfg.GetString("baseurl")).
SetTimeout(cfg.GetDuration("timeout")).
// Debug mode provides a logging, but it's not in the same format with our logger.
SetDebug(cfg.GetBool("debug"))
return &driverBuilder{
cfg: cfg,
d: resty.New().SetBaseURL(cfg.GetString("baseurl")),
d: d,
}
}

Expand All @@ -32,7 +39,9 @@ func (b *driverBuilder) AddUpdaters(wrappers ...DriverWrapper) *driverBuilder {
}

func (b *driverBuilder) build() *resty.Client {
b.useLogging()
if b.cfg.GetBool("logging") {
b.useLogging()
}

for _, upd := range b.updaters {
b.d = upd(b.d)
Expand Down Expand Up @@ -68,3 +77,9 @@ func (b *driverBuilder) useLogging() {
return nil
})
}

func setDefaults(cfg *config.Config) {
cfg.SetDefault("timeout", "5s")
cfg.SetDefault("debug", false)
cfg.SetDefault("logging", false)
}

0 comments on commit 1bbe30a

Please sign in to comment.