Add logging service
New Go program (logger/) that: - Accepts POSTed JSON log messages via POST /log - Stores last N messages in a ring buffer (default 1M) - Retrieves logs via GET /logs with limit/before/after filters - Shows status via GET /status Also updates express/logging.ts to POST messages to the logger service. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
70
logger/main.go
Normal file
70
logger/main.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
port := flag.Int("port", 8085, "port to listen on")
|
||||
capacity := flag.Int("capacity", 1000000, "max messages to store")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
store := NewLogStore(*capacity)
|
||||
|
||||
http.HandleFunc("POST /log", func(w http.ResponseWriter, r *http.Request) {
|
||||
var msg Message
|
||||
if err := json.NewDecoder(r.Body).Decode(&msg); err != nil {
|
||||
http.Error(w, "invalid JSON", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
store.Add(msg)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
})
|
||||
|
||||
http.HandleFunc("GET /logs", func(w http.ResponseWriter, r *http.Request) {
|
||||
params := FilterParams{}
|
||||
|
||||
if limit := r.URL.Query().Get("limit"); limit != "" {
|
||||
if n, err := strconv.Atoi(limit); err == nil {
|
||||
params.Limit = n
|
||||
}
|
||||
}
|
||||
if before := r.URL.Query().Get("before"); before != "" {
|
||||
if ts, err := strconv.ParseInt(before, 10, 64); err == nil {
|
||||
params.Before = ts
|
||||
}
|
||||
}
|
||||
if after := r.URL.Query().Get("after"); after != "" {
|
||||
if ts, err := strconv.ParseInt(after, 10, 64); err == nil {
|
||||
params.After = ts
|
||||
}
|
||||
}
|
||||
|
||||
messages := store.GetFiltered(params)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(messages)
|
||||
})
|
||||
|
||||
http.HandleFunc("GET /status", func(w http.ResponseWriter, r *http.Request) {
|
||||
status := map[string]any{
|
||||
"count": store.Count(),
|
||||
"capacity": *capacity,
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(status)
|
||||
})
|
||||
|
||||
listenAddr := fmt.Sprintf(":%d", *port)
|
||||
log.Printf("[logger] Listening on %s (capacity: %d)", listenAddr, *capacity)
|
||||
if err := http.ListenAndServe(listenAddr, nil); err != nil {
|
||||
log.Fatalf("[logger] Failed to start: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user