improved logging thing with the "debug" build tag.

This commit is contained in:
fiatjaf 2023-04-11 09:07:37 -03:00
parent 7b0af23f1a
commit 6f74d284c4
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
6 changed files with 51 additions and 20 deletions

14
lib.go
View File

@ -1,14 +0,0 @@
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)
)

14
log.go Normal file
View File

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

24
log_debug.go Normal file
View File

@ -0,0 +1,24 @@
//go:build debug
package nostr
import (
"encoding/json"
"fmt"
)
func debugLog(str string, args ...any) {
for i, v := range args {
switch v.(type) {
case []json.RawMessage:
j, _ := json.Marshal(v)
args[i] = string(j)
case []byte:
args[i] = string(v.([]byte))
case fmt.Stringer:
args[i] = v.(fmt.Stringer).String()
}
}
DebugLogger.Printf(str, args...)
}

7
log_normal.go Normal file
View File

@ -0,0 +1,7 @@
//go:build !debug
package nostr
func debugLog(str string, args ...any) {
return
}

View File

@ -160,7 +160,7 @@ func (r *Relay) Connect(ctx context.Context) error {
switch command {
case "NOTICE":
DebugLogger.Printf("{%s} %v\n", r.URL, jsonMessage)
debugLog("{%s} %v\n", r.URL, jsonMessage)
var content string
json.Unmarshal(jsonMessage[1], &content)
go func() {
@ -171,7 +171,7 @@ func (r *Relay) Connect(ctx context.Context) error {
r.mutex.RUnlock()
}()
case "AUTH":
DebugLogger.Printf("{%s} %v\n", r.URL, jsonMessage)
debugLog("{%s} %v\n", r.URL, jsonMessage)
var challenge string
json.Unmarshal(jsonMessage[1], &challenge)
go func() {
@ -228,7 +228,7 @@ func (r *Relay) Connect(ctx context.Context) error {
if len(jsonMessage) < 2 {
continue
}
DebugLogger.Printf("{%s} %v\n", r.URL, jsonMessage)
debugLog("{%s} %v\n", r.URL, jsonMessage)
var subId string
json.Unmarshal(jsonMessage[1], &subId)
if subscription, ok := r.subscriptions.Load(subId); ok {
@ -240,7 +240,7 @@ func (r *Relay) Connect(ctx context.Context) error {
if len(jsonMessage) < 3 {
continue
}
DebugLogger.Printf("{%s} %v\n", r.URL, jsonMessage)
debugLog("{%s} %v\n", r.URL, jsonMessage)
var (
eventId string
ok bool
@ -376,7 +376,7 @@ func (r *Relay) Auth(ctx context.Context, event Event) (Status, error) {
// send AUTH
authResponse := []interface{}{"AUTH", event}
DebugLogger.Printf("{%s} sending %v\n", r.URL, authResponse)
debugLog("{%s} sending %v\n", r.URL, authResponse)
if err := r.Connection.WriteJSON(authResponse); err != nil {
// status will be "failed"
return status, err

View File

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