mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-03-17 21:32:56 +01:00
a pluggable logging interface and more debug logging.
This commit is contained in:
parent
f918809e21
commit
e103c99bb8
14
lib.go
Normal file
14
lib.go
Normal 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)
|
||||
)
|
11
relay.go
11
relay.go
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
@ -120,7 +119,7 @@ func (r *Relay) Connect(ctx context.Context) error {
|
||||
case <-ticker.C:
|
||||
err := conn.socket.WriteMessage(websocket.PingMessage, 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
|
||||
}
|
||||
}
|
||||
@ -188,7 +187,7 @@ func (r *Relay) Connect(ctx context.Context) error {
|
||||
var subId string
|
||||
json.Unmarshal(jsonMessage[1], &subId)
|
||||
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
|
||||
} else {
|
||||
func() {
|
||||
@ -198,7 +197,7 @@ func (r *Relay) Connect(ctx context.Context) error {
|
||||
|
||||
// check if the event matches the desired filter, ignore otherwise
|
||||
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
|
||||
}
|
||||
|
||||
@ -215,7 +214,7 @@ func (r *Relay) Connect(ctx context.Context) error {
|
||||
if err != nil {
|
||||
errmsg = err.Error()
|
||||
}
|
||||
log.Printf("bad signature: %s\n", errmsg)
|
||||
InfoLogger.Printf("{%s} bad signature: %s\n", r.URL, errmsg)
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -250,6 +249,8 @@ func (r *Relay) Connect(ctx context.Context) error {
|
||||
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 {
|
||||
okCallback(ok, msg)
|
||||
}
|
||||
|
@ -68,6 +68,8 @@ func (sub *Subscription) Fire() error {
|
||||
message = append(message, filter)
|
||||
}
|
||||
|
||||
DebugLogger.Printf("{%s} REQ: %v", sub.Relay.URL, message)
|
||||
|
||||
err := sub.conn.WriteJSON(message)
|
||||
if err != nil {
|
||||
sub.cancel()
|
||||
|
Loading…
x
Reference in New Issue
Block a user