a pluggable logging interface and more debug logging.

This commit is contained in:
fiatjaf 2023-04-11 00:28:52 -03:00
parent f918809e21
commit e103c99bb8
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
3 changed files with 22 additions and 5 deletions

14
lib.go Normal file
View File

@ -0,0 +1,14 @@
package nostr
import (
"io/ioutil"
"log"
)
var (
// call SetOutput on InfoLogger to enable info logging
InfoLogger = log.New(ioutil.Discard, "[go-nostr][info] ", log.LstdFlags)
// call SetOutput on DebugLogger to enable debug logging
DebugLogger = log.New(ioutil.Discard, "[go-nostr][debug] ", log.LstdFlags)
)

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"net/http" "net/http"
"sync" "sync"
"time" "time"
@ -120,7 +119,7 @@ func (r *Relay) Connect(ctx context.Context) error {
case <-ticker.C: case <-ticker.C:
err := conn.socket.WriteMessage(websocket.PingMessage, nil) err := conn.socket.WriteMessage(websocket.PingMessage, nil)
if err != nil { if err != nil {
log.Printf("error writing ping: %v; closing websocket", err) InfoLogger.Printf("{%s} error writing ping: %v; closing websocket", r.URL, err)
return return
} }
} }
@ -188,7 +187,7 @@ func (r *Relay) Connect(ctx context.Context) error {
var subId string var subId string
json.Unmarshal(jsonMessage[1], &subId) json.Unmarshal(jsonMessage[1], &subId)
if subscription, ok := r.subscriptions.Load(subId); !ok { if subscription, ok := r.subscriptions.Load(subId); !ok {
log.Printf("no subscription with id '%s'\n", subId) InfoLogger.Printf("{%s} no subscription with id '%s'\n", r.URL, subId)
continue continue
} else { } else {
func() { func() {
@ -198,7 +197,7 @@ func (r *Relay) Connect(ctx context.Context) error {
// check if the event matches the desired filter, ignore otherwise // check if the event matches the desired filter, ignore otherwise
if !subscription.Filters.Match(&event) { if !subscription.Filters.Match(&event) {
log.Printf("filter does not match (%s): %v ~ %v\n", r.URL, subscription.Filters[0], event) InfoLogger.Printf("{%s} filter does not match: %v ~ %v\n", r.URL, subscription.Filters[0], event)
return return
} }
@ -215,7 +214,7 @@ func (r *Relay) Connect(ctx context.Context) error {
if err != nil { if err != nil {
errmsg = err.Error() errmsg = err.Error()
} }
log.Printf("bad signature: %s\n", errmsg) InfoLogger.Printf("{%s} bad signature: %s\n", r.URL, errmsg)
return return
} }
} }
@ -250,6 +249,8 @@ func (r *Relay) Connect(ctx context.Context) error {
json.Unmarshal(jsonMessage[3], &msg) json.Unmarshal(jsonMessage[3], &msg)
} }
DebugLogger.Printf("{%s} OK: %s %v %s\n", r.URL, eventId, ok, msg)
if okCallback, exist := r.okCallbacks.Load(eventId); exist { if okCallback, exist := r.okCallbacks.Load(eventId); exist {
okCallback(ok, msg) okCallback(ok, msg)
} }

View File

@ -68,6 +68,8 @@ func (sub *Subscription) Fire() error {
message = append(message, filter) message = append(message, filter)
} }
DebugLogger.Printf("{%s} REQ: %v", sub.Relay.URL, message)
err := sub.conn.WriteJSON(message) err := sub.conn.WriteJSON(message)
if err != nil { if err != nil {
sub.cancel() sub.cancel()