mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-20 13:50:55 +02: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"
|
"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)
|
||||||
}
|
}
|
||||||
|
@ -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()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user