Skip to content

Commit

Permalink
API-12106 allow custom logger v6 (#127)
Browse files Browse the repository at this point in the history
* API-12106 add custom logger

* API-12106 change definition of default log

* API-12106 add logging legacy/deprecated versions
  • Loading branch information
maciejjwojcik authored Jan 12, 2023
1 parent 841b160 commit 6abebaa
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/web_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"time"
Expand Down Expand Up @@ -38,6 +39,7 @@ type api struct {
customHeaders http.Header
retryStrategy RetryStrategyFunc
statsSink StatsSinkFunc
logger *log.Logger
}

// HTTPRequestGenerator is called by each API method to generate api http url.
Expand Down Expand Up @@ -70,6 +72,7 @@ func NewAPI(t authorization.TokenGetter, client *http.Client, clientID string, r
httpEndpointGenerator: r,
customHeaders: make(http.Header),
statsSink: func(metrics.APICallStats) {},
logger: log.Default(),
}, nil
}

Expand Down Expand Up @@ -252,6 +255,13 @@ func (a *api) send(req *http.Request, respPayload interface{}) error {
return err
}

if h := resp.Header.Get("Legacy"); h != "" {
a.logger.Printf("[Notice] This is a legacy version. It will be deprecated after %s.", h)
}
if h := resp.Header.Get("Deprecation"); h != "" {
a.logger.Printf("[Warning] This version is deprecated. It will be decommissioned after %s.", h)
}

return json.Unmarshal(bodyBytes, respPayload)
}

Expand Down Expand Up @@ -281,3 +291,8 @@ func DefaultHTTPRequestGenerator(name string) HTTPEndpointGenerator {
return fmt.Sprintf("%s/v%s/%s/action/%s", host, apiVersion, name, action)
}
}

// SetLogger allows to set a custom logger. When it's not called, a default logger will be used.
func (a *api) SetLogger(logger *log.Logger) {
a.logger = logger
}

0 comments on commit 6abebaa

Please sign in to comment.