mirror of
https://github.com/taigrr/log-socket
synced 2026-04-14 09:31:16 -07:00
Compare commits
1 Commits
cd/websock
...
cd/logger-
| Author | SHA1 | Date | |
|---|---|---|---|
| bd18d338e5 |
@@ -1,53 +0,0 @@
|
|||||||
package browser
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestLogSocketViewHandler_HTTP(t *testing.T) {
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "http://localhost:8080/", nil)
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
LogSocketViewHandler(w, req)
|
|
||||||
|
|
||||||
if w.Code != http.StatusOK {
|
|
||||||
t.Fatalf("status = %d, want 200", w.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
body := w.Body.String()
|
|
||||||
// html/template escapes forward slashes in JS context
|
|
||||||
if !strings.Contains(body, `ws:\/\/localhost:8080\/ws`) {
|
|
||||||
t.Error("response should contain escaped ws://localhost:8080/ws URL")
|
|
||||||
}
|
|
||||||
if !strings.Contains(body, "<!DOCTYPE html>") {
|
|
||||||
t.Error("response should contain HTML doctype")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLogSocketViewHandler_CustomPath(t *testing.T) {
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "http://myhost:9090/dashboard/", nil)
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
LogSocketViewHandler(w, req)
|
|
||||||
|
|
||||||
body := w.Body.String()
|
|
||||||
if !strings.Contains(body, `ws:\/\/myhost:9090\/dashboard\/ws`) {
|
|
||||||
t.Error("expected escaped ws://myhost:9090/dashboard/ws in body")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLogSocketViewHandler_TrailingSlashTrimmed(t *testing.T) {
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "http://example.com/", nil)
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
LogSocketViewHandler(w, req)
|
|
||||||
|
|
||||||
body := w.Body.String()
|
|
||||||
// Should NOT have double slash before ws
|
|
||||||
if strings.Contains(body, `\/\/ws`) {
|
|
||||||
t.Error("should not have double slash before /ws")
|
|
||||||
}
|
|
||||||
if !strings.Contains(body, `ws:\/\/example.com\/ws`) {
|
|
||||||
t.Error("expected escaped ws://example.com/ws in body")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
17
log/log.go
17
log/log.go
@@ -1,7 +1,6 @@
|
|||||||
package log
|
package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@@ -154,7 +153,6 @@ func (c *Client) SetLogLevel(level Level) {
|
|||||||
c.LogLevel = level
|
c.LogLevel = level
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get blocks until a log entry is available and returns it.
|
|
||||||
func (c *Client) Get() Entry {
|
func (c *Client) Get() Entry {
|
||||||
if !c.initialized {
|
if !c.initialized {
|
||||||
panic(errors.New("cannot get logs for uninitialized client, did you use CreateClient?"))
|
panic(errors.New("cannot get logs for uninitialized client, did you use CreateClient?"))
|
||||||
@@ -162,21 +160,6 @@ func (c *Client) Get() Entry {
|
|||||||
return <-c.writer
|
return <-c.writer
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetContext blocks until a log entry is available or ctx is cancelled.
|
|
||||||
// The second return value is false when the context was cancelled before
|
|
||||||
// an entry arrived.
|
|
||||||
func (c *Client) GetContext(ctx context.Context) (Entry, bool) {
|
|
||||||
if !c.initialized {
|
|
||||||
panic(errors.New("cannot get logs for uninitialized client, did you use CreateClient?"))
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case e := <-c.writer:
|
|
||||||
return e, true
|
|
||||||
case <-ctx.Done():
|
|
||||||
return Entry{}, false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Trace prints out logs on trace level
|
// Trace prints out logs on trace level
|
||||||
func Trace(args ...any) {
|
func Trace(args ...any) {
|
||||||
output := fmt.Sprint(args...)
|
output := fmt.Sprint(args...)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package log
|
package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -551,68 +550,6 @@ func TestMatchesNamespace(t *testing.T) {
|
|||||||
c2.Destroy()
|
c2.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestGetContext verifies context cancellation stops blocking Get.
|
|
||||||
func TestGetContext(t *testing.T) {
|
|
||||||
c := CreateClient(DefaultNamespace)
|
|
||||||
c.SetLogLevel(LTrace)
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
cancel() // cancel immediately
|
|
||||||
|
|
||||||
_, ok := c.GetContext(ctx)
|
|
||||||
if ok {
|
|
||||||
t.Error("expected GetContext to return false on cancelled context")
|
|
||||||
}
|
|
||||||
c.Destroy()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestGetContextReceivesEntry verifies GetContext delivers entries normally.
|
|
||||||
func TestGetContextReceivesEntry(t *testing.T) {
|
|
||||||
c := CreateClient(DefaultNamespace)
|
|
||||||
c.SetLogLevel(LTrace)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
time.Sleep(10 * time.Millisecond)
|
|
||||||
Info("context entry")
|
|
||||||
}()
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
e, ok := c.GetContext(ctx)
|
|
||||||
if !ok {
|
|
||||||
t.Fatal("expected GetContext to return entry")
|
|
||||||
}
|
|
||||||
if e.Output != "context entry" {
|
|
||||||
t.Errorf("output = %q, want %q", e.Output, "context entry")
|
|
||||||
}
|
|
||||||
c.Destroy()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestLevelString verifies the Level.String() method.
|
|
||||||
func TestLevelString(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
level Level
|
|
||||||
want string
|
|
||||||
}{
|
|
||||||
{LTrace, "TRACE"},
|
|
||||||
{LDebug, "DEBUG"},
|
|
||||||
{LInfo, "INFO"},
|
|
||||||
{LNotice, "NOTICE"},
|
|
||||||
{LWarn, "WARN"},
|
|
||||||
{LError, "ERROR"},
|
|
||||||
{LPanic, "PANIC"},
|
|
||||||
{LFatal, "FATAL"},
|
|
||||||
{Level(99), "UNKNOWN"},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
got := tt.level.String()
|
|
||||||
if got != tt.want {
|
|
||||||
t.Errorf("Level(%d).String() = %q, want %q", tt.level, got, tt.want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFlush(t *testing.T) {
|
func TestFlush(t *testing.T) {
|
||||||
defer Flush()
|
defer Flush()
|
||||||
}
|
}
|
||||||
|
|||||||
25
log/types.go
25
log/types.go
@@ -15,31 +15,6 @@ const (
|
|||||||
|
|
||||||
const DefaultNamespace = "default"
|
const DefaultNamespace = "default"
|
||||||
|
|
||||||
// String returns the human-readable name of the log level (e.g. "INFO").
|
|
||||||
// It implements [fmt.Stringer].
|
|
||||||
func (l Level) String() string {
|
|
||||||
switch l {
|
|
||||||
case LTrace:
|
|
||||||
return "TRACE"
|
|
||||||
case LDebug:
|
|
||||||
return "DEBUG"
|
|
||||||
case LInfo:
|
|
||||||
return "INFO"
|
|
||||||
case LNotice:
|
|
||||||
return "NOTICE"
|
|
||||||
case LWarn:
|
|
||||||
return "WARN"
|
|
||||||
case LError:
|
|
||||||
return "ERROR"
|
|
||||||
case LPanic:
|
|
||||||
return "PANIC"
|
|
||||||
case LFatal:
|
|
||||||
return "FATAL"
|
|
||||||
default:
|
|
||||||
return "UNKNOWN"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type (
|
type (
|
||||||
LogWriter chan Entry
|
LogWriter chan Entry
|
||||||
Level int
|
Level int
|
||||||
|
|||||||
@@ -1,62 +0,0 @@
|
|||||||
package ws
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
logger "github.com/taigrr/log-socket/v2/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNamespacesHandler(t *testing.T) {
|
|
||||||
// Log to a known namespace to ensure it appears
|
|
||||||
nsLogger := logger.NewLogger("ns-handler-test")
|
|
||||||
nsLogger.Info("register namespace")
|
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "/api/namespaces", nil)
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
NamespacesHandler(w, req)
|
|
||||||
|
|
||||||
if w.Code != http.StatusOK {
|
|
||||||
t.Fatalf("status = %d, want 200", w.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
ct := w.Header().Get("Content-Type")
|
|
||||||
if ct != "application/json" {
|
|
||||||
t.Errorf("Content-Type = %q, want application/json", ct)
|
|
||||||
}
|
|
||||||
|
|
||||||
var result struct {
|
|
||||||
Namespaces []string `json:"namespaces"`
|
|
||||||
}
|
|
||||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
|
||||||
t.Fatalf("failed to unmarshal response: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
found := false
|
|
||||||
for _, ns := range result.Namespaces {
|
|
||||||
if ns == "ns-handler-test" {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
t.Errorf("namespace 'ns-handler-test' not found in %v", result.Namespaces)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNamespacesHandler_ResponseFormat(t *testing.T) {
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "/api/namespaces", nil)
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
NamespacesHandler(w, req)
|
|
||||||
|
|
||||||
var result map[string]interface{}
|
|
||||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
|
||||||
t.Fatalf("response is not valid JSON: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := result["namespaces"]; !ok {
|
|
||||||
t.Error("response missing 'namespaces' key")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
44
ws/server.go
44
ws/server.go
@@ -1,7 +1,6 @@
|
|||||||
package ws
|
package ws
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -12,61 +11,36 @@ import (
|
|||||||
|
|
||||||
var upgrader = websocket.Upgrader{} // use default options
|
var upgrader = websocket.Upgrader{} // use default options
|
||||||
|
|
||||||
// SetUpgrader replaces the default [websocket.Upgrader] used by
|
|
||||||
// [LogSocketHandler].
|
|
||||||
func SetUpgrader(u websocket.Upgrader) {
|
func SetUpgrader(u websocket.Upgrader) {
|
||||||
upgrader = u
|
upgrader = u
|
||||||
}
|
}
|
||||||
|
|
||||||
// LogSocketHandler upgrades the HTTP connection to a WebSocket and streams
|
|
||||||
// log entries to the client. An optional "namespaces" query parameter
|
|
||||||
// (comma-separated) filters which namespaces the client receives.
|
|
||||||
func LogSocketHandler(w http.ResponseWriter, r *http.Request) {
|
func LogSocketHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
// Get namespaces from query parameter, comma-separated.
|
// Get namespaces from query parameter, comma-separated
|
||||||
// Empty or missing means all namespaces.
|
// Empty or missing means all namespaces
|
||||||
namespacesParam := r.URL.Query().Get("namespaces")
|
namespacesParam := r.URL.Query().Get("namespaces")
|
||||||
var namespaces []string
|
var namespaces []string
|
||||||
if namespacesParam != "" {
|
if namespacesParam != "" {
|
||||||
namespaces = strings.Split(namespacesParam, ",")
|
namespaces = strings.Split(namespacesParam, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
c, err := upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("upgrade:", err)
|
logger.Error("upgrade:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer c.Close()
|
||||||
|
|
||||||
lc := logger.CreateClient(namespaces...)
|
lc := logger.CreateClient(namespaces...)
|
||||||
defer lc.Destroy()
|
defer lc.Destroy()
|
||||||
lc.SetLogLevel(logger.LTrace)
|
lc.SetLogLevel(logger.LTrace)
|
||||||
logger.Info("Websocket client attached.")
|
logger.Info("Websocket client attached.")
|
||||||
|
|
||||||
// Start a read pump so the server detects client disconnects promptly.
|
|
||||||
// Without this, a disconnected client is only noticed when WriteMessage
|
|
||||||
// fails, which can be delayed indefinitely when no logs are produced.
|
|
||||||
ctx, cancel := context.WithCancel(r.Context())
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer cancel()
|
|
||||||
for {
|
for {
|
||||||
if _, _, err := conn.ReadMessage(); err != nil {
|
logEvent := lc.Get()
|
||||||
return
|
logJSON, _ := json.Marshal(logEvent)
|
||||||
}
|
err = c.WriteMessage(websocket.TextMessage, logJSON)
|
||||||
}
|
if err != nil {
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
|
||||||
entry, ok := lc.GetContext(ctx)
|
|
||||||
if !ok {
|
|
||||||
// Context cancelled — client disconnected.
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logJSON, _ := json.Marshal(entry)
|
|
||||||
if err := conn.WriteMessage(websocket.TextMessage, logJSON); err != nil {
|
|
||||||
logger.Warn("write:", err)
|
logger.Warn("write:", err)
|
||||||
return
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,127 +0,0 @@
|
|||||||
package ws
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
logger "github.com/taigrr/log-socket/v2/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestSetUpgrader(t *testing.T) {
|
|
||||||
custom := websocket.Upgrader{
|
|
||||||
ReadBufferSize: 2048,
|
|
||||||
WriteBufferSize: 2048,
|
|
||||||
}
|
|
||||||
SetUpgrader(custom)
|
|
||||||
if upgrader.ReadBufferSize != 2048 {
|
|
||||||
t.Errorf("ReadBufferSize = %d, want 2048", upgrader.ReadBufferSize)
|
|
||||||
}
|
|
||||||
if upgrader.WriteBufferSize != 2048 {
|
|
||||||
t.Errorf("WriteBufferSize = %d, want 2048", upgrader.WriteBufferSize)
|
|
||||||
}
|
|
||||||
// Reset to default
|
|
||||||
SetUpgrader(websocket.Upgrader{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLogSocketHandler_NonWebSocket(t *testing.T) {
|
|
||||||
// A non-upgrade request should fail gracefully (upgrader returns error)
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "/ws", nil)
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
LogSocketHandler(w, req)
|
|
||||||
// The upgrader should return a 400-level error for non-websocket requests
|
|
||||||
if w.Code == http.StatusOK || w.Code == http.StatusSwitchingProtocols {
|
|
||||||
t.Errorf("expected error status for non-websocket request, got %d", w.Code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLogSocketHandler_WebSocket(t *testing.T) {
|
|
||||||
// Set upgrader with permissive origin check for testing
|
|
||||||
SetUpgrader(websocket.Upgrader{
|
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
|
||||||
})
|
|
||||||
defer SetUpgrader(websocket.Upgrader{})
|
|
||||||
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(LogSocketHandler))
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
wsURL := "ws" + strings.TrimPrefix(server.URL, "http") + "/ws"
|
|
||||||
conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to connect: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Send a log entry and verify it arrives over the websocket
|
|
||||||
testLogger := logger.NewLogger("ws-test")
|
|
||||||
testLogger.Info("test message for websocket")
|
|
||||||
|
|
||||||
// Read messages until we find our test entry (the handler itself
|
|
||||||
// logs "Websocket client attached." which may arrive first)
|
|
||||||
conn.SetReadDeadline(time.Now().Add(2 * time.Second))
|
|
||||||
var found bool
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
_, message, err := conn.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to read message: %v", err)
|
|
||||||
}
|
|
||||||
var entry logger.Entry
|
|
||||||
if err := json.Unmarshal(message, &entry); err != nil {
|
|
||||||
t.Fatalf("failed to unmarshal entry: %v", err)
|
|
||||||
}
|
|
||||||
if entry.Namespace == "ws-test" && entry.Level == "INFO" {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
t.Error("did not receive expected log entry with namespace ws-test")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLogSocketHandler_NamespaceFilter(t *testing.T) {
|
|
||||||
SetUpgrader(websocket.Upgrader{
|
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
|
||||||
})
|
|
||||||
defer SetUpgrader(websocket.Upgrader{})
|
|
||||||
|
|
||||||
server := httptest.NewServer(http.HandlerFunc(LogSocketHandler))
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
// Connect with namespace filter
|
|
||||||
wsURL := "ws" + strings.TrimPrefix(server.URL, "http") + "/ws?namespaces=filtered-ns"
|
|
||||||
conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to connect: %v", err)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
// Send a log to a different namespace — it should NOT be received
|
|
||||||
otherLogger := logger.NewLogger("other-ns")
|
|
||||||
otherLogger.Info("should not arrive")
|
|
||||||
|
|
||||||
// Send a log to the filtered namespace — it SHOULD be received
|
|
||||||
filteredLogger := logger.NewLogger("filtered-ns")
|
|
||||||
filteredLogger.Info("should arrive")
|
|
||||||
|
|
||||||
conn.SetReadDeadline(time.Now().Add(2 * time.Second))
|
|
||||||
_, message, err := conn.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to read message: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var entry logger.Entry
|
|
||||||
if err := json.Unmarshal(message, &entry); err != nil {
|
|
||||||
t.Fatalf("failed to unmarshal entry: %v", err)
|
|
||||||
}
|
|
||||||
if entry.Namespace != "filtered-ns" {
|
|
||||||
t.Errorf("namespace = %q, want filtered-ns", entry.Namespace)
|
|
||||||
}
|
|
||||||
if !strings.Contains(entry.Output, "should arrive") {
|
|
||||||
t.Errorf("output = %q, want to contain 'should arrive'", entry.Output)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user