diff --git a/lib.go b/lib.go deleted file mode 100644 index f4e1d58..0000000 --- a/lib.go +++ /dev/null @@ -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) -) diff --git a/log.go b/log.go new file mode 100644 index 0000000..52e2a44 --- /dev/null +++ b/log.go @@ -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) +) diff --git a/log_debug.go b/log_debug.go new file mode 100644 index 0000000..175aecd --- /dev/null +++ b/log_debug.go @@ -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...) +} diff --git a/log_normal.go b/log_normal.go new file mode 100644 index 0000000..cd6a746 --- /dev/null +++ b/log_normal.go @@ -0,0 +1,7 @@ +//go:build !debug + +package nostr + +func debugLog(str string, args ...any) { + return +} diff --git a/relay.go b/relay.go index 75902ee..ab6be94 100644 --- a/relay.go +++ b/relay.go @@ -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 diff --git a/subscription.go b/subscription.go index c9f4c79..5e0c44a 100644 --- a/subscription.go +++ b/subscription.go @@ -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 {