use log/slog
1 files changed,  +24, -23
M ysweet/proxy.go
+24, -23
  1@@ -4,7 +4,7 @@ import (
  2 	"context"
  3 	"fmt"
  4 	"io"
  5-	"log"
  6+	"log/slog"
  7 	"net/http"
  8 	"net/url"
  9 	"strings"
 10@@ -251,12 +251,15 @@ func ProxyHandler(targetURL string, opts ...ProxyOption) http.Handler {
 11 func handleWebsocketProxy(w http.ResponseWriter, r *http.Request, config *proxyConfig, docID string) {
 12 	ctx := r.Context()
 13 
 14-	// DEBUG: Log request details before attempting websocket upgrade
 15-	log.Printf("[DEBUG] handleWebsocketProxy: docID=%s method=%s url=%s", docID, r.Method, r.URL.String())
 16-	log.Printf("[DEBUG] handleWebsocketProxy: remote_addr=%s", r.RemoteAddr)
 17-	log.Printf("[DEBUG] handleWebsocketProxy: origin=%s", r.Header.Get("Origin"))
 18-	log.Printf("[DEBUG] handleWebsocketProxy: upgrade_header=%s", r.Header.Get("Upgrade"))
 19-	log.Printf("[DEBUG] handleWebsocketProxy: connection_header=%s", r.Header.Get("Connection"))
 20+	slog.Debug("handleWebsocketProxy",
 21+		"doc_id", docID,
 22+		"method", r.Method,
 23+		"url", r.URL.String(),
 24+		"remote_addr", r.RemoteAddr,
 25+		"origin", r.Header.Get("Origin"),
 26+		"upgrade", r.Header.Get("Upgrade"),
 27+		"connection", r.Header.Get("Connection"),
 28+	)
 29 
 30 	// Merge with master context if configured
 31 	if config.masterCtx != nil {
 32@@ -273,17 +276,16 @@ func handleWebsocketProxy(w http.ResponseWriter, r *http.Request, config *proxyC
 33 	}
 34 	clientConn, err := websocket.Accept(w, r, wsOpts)
 35 	if err != nil {
 36-		// DEBUG: Log why Accept failed
 37-		log.Printf("[DEBUG] websocket.Accept failed: err=%v", err)
 38+		slog.Debug("websocket accept failed", "doc_id", docID, "error", err)
 39 		// Connection already rejected, nothing more to do
 40 		return
 41 	}
 42-	log.Printf("[DEBUG] websocket.Accept succeeded for docID=%s", docID)
 43+	slog.Debug("websocket accept succeeded", "doc_id", docID)
 44 	defer clientConn.Close(websocket.StatusNormalClosure, "")
 45 
 46 	// Build upstream URL (preserves query parameters including token)
 47 	upstreamURL := buildUpstreamURL(config.targetURL, docID, r.URL.Query())
 48-	log.Printf("[DEBUG] connecting to upstream: url=%s", upstreamURL)
 49+	slog.Debug("connecting to upstream", "doc_id", docID, "url", upstreamURL)
 50 
 51 	// Connect to upstream y-sweet server
 52 	upstreamOpts := &websocket.DialOptions{}
 53@@ -296,22 +298,21 @@ func handleWebsocketProxy(w http.ResponseWriter, r *http.Request, config *proxyC
 54 
 55 	upstreamConn, _, err := websocket.Dial(ctx, upstreamURL, upstreamOpts)
 56 	if err != nil {
 57-		// DEBUG: Log why Dial failed
 58-		log.Printf("[DEBUG] websocket.Dial to upstream failed: url=%s err=%v", upstreamURL, err)
 59+		slog.Debug("upstream dial failed", "doc_id", docID, "url", upstreamURL, "error", err)
 60 		// Check if this was due to context cancellation
 61 		if ctx.Err() != nil {
 62-			log.Printf("[DEBUG] dial failed due to context cancellation")
 63+			slog.Debug("upstream dial failed due to context cancellation", "doc_id", docID)
 64 			clientConn.Close(websocket.StatusNormalClosure, "")
 65 			if config.onDisconnect != nil {
 66 				config.onDisconnect(context.Background(), docID, DisconnectReasonContextCancelled)
 67 			}
 68 			return
 69 		}
 70-		log.Printf("[DEBUG] closing client connection due to upstream dial failure")
 71+		slog.Debug("closing client connection due to upstream dial failure", "doc_id", docID)
 72 		clientConn.Close(websocket.StatusInternalError, "upstream connection failed")
 73 		return
 74 	}
 75-	log.Printf("[DEBUG] websocket.Dial to upstream succeeded for docID=%s", docID)
 76+	slog.Debug("upstream dial succeeded", "doc_id", docID)
 77 	defer upstreamConn.Close(websocket.StatusNormalClosure, "")
 78 
 79 	// Call OnWebsocketUpgrade hook if configured
 80@@ -381,7 +382,7 @@ func buildUpstreamURL(baseURL, docID string, query url.Values) string {
 81 // proxyConnections proxies messages bidirectionally between client and upstream.
 82 // It runs until either connection closes or the context is cancelled.
 83 func proxyConnections(ctx context.Context, clientConn, upstreamConn *websocket.Conn, config *proxyConfig, docID string) {
 84-	log.Printf("[DEBUG] proxyConnections started for docID=%s", docID)
 85+	slog.Debug("proxy connections started", "doc_id", docID)
 86 	
 87 	// Create a cancelable context for this proxy session
 88 	ctx, cancel := context.WithCancel(ctx)
 89@@ -398,7 +399,7 @@ func proxyConnections(ctx context.Context, clientConn, upstreamConn *websocket.C
 90 		defer close(done)
 91 		err := proxyMessages(ctx, clientConn, upstreamConn, "client->upstream")
 92 		if err != nil {
 93-			log.Printf("[DEBUG] client->upstream proxy error for docID=%s: %v", docID, err)
 94+			slog.Debug("client to upstream proxy error", "doc_id", docID, "error", err)
 95 		}
 96 		// Check if context was cancelled when we return
 97 		if ctx.Err() != nil {
 98@@ -410,7 +411,7 @@ func proxyConnections(ctx context.Context, clientConn, upstreamConn *websocket.C
 99 	go func() {
100 		err := proxyMessages(ctx, upstreamConn, clientConn, "upstream->client")
101 		if err != nil {
102-			log.Printf("[DEBUG] upstream->client proxy error for docID=%s: %v", docID, err)
103+			slog.Debug("upstream to client proxy error", "doc_id", docID, "error", err)
104 		}
105 		// Check if context was cancelled when we return
106 		if ctx.Err() != nil {
107@@ -420,7 +421,7 @@ func proxyConnections(ctx context.Context, clientConn, upstreamConn *websocket.C
108 
109 	// Wait for either direction to close
110 	<-done
111-	log.Printf("[DEBUG] proxyConnections: first direction closed for docID=%s", docID)
112+	slog.Debug("proxy connections first direction closed", "doc_id", docID)
113 
114 	// Check if context was cancelled (indicates shutdown)
115 	if atomic.LoadInt32(&contextWasCancelled) == 1 {
116@@ -432,10 +433,10 @@ func proxyConnections(ctx context.Context, clientConn, upstreamConn *websocket.C
117 		select {
118 		case <-done:
119 			// Other direction already closed
120-			log.Printf("[DEBUG] proxyConnections: both directions closed (context cancelled) for docID=%s", docID)
121+			slog.Debug("proxy connections both directions closed", "doc_id", docID)
122 		case <-time.After(5 * time.Second):
123 			// Drain timeout exceeded, force close
124-			log.Printf("[DEBUG] proxyConnections: drain timeout exceeded for docID=%s", docID)
125+			slog.Debug("proxy connections drain timeout exceeded", "doc_id", docID)
126 		}
127 	}
128 
129@@ -446,7 +447,7 @@ func proxyConnections(ctx context.Context, clientConn, upstreamConn *websocket.C
130 	clientConn.Close(websocket.StatusNormalClosure, "")
131 	upstreamConn.Close(websocket.StatusNormalClosure, "")
132 
133-	log.Printf("[DEBUG] proxyConnections ended for docID=%s, reason=%d", docID, disconnectReason)
134+	slog.Debug("proxy connections ended", "doc_id", docID, "reason", disconnectReason)
135 	
136 	// Call OnDisconnect hook if configured
137 	if config.onDisconnect != nil {