Skip to content

Commit

Permalink
feat: add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ocavue committed Mar 29, 2021
1 parent 10e25a0 commit 4453ae0
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ func (c *Workwx) WithApp(corpSecret string, agentID int64) *WorkwxApp {
CorpSecret: corpSecret,
AgentID: agentID,

accessToken: &token{mutex: &sync.RWMutex{}},
jsapiTicket: &token{mutex: &sync.RWMutex{}},
jsapiTicketAgentConfig: &token{mutex: &sync.RWMutex{}},
accessToken: &token{mutex: &sync.RWMutex{}, logger: c.opts.Logger},
jsapiTicket: &token{mutex: &sync.RWMutex{}, logger: c.opts.Logger},
jsapiTicketAgentConfig: &token{mutex: &sync.RWMutex{}, logger: c.opts.Logger},
}
app.accessToken.setGetTokenFunc(app.getAccessToken)
app.jsapiTicket.setGetTokenFunc(app.getJSAPITicket)
Expand Down
22 changes: 22 additions & 0 deletions client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const DefaultQYAPIHost = "https://qyapi.weixin.qq.com"
type options struct {
QYAPIHost string
HTTP *http.Client
Logger Logger
}

// CtorOption 客户端对象构造参数
Expand All @@ -22,6 +23,7 @@ func defaultOptions() options {
return options{
QYAPIHost: DefaultQYAPIHost,
HTTP: &http.Client{},
Logger: newDefaultLogger(),
}
}

Expand Down Expand Up @@ -62,3 +64,23 @@ var _ CtorOption = (*withHTTPClient)(nil)
func (x *withHTTPClient) applyTo(y *options) {
y.HTTP = x.x
}

//
//
//

type withLogger struct {
x Logger
}

// WithLogger 使用给定的 logger 作为日志打印的方式。如果不使用 WithLogger 自定义 logger,
// 则默认 logger 会将 Info 级别的日志打印在 stdout 中,将 Error 级别的日志打印在 stderr 中。
func WithLogger(logger Logger) CtorOption {
return &withLogger{x: logger}
}

var _ CtorOption = (*withLogger)(nil)

func (x *withLogger) applyTo(y *options) {
y.Logger = x.x
}
27 changes: 27 additions & 0 deletions client_options_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package workwx

import (
"fmt"
"net/http"
"testing"

Expand Down Expand Up @@ -52,3 +53,29 @@ func TestWithQYAPIHost(t *testing.T) {
})
})
}

func TestWithLogger(t *testing.T) {
c.Convey("给定一个 options", t, func() {
opts := options{}

c.Convey("用 WithLogger 修饰它", func() {

l := &myLogger{}
o := WithLogger(l)
o.applyTo(&opts)

c.Convey("options.Logger 应该变了", func() {
c.So(opts.Logger, c.ShouldEqual, l)
})
})
})
}

type myLogger struct{}

func (*myLogger) Info(msg string) {
fmt.Println("INFO: " + msg)
}
func (*myLogger) Error(msg string) {
fmt.Println("ERROR: " + msg)
}
30 changes: 30 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package workwx

import (
"log"
"os"
)

type Logger interface {
Info(msg string)
Error(msg string)
}

type defaultLogger struct {
stderr *log.Logger
stdout *log.Logger
}

func (l *defaultLogger) Info(msg string) {
l.stdout.Println(msg)
}

func (l *defaultLogger) Error(msg string) {
l.stderr.Println(msg)
}

func newDefaultLogger() *defaultLogger {
stderr := log.New(os.Stderr, "[workwx INFO]", 0)
stdout := log.New(os.Stdout, "[workwx ERR]", 0)
return &defaultLogger{stderr: stderr, stdout: stdout}
}
6 changes: 4 additions & 2 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package workwx

import (
"context"
"fmt"
"sync"
"time"

Expand All @@ -18,6 +19,7 @@ type token struct {
tokenInfo
lastRefresh time.Time
getTokenFunc func() (tokenInfo, error)
logger Logger
}

// getAccessToken 获取 access token
Expand Down Expand Up @@ -150,8 +152,7 @@ func (t *token) tokenRefresher(ctx context.Context) {
case <-time.After(waitDuration):
retryer := backoff.WithContext(backoff.NewExponentialBackOff(), ctx)
if err := backoff.Retry(t.syncToken, retryer); err != nil {
// TODO: logging
_ = err
t.logger.Error(fmt.Sprintf("failed to refresh token: %v. will retry.", err))
}

waitUntilTime := t.lastRefresh.Add(t.expiresIn).Add(-refreshTimeWindow)
Expand All @@ -160,6 +161,7 @@ func (t *token) tokenRefresher(ctx context.Context) {
waitDuration = minRefreshDuration
}
case <-ctx.Done():
t.logger.Info("tokenRefresher terminated")
return
}
}
Expand Down

0 comments on commit 4453ae0

Please sign in to comment.