Skip to content

Commit

Permalink
Remove logus for default logger and add windows support (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
doriac11 authored Dec 13, 2024
1 parent 451fce0 commit a319320
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 73 deletions.
7 changes: 0 additions & 7 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 14 additions & 14 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"math"
"net/http"
"os"
Expand All @@ -30,7 +31,6 @@ import (

"github.com/dell/goscaleio/api"
types "github.com/dell/goscaleio/types/v1"
log "github.com/sirupsen/logrus"
)

var (
Expand All @@ -44,6 +44,7 @@ var (

debug, _ = strconv.ParseBool(os.Getenv("GOSCALEIO_DEBUG"))
showHTTP, _ = strconv.ParseBool(os.Getenv("GOSCALEIO_SHOWHTTP"))
logger = slog.New(slog.NewTextHandler(os.Stderr, nil))
)

// Client defines struct for Client
Expand Down Expand Up @@ -79,7 +80,7 @@ func (c *Client) GetVersion() (string, error) {
}
defer func() {
if err := resp.Body.Close(); err != nil {
doLog(log.WithError(err).Error, "")
doLog(logger.Error, err.Error())
}
}()
// parse the response
Expand Down Expand Up @@ -147,13 +148,13 @@ func (c *Client) Authenticate(configConnect *ConfigConnect) (Cluster, error) {
resp, err := c.api.DoAndGetResponseBody(
context.Background(), http.MethodGet, "api/login", headers, nil, c.configConnect.Version)
if err != nil {
doLog(log.WithError(err).Error, "")
doLog(logger.Error, err.Error())
return Cluster{}, err
}

defer func() {
if err := resp.Body.Close(); err != nil {
doLog(log.WithError(err).Error, "")
doLog(logger.Error, err.Error())
}
}()

Expand Down Expand Up @@ -204,9 +205,9 @@ func (c *Client) getJSONWithRetry(

// check if we need to authenticate
if e, ok := err.(*types.Error); ok {
doLog(log.WithError(err).Debug, fmt.Sprintf("Got JSON error: %+v", e))
doLog(logger.Debug, fmt.Sprintf("Got JSON error: %+v", e))
if e.HTTPStatusCode == 401 {
doLog(log.Info, "Need to re-auth")
doLog(logger.Info, "Need to re-auth")
// Authenticate then try again
if _, err := c.Authenticate(c.configConnect); err != nil {
return fmt.Errorf("Error Authenticating: %s", err)
Expand All @@ -215,7 +216,7 @@ func (c *Client) getJSONWithRetry(
context.Background(), method, uri, headers, body, resp, c.configConnect.Version)
}
}
doLog(log.WithError(err).Error, "returning error")
doLog(logger.Error, err.Error())

return err
}
Expand Down Expand Up @@ -252,7 +253,7 @@ func (c *Client) getStringWithRetry(
checkResponse := func(resp *http.Response) (string, bool, error) {
defer func() {
if err := resp.Body.Close(); err != nil {
doLog(log.WithError(err).Error, "")
doLog(logger.Error, err.Error())
}
}()

Expand Down Expand Up @@ -282,7 +283,7 @@ func (c *Client) getStringWithRetry(
s, retry, httpErr := checkResponse(resp)
if httpErr != nil {
if retry {
doLog(log.Info, "need to re-auth")
doLog(logger.Info, "need to re-auth")
// Authenticate then try again
if _, err = c.Authenticate(c.configConnect); err != nil {
return "", fmt.Errorf("Error Authenticating: %s", err)
Expand Down Expand Up @@ -349,11 +350,10 @@ func NewClientWithArgs(
"debug": debug,
"showHTTP": showHTTP,
}

doLog(log.WithFields(fields).Debug, "goscaleio client init")
doLog(logger.Debug, fmt.Sprintf("goscaleio client init, Fields: %+v", fields))

if endpoint == "" {
doLog(log.WithFields(fields).Error, "endpoint is required")
doLog(logger.Error, fmt.Sprintf("endpoint is required, Fields: %+v", fields))
return nil,
withFields(fields, "endpoint is required")
}
Expand All @@ -371,7 +371,7 @@ func NewClientWithArgs(

ac, err := api.New(context.Background(), endpoint, opts, debug)
if err != nil {
doLog(log.WithError(err).Error, "Unable to create HTTP client")
doLog(logger.Error, fmt.Sprintf("Unable to create HTTP client: %s", err.Error()))
return nil, err
}

Expand Down Expand Up @@ -430,7 +430,7 @@ func withFieldsE(
}

func doLog(
l func(args ...interface{}),
l func(msg string, args ...any),
msg string,
) {
if debug {
Expand Down
15 changes: 7 additions & 8 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"

log "github.com/sirupsen/logrus"

types "github.com/dell/goscaleio/types/v1"
)

Expand All @@ -46,6 +46,7 @@ const (
var (
errNewClient = errors.New("missing endpoint")
errSysCerts = errors.New("Unable to initialize cert pool from system")
logger = slog.New(slog.NewTextHandler(os.Stderr, nil))
)

// Client is an API client.
Expand Down Expand Up @@ -271,7 +272,7 @@ func (c *client) DoWithHeaders(

defer func() {
if err := res.Body.Close(); err != nil {
c.doLog(log.WithError(err).Error, "")
c.doLog(logger.Error, err.Error())
}
}()

Expand All @@ -285,9 +286,7 @@ func (c *client) DoWithHeaders(
}
dec := json.NewDecoder(res.Body)
if err = dec.Decode(resp); err != nil && err != io.EOF {
c.doLog(log.WithError(err).Error,
fmt.Sprintf("Unable to decode response into %+v",
resp))
c.doLog(logger.Error, fmt.Sprintf("Error: %s Unable to decode response into %+v", err.Error(), resp))
return err
}
default:
Expand Down Expand Up @@ -340,7 +339,7 @@ func (c *client) DoAndGetResponseBody(

defer func() {
if err := r.Close(); err != nil {
c.doLog(log.WithError(err).Error, "")
c.doLog(logger.Error, err.Error())
}
}()

Expand Down Expand Up @@ -456,7 +455,7 @@ func (c *client) ParseJSONError(r *http.Response) error {
}

func (c *client) doLog(
l func(args ...interface{}),
l func(msg string, args ...any),
msg string,
) {
if c.debug {
Expand Down
22 changes: 8 additions & 14 deletions api/api_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"net/http"
"net/http/httputil"
"strings"

log "github.com/sirupsen/logrus"
)

func isBinOctetBody(h http.Header) bool {
Expand All @@ -32,10 +30,8 @@ func isBinOctetBody(h http.Header) bool {
func logRequest(
_ context.Context,
req *http.Request,
lf func(func(args ...interface{}), string),
lf func(func(msg string, args ...any), string),
) {
log.SetLevel(log.DebugLevel)

w := &bytes.Buffer{}

fmt.Fprintln(w)
Expand All @@ -51,19 +47,16 @@ func logRequest(
if err := WriteIndented(w, buf); err != nil {
fmt.Printf("WriteIndented returned error: %s", err.Error())
}

fmt.Fprintln(w)

lf(log.Debug, w.String())
if lf != nil {
lf(logger.Debug, w.String())
}
}

func logResponse(
_ context.Context,
res *http.Response,
_ func(func(args ...interface{}), string),
lf func(func(msg string, args ...any), string),
) {
log.SetLevel(log.DebugLevel)

w := &bytes.Buffer{}

fmt.Fprintln(w)
Expand All @@ -89,8 +82,9 @@ func logResponse(
}
fmt.Fprintln(w, scanner.Text())
}

log.Debug(w.String())
if lf != nil {
lf(logger.Debug, w.String())
}
}

// WriteIndentedN indents all lines n spaces.
Expand Down
12 changes: 3 additions & 9 deletions api/api_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"net/http"
"strings"
"testing"

log "github.com/sirupsen/logrus"
)

func TestIsBinOctetBody(t *testing.T) {
Expand Down Expand Up @@ -185,12 +183,8 @@ func TestLogResponse(_ *testing.T) {
logResponse(context.Background(), res, nil)
}

func logChecker(level log.Level, msg string) func(func(args ...interface{}), string) {
return func(lf func(args ...interface{}), message string) {
if level != log.DebugLevel {
// If the log level is not DebugLevel, call the logging function with an error message
lf(fmt.Sprintf("Expected debug level, got %v", level))
}
func logChecker(msg string) func(func(msg string, args ...any), string) {
return func(lf func(msg string, args ...any), message string) {
if !strings.Contains(msg, "GOSCALEIO HTTP REQUEST") {
// If the message does not contain the expected string, call the logging function with an error message
lf(fmt.Sprintf("Expected request log, got %s", msg))
Expand All @@ -207,7 +201,7 @@ func TestLogRequest(t *testing.T) {
if err != nil {
t.Fatal(err)
}
logFunc := logChecker(log.DebugLevel, "GOSCALEIO HTTP REQUEST")
logFunc := logChecker("GOSCALEIO HTTP REQUEST")
logRequest(context.Background(), req, logFunc)

// Test case: Error in dumpRequest
Expand Down
3 changes: 1 addition & 2 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (

"github.com/dell/goscaleio/api"
types "github.com/dell/goscaleio/types/v1"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -144,7 +143,7 @@ func (gc *GatewayClient) NewTokenGeneration() (string, error) {

defer func() {
if err := resp.Body.Close(); err != nil {
doLog(log.WithError(err).Error, "")
doLog(logger.Error, err.Error())
}
}()

Expand Down
2 changes: 2 additions & 0 deletions drv_cfg.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !windows

// Copyright © 2021 - 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ module github.com/dell/goscaleio
require (
github.com/google/uuid v1.6.0
github.com/joho/godotenv v1.5.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.27.0 // indirect
)

go 1.23
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand All @@ -7,17 +6,9 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2 changes: 2 additions & 0 deletions inttests/drv_cfg_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !windows

// Copyright © 2021 - 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
1 change: 0 additions & 1 deletion inttests/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"

"github.com/dell/goscaleio"
// log "github.com/sirupsen/logrus"
types "github.com/dell/goscaleio/types/v1"
"github.com/stretchr/testify/assert"
)
Expand Down
3 changes: 0 additions & 3 deletions inttests/protectiondomain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"github.com/dell/goscaleio"
types "github.com/dell/goscaleio/types/v1"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -63,11 +62,9 @@ func getAllProtectionDomains(t *testing.T) []*goscaleio.ProtectionDomain {
system := getSystem()
assert.NotNil(t, system)

log.SetLevel(log.DebugLevel)
pd, err := system.GetProtectionDomain("")
assert.Nil(t, err)
assert.NotZero(t, len(pd))
log.SetLevel(log.InfoLevel)

var allDomains []*goscaleio.ProtectionDomain

Expand Down
2 changes: 0 additions & 2 deletions inttests/replication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"github.com/dell/goscaleio"
siotypes "github.com/dell/goscaleio/types/v1"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -349,7 +348,6 @@ func TestCreateReplicationConsistencyGroup(t *testing.T) {
rcgResp, err := C.CreateReplicationConsistencyGroup(rcgPayload)
assert.Nil(t, err)

log.Debugf("RCG ID: %s", rcgResp.ID)
rep.rcgID = rcgResp.ID

time.Sleep(5 * time.Second)
Expand Down
Loading

0 comments on commit a319320

Please sign in to comment.