Skip to content

Commit

Permalink
Add default no-op logger to Client. Fixes #41
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Sep 21, 2021
1 parent 01289e5 commit 5dd96e0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ type Logger interface {
Debugfln(message string, args ...interface{})
}

// StubLogger is an implementation of Logger that does nothing
type StubLogger struct{}

func (sl *StubLogger) Debugfln(message string, args ...interface{}) {}
func (sl *StubLogger) Warnfln(message string, args ...interface{}) {}

var stubLogger = &StubLogger{}

type WarnLogger interface {
Logger
Warnfln(message string, args ...interface{})
Expand Down Expand Up @@ -228,7 +236,7 @@ func (cli *Client) SyncWithContext(ctx context.Context) error {
filterID = resFilter.FilterID
cli.Store.SaveFilterID(cli.UserID, filterID)
}
lastSuccessfulSync := time.Now().Add(-cli.StreamSyncMinAge - 1 * time.Hour)
lastSuccessfulSync := time.Now().Add(-cli.StreamSyncMinAge - 1*time.Hour)
for {
streamResp := false
if cli.StreamSyncMinAge > 0 && time.Since(lastSuccessfulSync) > cli.StreamSyncMinAge {
Expand Down Expand Up @@ -294,7 +302,7 @@ const logBodyContextKey = "fi.mau.mautrix.log_body"
const logRequestIDContextKey = "fi.mau.mautrix.request_id"

func (cli *Client) LogRequest(req *http.Request) {
if cli.Logger == nil {
if cli.Logger == stubLogger {
return
}
body, ok := req.Context().Value(logBodyContextKey).(string)
Expand Down Expand Up @@ -1465,6 +1473,7 @@ func NewClient(homeserverURL string, userID id.UserID, accessToken string) (*Cli
Client: &http.Client{Timeout: 180 * time.Second},
Prefix: URLPath{"_matrix", "client", "r0"},
Syncer: NewDefaultSyncer(),
Logger: stubLogger,
// By default, use an in-memory store which will never save filter ids / next batch tokens to disk.
// The client will work with this storer: it just won't remember across restarts.
// In practice, a database backend should be used.
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package mautrix

const Version = "v0.9.25"
const Version = "v0.9.26"

var DefaultUserAgent = "mautrix-go/" + Version

0 comments on commit 5dd96e0

Please sign in to comment.