Skip to content

Commit

Permalink
Refactor logging writer
Browse files Browse the repository at this point in the history
  • Loading branch information
loafoe committed Feb 8, 2023
1 parent 24bb96b commit 1a62c3c
Show file tree
Hide file tree
Showing 45 changed files with 91 additions and 283 deletions.
10 changes: 2 additions & 8 deletions ai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"net/http"
"net/url"
"os"
"path"
"strings"

Expand All @@ -33,7 +32,7 @@ type Config struct {
OrganizationID string `Validate:"required"`
BaseURL string
Service string `Validate:"required"`
DebugLog string
DebugLog io.Writer
Retry int
}

Expand All @@ -47,8 +46,7 @@ type Client struct {
// User agent used when communicating with the HSDP Notification API
UserAgent string

debugFile *os.File
validate *validator.Validate
validate *validator.Validate

ComputeTarget *ComputeTargetService
ComputeProvider *ComputeProviderService
Expand Down Expand Up @@ -89,10 +87,6 @@ func doAutoconf(config *Config) {

// Close releases allocated resources of clients
func (c *Client) Close() {
if c.debugFile != nil {
_ = c.debugFile.Close()
c.debugFile = nil
}
}

// GetBaseURL returns the base URL as configured
Expand Down
2 changes: 1 addition & 1 deletion ai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func TestDebug(t *testing.T) {

aiClient, err = ai.NewClient(iamClient, &ai.Config{
BaseURL: serverAI.URL,
DebugLog: tempFile.Name(),
DebugLog: tempFile,
Service: "inference",
OrganizationID: "xxx",
})
Expand Down
2 changes: 1 addition & 1 deletion ai/inference/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestDebug(t *testing.T) {

inferenceClient, err = inference.NewClient(iamClient, &ai.Config{
BaseURL: serverInference.URL,
DebugLog: tempFile.Name(),
DebugLog: tempFile,
OrganizationID: "xxx",
})
if !assert.Nil(t, err) {
Expand Down
2 changes: 1 addition & 1 deletion ai/training/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestDebug(t *testing.T) {

trainingClient, err = training.NewClient(iamClient, &ai.Config{
BaseURL: serverTraining.URL,
DebugLog: tmpfile.Name(),
DebugLog: tmpfile,
OrganizationID: "xxx",
})
if !assert.Nil(t, err) {
Expand Down
2 changes: 1 addition & 1 deletion ai/workspace/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestDebug(t *testing.T) {

workspaceClient, err = workspace.NewClient(iamClient, &ai.Config{
BaseURL: serverWorkspace.URL,
DebugLog: tmpfile.Name(),
DebugLog: tmpfile,
OrganizationID: "xxx",
})
if !assert.Nil(t, err) {
Expand Down
17 changes: 3 additions & 14 deletions audit/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"net/http"
"net/url"
"os"
"strings"

"github.com/google/fhir/go/fhirversion"
Expand Down Expand Up @@ -38,7 +37,7 @@ type Config struct {
// SharedSecret is the IAM API signing secret
SharedSecret string
TimeZone string
DebugLog string
DebugLog io.Writer
}

// Client holds state of a HSDP Audit client
Expand All @@ -53,8 +52,6 @@ type Client struct {
ma *jsonformat.Marshaller
um *jsonformat.Unmarshaller
httpSigner *signer.Signer

debugFile *os.File
}

// NewClient returns a new HSDP Audit API client. Configured console and IAM clients
Expand All @@ -76,12 +73,8 @@ func newClient(httpClient *http.Client, config *Config) (*Client, error) {
}

c := &Client{httpClient: httpClient, config: config, UserAgent: userAgent}
if config.DebugLog != "" {
var err error
c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err == nil {
httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, c.debugFile)
}
if config.DebugLog != nil {
httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, config.DebugLog)
}
c.httpSigner, err = signer.New(c.config.SharedKey, c.config.SharedSecret)
if err != nil {
Expand All @@ -104,10 +97,6 @@ func newClient(httpClient *http.Client, config *Config) (*Client, error) {

// Close releases allocated resources of clients
func (c *Client) Close() {
if c.debugFile != nil {
_ = c.debugFile.Close()
c.debugFile = nil
}
}

func (c *Client) setAuditBaseURL(urlStr string) error {
Expand Down
10 changes: 2 additions & 8 deletions blr/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"net/http"
"net/url"
"os"
"path"
"strings"

Expand All @@ -32,7 +31,7 @@ type Config struct {
Region string
Environment string
BaseURL string
DebugLog string
DebugLog io.Writer
Retry int
}

Expand All @@ -46,8 +45,7 @@ type Client struct {
// User agent used when communicating with the HSDP Blob Repository API
UserAgent string

debugFile *os.File
validate *validator.Validate
validate *validator.Validate

Blobs *BlobsService
}
Expand Down Expand Up @@ -89,10 +87,6 @@ func doAutoconf(config *Config) {

// Close releases allocated resources of clients
func (c *Client) Close() {
if c.debugFile != nil {
_ = c.debugFile.Close()
c.debugFile = nil
}
}

// GetBaseURL returns the base URL as configured
Expand Down
26 changes: 9 additions & 17 deletions cartel/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"io"
"net/http"
"net/url"
"os"
"strings"

"github.com/philips-software/go-hsdp-api/internal"
Expand All @@ -28,14 +27,13 @@ const (

// Config the client
type Config struct {
Region string `cloud:"-" json:"-"`
Token string `cloud:"token" json:"token"`
Secret string `cloud:"secret" json:"secret"`
SkipVerify bool `cloud:"skip_verify" json:"skip_verify"`
NoTLS bool `cloud:"no_tls" json:"no_tls"`
Host string `cloud:"host" json:"host"`
Debug bool `cloud:"-" json:"debug,omitempty"`
DebugLog string `cloud:"-" json:"debug_log,omitempty"`
Region string `cloud:"-" json:"-"`
Token string `cloud:"token" json:"token"`
Secret string `cloud:"secret" json:"secret"`
SkipVerify bool `cloud:"skip_verify" json:"skip_verify"`
NoTLS bool `cloud:"no_tls" json:"no_tls"`
Host string `cloud:"host" json:"host"`
DebugLog io.Writer `cloud:"-" json:"-"`
}

// Valid returns if all required config fields are present, false otherwise
Expand All @@ -58,8 +56,6 @@ type Client struct {
httpClient *http.Client
baseURL *url.URL
userAgent string

debugFile *os.File
}

// Response holds a Cartel response
Expand Down Expand Up @@ -121,12 +117,8 @@ func NewClient(httpClient *http.Client, config *Config) (*Client, error) {
cartel.httpClient = httpClient
cartel.userAgent = userAgent

if config.DebugLog != "" {
var err error
cartel.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err == nil {
httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, cartel.debugFile)
}
if config.DebugLog != nil {
httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, config.DebugLog)
}

// Make sure the given URL ends with a slash
Expand Down
3 changes: 1 addition & 2 deletions cartel/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ func TestDebug(t *testing.T) {
Token: sharedToken,
Secret: sharedSecret,
Host: "foo",
Debug: true,
DebugLog: tmpfile.Name(),
DebugLog: tmpfile,
})
if !assert.Nil(t, err) {
return
Expand Down
10 changes: 2 additions & 8 deletions cdl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"net/http"
"net/url"
"os"
"strings"

"github.com/go-playground/validator/v10"
Expand All @@ -33,7 +32,7 @@ type Config struct {
OrganizationID string `validate:"required"`
CDLURL string
CDLStore string
DebugLog string
DebugLog io.Writer
Retry int
}

Expand All @@ -51,8 +50,7 @@ type Client struct {
// User agent used when communicating with the HSDP Notification API
UserAgent string

debugFile *os.File
validate *validator.Validate
validate *validator.Validate

Study *StudyService
DataTypeDefinition *DatatypeDefinitionService
Expand Down Expand Up @@ -128,10 +126,6 @@ func doAutoconf(config *Config) {

// Close releases allocated resources of clients
func (c *Client) Close() {
if c.debugFile != nil {
_ = c.debugFile.Close()
c.debugFile = nil
}
}

// GetCDLURL returns the base CDL Store base URL as configured
Expand Down
2 changes: 1 addition & 1 deletion cdl/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestDebug(t *testing.T) {

cdlClient, err = cdl.NewClient(iamClient, &cdl.Config{
CDLStore: serverCDL.URL,
DebugLog: tmpfile.Name(),
DebugLog: tmpfile,
})
if !assert.Nil(t, err) {
return
Expand Down
2 changes: 1 addition & 1 deletion cdr/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Config struct {
FHIRStore string
Type string
TimeZone string
DebugLog string
DebugLog io.Writer
}

// A Client manages communication with HSDP CDR API
Expand Down
2 changes: 1 addition & 1 deletion cdr/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestDebug(t *testing.T) {

cdrClient, err = cdr.NewClient(iamClient, &cdr.Config{
CDRURL: serverCDR.URL,
DebugLog: tmpfile.Name(),
DebugLog: tmpfile,
})
if !assert.Nil(t, err) {
return
Expand Down
10 changes: 2 additions & 8 deletions connect/mdm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"net/http"
"net/url"
"os"
"path"
"strings"

Expand All @@ -31,7 +30,7 @@ type Config struct {
Region string
Environment string
BaseURL string
DebugLog string
DebugLog io.Writer
Retry int
}

Expand All @@ -48,8 +47,7 @@ type Client struct {
systemIDM string
systemIAM string

debugFile *os.File
validate *validator.Validate
validate *validator.Validate

Propositions *PropositionsService
Applications *ApplicationsService
Expand Down Expand Up @@ -142,10 +140,6 @@ func doAutoconf(config *Config) {

// Close releases allocated resources of clients
func (c *Client) Close() {
if c.debugFile != nil {
_ = c.debugFile.Close()
c.debugFile = nil
}
}

// GetBaseURL returns the base URL as configured
Expand Down
22 changes: 4 additions & 18 deletions console/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -131,7 +130,6 @@ type Client struct {

Metrics *MetricsService

debugFile *os.File
consoleErr error

sync.Mutex
Expand Down Expand Up @@ -166,24 +164,16 @@ func newClient(httpClient *http.Client, config *Config) (*Client, error) {
if err := c.SetBaseConsoleURL(c.config.BaseConsoleURL); err != nil {
c.consoleErr = err
}
if config.DebugLog != "" {
var err error
c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err == nil {
httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, c.debugFile)
}
if config.DebugLog != nil {
httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, config.DebugLog)
}
header := make(http.Header)
header.Set("User-Agent", userAgent)
httpClient.Transport = internal.NewHeaderRoundTripper(httpClient.Transport, header)

authClient := oauth2.NewClient(context.Background(), c)
if config.DebugLog != "" {
var err error
c.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err == nil {
authClient.Transport = internal.NewLoggingRoundTripper(authClient.Transport, c.debugFile)
}
if config.DebugLog != nil {
authClient.Transport = internal.NewLoggingRoundTripper(authClient.Transport, config.DebugLog)
}
// Injecting these headers so we satisfy the proxies
authClient.Transport = internal.NewHeaderRoundTripper(authClient.Transport, header, func(req *http.Request) error {
Expand Down Expand Up @@ -221,10 +211,6 @@ func doAutoconf(config *Config) {

// Close releases allocated resources of clients
func (c *Client) Close() {
if c.debugFile != nil {
_ = c.debugFile.Close()
c.debugFile = nil
}
}

// HttpClient returns the http Client used for connections
Expand Down
2 changes: 1 addition & 1 deletion console/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestDebug(t *testing.T) {
UAAURL: serverUAA.URL,
BaseConsoleURL: serverCONSOLE.URL,
Debug: true,
DebugLog: tmpfile.Name(),
DebugLog: tmpfile,
})

if err != nil {
Expand Down
Loading

0 comments on commit 1a62c3c

Please sign in to comment.