Skip to content

Commit c6abccc

Browse files
authored
Add and use configurable logger (#16)
1 parent 0f55057 commit c6abccc

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed

client/build.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"encoding/json"
1212
"net/http"
1313

14-
"github.com/golang/glog"
1514
jsonresp "github.com/sylabs/json-resp"
1615
)
1716

@@ -34,7 +33,7 @@ func (c *Client) Submit(ctx context.Context, d Definition, libraryRef string, li
3433
}
3534
req = req.WithContext(ctx)
3635
req.Header.Set("Content-Type", "application/json")
37-
glog.V(2).Infof("Sending build request to %s", req.URL.String())
36+
c.Logger.Logf("Sending build request to %s", req.URL.String())
3837

3938
res, err := c.HTTPClient.Do(req)
4039
if err != nil {
@@ -44,7 +43,7 @@ func (c *Client) Submit(ctx context.Context, d Definition, libraryRef string, li
4443

4544
err = jsonresp.ReadResponse(res.Body, &bi)
4645
if err == nil {
47-
glog.V(2).Infof("Build response - id: %s, libref: %s", bi.ID, bi.LibraryRef)
46+
c.Logger.Logf("Build response - id: %s, libref: %s", bi.ID, bi.LibraryRef)
4847
}
4948
return
5049
}

client/client.go

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"io"
1111
"net/http"
1212
"net/url"
13+
14+
"github.com/go-log/log"
1315
)
1416

1517
// Config contains the client configuration.
@@ -22,6 +24,8 @@ type Config struct {
2224
UserAgent string
2325
// HTTPClient to use to make HTTP requests (if supplied).
2426
HTTPClient *http.Client
27+
// Logger to be used when output is generated
28+
Logger log.Logger
2529
}
2630

2731
// DefaultConfig is a configuration that uses default values.
@@ -37,6 +41,8 @@ type Client struct {
3741
UserAgent string
3842
// HTTPClient to use to make HTTP requests.
3943
HTTPClient *http.Client
44+
// Logger to be used when output is generated
45+
Logger log.Logger
4046
}
4147

4248
const defaultBaseURL = "https://build.sylabs.io"
@@ -73,6 +79,11 @@ func New(cfg *Config) (c *Client, err error) {
7379
c.HTTPClient = http.DefaultClient
7480
}
7581

82+
if cfg.Logger != nil {
83+
c.Logger = cfg.Logger
84+
} else {
85+
c.Logger = log.DefaultLogger
86+
}
7687
return c, nil
7788
}
7889

client/output.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"net/http"
1212
"net/url"
1313

14-
"github.com/golang/glog"
1514
"github.com/gorilla/websocket"
1615
)
1716

@@ -36,7 +35,7 @@ func (c *Client) GetOutput(ctx context.Context, buildID string, or OutputReader)
3635

3736
ws, resp, err := websocket.DefaultDialer.Dial(u.String(), h)
3837
if err != nil {
39-
glog.V(2).Infof("websocket dial err - %s, partial response: %+v", err, resp)
38+
c.Logger.Logf("websocket dial err - %s, partial response: %+v", err, resp)
4039
return err
4140
}
4241
defer ws.Close()
@@ -55,7 +54,7 @@ func (c *Client) GetOutput(ctx context.Context, buildID string, or OutputReader)
5554
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
5655
return nil
5756
}
58-
glog.Infof("websocket read message err - %s", err)
57+
c.Logger.Logf("websocket read message err - %s", err)
5958
return err
6059
}
6160

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/sylabs/scs-build-client
33
go 1.12
44

55
require (
6-
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
6+
github.com/go-log/log v0.1.0
77
github.com/gorilla/websocket v1.4.0
88
github.com/sylabs/json-resp v0.5.0
99
)

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
2-
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
1+
github.com/go-log/log v0.1.0 h1:wudGTNsiGzrD5ZjgIkVZ517ugi2XRe9Q/xRCzwEO4/U=
2+
github.com/go-log/log v0.1.0/go.mod h1:4mBwpdRMFLiuXZDCwU2lKQFsoSCo72j3HqBK9d81N2M=
33
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
44
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
55
github.com/sylabs/json-resp v0.5.0 h1:AWdKu6aS0WrkkltX1M0ex0lENrIcx5TISox902s2L2M=

0 commit comments

Comments
 (0)