feat(logging): integrate slog logging with env control and request/sync tracing
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 39s

- Configure structured slog logger in cmd/server/main.go via LOG_LEVEL and DEBUG env vars
- Add HTTP API request logging middleware in internal/api/ with status and duration_ms
- Add WebSocket join/leave, event reception, and debounced SQLite flush tracing in docroom
- Document LOG_LEVEL and DEBUG settings in .env.example, compose.yaml, and docker-compose.yml
This commit is contained in:
sandrodach committed 2026-10-26 22:12:51 +01:00
1 parent 177e24fdc8
commit 48fe61fbce
9 files changed
+208 -16

No files matched your search

+47 -3
View File
@@ -2,7 +2,7 @@ package ws
import (
"encoding/json"
"log"
"log/slog"
"net/http"
"sync"
"time"
@@ -477,13 +477,30 @@ func (r *DocRoom) Flush() {
r.dirty = false
r.mu.Unlock()
tagsCount := 0
for _, b := range blocksCopy {
tagsCount += len(domain.ExtractTags(b.Content))
}
start := time.Now()
r.hub.mu.RLock()
store := r.hub.docStore
r.hub.mu.RUnlock()
if store != nil {
if err := store.FlushDocumentState(docID, title, blocksCopy); err != nil {
log.Printf("[doc-room] flush error for doc %s: %v", docID, err)
slog.Error("SQLite document flush failed",
"doc_id", docID,
"error", err,
)
} else {
durationMs := float64(time.Since(start).Microseconds()) / 1000.0
slog.Debug("SQLite document flushed",
"doc_id", docID,
"blocks_count", len(blocksCopy),
"tags_count", tagsCount,
"duration_ms", durationMs,
)
}
}
}
@@ -492,7 +509,7 @@ func (r *DocRoom) Flush() {
func (r *DocRoom) ServeDocWS(w http.ResponseWriter, req *http.Request, userID string, clientID string, canWrite bool) {
conn, err := upgrader.Upgrade(w, req, nil)
if err != nil {
log.Printf("[doc-ws] upgrade error for doc %s: %v", r.docID, err)
slog.Error("WebSocket upgrade error", "doc_id", r.docID, "error", err)
return
}
@@ -518,6 +535,7 @@ func (r *DocRoom) ServeDocWS(w http.ResponseWriter, req *http.Request, userID st
r.mu.Lock()
r.wsClients[client] = true
activeClients := len(r.wsClients)
if r.activeCursors == nil {
r.activeCursors = make(map[string]*CursorPresence)
}
@@ -542,6 +560,13 @@ func (r *DocRoom) ServeDocWS(w http.ResponseWriter, req *http.Request, userID st
}
r.mu.Unlock()
slog.Debug("WS client joined doc room",
"doc_id", r.docID,
"client_id", clientID,
"user_id", userID,
"active_clients", activeClients,
)
// Immediately send live in-memory state and current cursors to the newly connected client
initEvt := DocSyncEvent{
Type: "doc_state",
@@ -609,6 +634,13 @@ func (c *DocWSClient) readPump() {
}
c.room.mu.Unlock()
slog.Debug("WS client left doc room",
"doc_id", c.room.docID,
"client_id", c.clientID,
"user_id", c.userID,
"active_clients", remaining,
)
_ = c.conn.Close()
// Flush immediately when the last client leaves the document room
@@ -632,6 +664,18 @@ func (c *DocWSClient) readPump() {
var evt DocSyncEvent
if err := json.Unmarshal(message, &evt); err == nil && evt.Type != "" {
targetBlockID := evt.BlockID
if targetBlockID == "" && evt.Block != nil {
targetBlockID = evt.Block.ID
}
slog.Debug("WS doc event received",
"type", evt.Type,
"doc_id", c.room.docID,
"block_id", targetBlockID,
"sender_client_id", c.clientID,
)
// Read-only clients can still move their cursor and send presence updates
if !c.canWrite && evt.Type != "cursor_moved" {
continue
+2 -2
View File
@@ -3,7 +3,7 @@ package ws
import (
"encoding/json"
"fmt"
"log"
"log/slog"
"net/http"
"strings"
"sync"
@@ -627,7 +627,7 @@ func (c *Client) SendEvent(event string, payload interface{}) {
func (h *Hub) ServeWS(w http.ResponseWriter, r *http.Request, roomID, userID string) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("[ws] upgrade error: %v", err)
slog.Error("WebSocket upgrade error", "room_id", roomID, "user_id", userID, "error", err)
return
}