Add logger to client

This commit is contained in:
tishmaria90 2021-11-25 12:51:29 +03:00 committed by GitHub
parent b625439a0c
commit 864572b667
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 3 deletions

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"strings" "strings"
"time" "time"
@ -22,6 +23,22 @@ func New(url string, token string) *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...)
}
// Bots get all available bots // Bots get all available bots
// //
// Example: // Example:

View File

@ -1,6 +1,7 @@
package v1 package v1
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
@ -830,3 +831,31 @@ func RandStringBytesMaskImprSrc(n int) string {
return string(b) return string(b)
} }
func TestMgClient_DebugNoLogger(t *testing.T) {
c := client()
c.Debug = true
var buf bytes.Buffer
log.SetOutput(&buf)
defer func() {
log.SetOutput(os.Stderr)
}()
c.writeLog("Test log string")
assert.Contains(t, buf.String(), "Test log string")
}
func TestMgClient_DebugWithLogger(t *testing.T) {
var buf bytes.Buffer
logger := log.New(&buf, "Custom log prefix ", 0)
c := client()
c.Debug = true
c.WithLogger(logger)
c.writeLog("Test log string")
assert.Contains(t, buf.String(), "Custom log prefix Test log string")
}

25
v1/log.go Normal file
View 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
View 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)
}

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
) )
@ -72,7 +71,7 @@ func makeRequest(reqType, url string, buf io.Reader, c *MgClient) ([]byte, int,
req.Header.Set("X-Bot-Token", c.Token) req.Header.Set("X-Bot-Token", c.Token)
if c.Debug { if c.Debug {
log.Printf("MG BOT API Request: %s %s %s %+v", reqType, url, c.Token, buf) c.writeLog("MG BOT API Request: %s %s %s %+v", reqType, url, c.Token, buf)
} }
resp, err := c.httpClient.Do(req) resp, err := c.httpClient.Do(req)
@ -91,7 +90,7 @@ func makeRequest(reqType, url string, buf io.Reader, c *MgClient) ([]byte, int,
} }
if c.Debug { if c.Debug {
log.Printf("MG BOT API Response: %s", res) c.writeLog("MG BOT API Response: %s", res)
} }
return res, resp.StatusCode, err return res, resp.StatusCode, err

View File

@ -83,6 +83,7 @@ type MgClient struct {
Token string `json:"token"` Token string `json:"token"`
Debug bool `json:"debug"` Debug bool `json:"debug"`
httpClient *http.Client httpClient *http.Client
logger BasicLogger `json:"-"`
} }
// Request types // Request types