Add logger to client (#75)
This commit is contained in:
parent
1306d648f6
commit
97bec2e80f
17
v1/client.go
17
v1/client.go
@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
@ -28,6 +29,22 @@ func NewWithClient(url string, token string, client *http.Client) *MgClient {
|
||||
}
|
||||
}
|
||||
|
||||
// WithLogger sets the provided logger instance into the Client
|
||||
func (c *MgClient) WithLogger(logger BasicLogger) *MgClient {
|
||||
c.logger = logger
|
||||
return c
|
||||
}
|
||||
|
||||
// writeLog writes to the log.
|
||||
func (c *MgClient) writeLog(format string, v ...interface{}) {
|
||||
if c.logger != nil {
|
||||
c.logger.Printf(format, v...)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf(format, v...)
|
||||
}
|
||||
|
||||
// TransportTemplates returns templates list
|
||||
//
|
||||
// Example:
|
||||
|
25
v1/log.go
Normal file
25
v1/log.go
Normal file
@ -0,0 +1,25 @@
|
||||
package v1
|
||||
|
||||
// BasicLogger provides basic functionality for logging.
|
||||
type BasicLogger interface {
|
||||
Printf(string, ...interface{})
|
||||
}
|
||||
|
||||
// DebugLogger can be used to easily wrap any logger with Debugf method into the BasicLogger instance.
|
||||
type DebugLogger interface {
|
||||
Debugf(string, ...interface{})
|
||||
}
|
||||
|
||||
type debugLoggerAdapter struct {
|
||||
logger DebugLogger
|
||||
}
|
||||
|
||||
// DebugLoggerAdapter returns BasicLogger that calls underlying DebugLogger.Debugf.
|
||||
func DebugLoggerAdapter(logger DebugLogger) BasicLogger {
|
||||
return &debugLoggerAdapter{logger: logger}
|
||||
}
|
||||
|
||||
// Printf data in the log using DebugLogger.Debugf.
|
||||
func (l *debugLoggerAdapter) Printf(format string, v ...interface{}) {
|
||||
l.logger.Debugf(format, v...)
|
||||
}
|
24
v1/log_test.go
Normal file
24
v1/log_test.go
Normal file
@ -0,0 +1,24 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type wrappedLogger struct {
|
||||
lastMessage string
|
||||
}
|
||||
|
||||
func (w *wrappedLogger) Debugf(msg string, v ...interface{}) {
|
||||
w.lastMessage = fmt.Sprintf(msg, v...)
|
||||
}
|
||||
|
||||
func TestDebugLoggerAdapter_Printf(t *testing.T) {
|
||||
wrapped := &wrappedLogger{}
|
||||
logger := DebugLoggerAdapter(wrapped)
|
||||
logger.Printf("Test message #%d", 1)
|
||||
|
||||
assert.Equal(t, "Test message #1", wrapped.lastMessage)
|
||||
}
|
@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
@ -64,9 +63,9 @@ func makeRequest(reqType, url string, buf io.Reader, c *MgClient) ([]byte, int,
|
||||
|
||||
if c.Debug {
|
||||
if strings.Contains(url, "/files/upload") {
|
||||
log.Printf("MG TRANSPORT API Request: %s %s %s [file data]", reqType, url, c.Token)
|
||||
c.writeLog("MG TRANSPORT API Request: %s %s %s [file data]", reqType, url, c.Token)
|
||||
} else {
|
||||
log.Printf("MG TRANSPORT API Request: %s %s %s %v", reqType, url, c.Token, buf)
|
||||
c.writeLog("MG TRANSPORT API Request: %s %s %s %v", reqType, url, c.Token, buf)
|
||||
}
|
||||
}
|
||||
|
||||
@ -86,7 +85,7 @@ func makeRequest(reqType, url string, buf io.Reader, c *MgClient) ([]byte, int,
|
||||
}
|
||||
|
||||
if c.Debug {
|
||||
log.Printf("MG TRANSPORT API Response: %s", res)
|
||||
c.writeLog("MG TRANSPORT API Response: %s", res)
|
||||
}
|
||||
|
||||
return res, resp.StatusCode, err
|
||||
|
@ -82,6 +82,7 @@ type MgClient struct {
|
||||
Token string `json:"token"`
|
||||
Debug bool `json:"debug"`
|
||||
httpClient *http.Client `json:"-"`
|
||||
logger BasicLogger `json:"-"`
|
||||
}
|
||||
|
||||
// Channel type.
|
||||
|
Loading…
Reference in New Issue
Block a user