Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: tail container and project logs properly on all ws hubs
Kyle Mendellb861c19757bc
diff --git a/backend/internal/api/container_handler.go b/backend/internal/api/container_handler.goindex 513d061df..2d615a7e1 100644--- a/backend/internal/api/container_handler.go+++ b/backend/internal/api/container_handler.go@@ -30,7 +30,7 @@ type ContainerHandler struct {containerService *services.ContainerServiceimageService *services.ImageServicedockerService *services.DockerClientService- logStreams sync.Map+ statsStreams sync.MapcontainerWSUpgrader websocket.Upgrader}@@ -67,87 +67,82 @@ func NewContainerHandler(group *gin.RouterGroup, dockerService *services.DockerCtype containerLogStream struct {hub *ws.Hub- once sync.Oncecancel context.CancelFuncformat stringseq atomic.Uint64}func (h *ContainerHandler) getOrStartContainerLogHub(containerID, format string, batched bool, follow bool, tail, since string, timestamps bool) *ws.Hub {- key := fmt.Sprintf("%s::%s::batched=%t", containerID, format, batched)- v, _ := h.logStreams.LoadOrStore(key, &containerLogStream{+ // Create a new hub for each connection to ensure every client gets historical logs+ ls := &containerLogStream{hub: ws.NewHub(1024),format: format,+ }++ ctx, cancel := context.WithCancel(context.Background())+ ls.cancel = cancel++ ls.hub.SetOnEmpty(func() {+ slog.Debug("client disconnected, cleaning up log hub", "containerID", containerID, "format", format, "tail", tail)+ cancel()})- ls := v.(*containerLogStream)- ls.once.Do(func() {- ctx, cancel := context.WithCancel(context.Background())- ls.cancel = cancel+ go ls.hub.Run(ctx)- ls.hub.SetOnEmpty(func() {- slog.Debug("all clients disconnected, cleaning up log hub", "containerID", containerID, "format", format)- cancel()- h.logStreams.Delete(key)- })+ slog.Debug("starting log stream pipeline", "containerID", containerID, "format", format, "batched", batched)- go ls.hub.Run(ctx)+ lines := make(chan string, 256)+ go func() {+ defer func() {+ close(lines)+ slog.Debug("lines channel closed", "containerID", containerID)+ }()- slog.Debug("starting log stream pipeline", "containerID", containerID, "format", format, "batched", batched)+ if err := h.containerService.StreamLogs(ctx, containerID, lines, follow, tail, since, timestamps); err != nil {+ slog.Error("StreamLogs failed", "containerID", containerID, "err", err)+ }+ }()- lines := make(chan string, 256)+ if format == "json" {+ msgs := make(chan ws.LogMessage, 256)go func() {defer func() {- close(lines)- slog.Debug("lines channel closed", "containerID", containerID)+ close(msgs)+ slog.Debug("msgs channel closed", "containerID", containerID)}()-- if err := h.containerService.StreamLogs(ctx, containerID, lines, follow, tail, since, timestamps); err != nil {- slog.Error("StreamLogs failed", "containerID", containerID, "err", err)- }- }()-- if format == "json" {- msgs := make(chan ws.LogMessage, 256)- go func() {- defer func() {- close(msgs)- slog.Debug("msgs channel closed", "containerID", containerID)- }()- lineCount := 0- for line := range lines {- lineCount++- level, msg, ts := ws.NormalizeContainerLine(line)- seq := ls.seq.Add(1)- timestamp := ts- if timestamp == "" {- timestamp = ws.NowRFC3339()- }- msgs <- ws.LogMessage{- Seq: seq,- Level: level,- Message: msg,- Timestamp: timestamp,- }+ lineCount := 0+ for line := range lines {+ lineCount+++ level, msg, ts := ws.NormalizeContainerLine(line)+ seq := ls.seq.Add(1)+ timestamp := ts+ if timestamp == "" {+ timestamp = ws.NowRFC3339()+ }+ msgs <- ws.LogMessage{+ Seq: seq,+ Level: level,+ Message: msg,+ Timestamp: timestamp,}- }()- if batched {- go ws.ForwardLogJSONBatched(ctx, ls.hub, msgs, 50, 400*time.Millisecond)- } else {- go ws.ForwardLogJSON(ctx, ls.hub, msgs)}+ }()+ if batched {+ go ws.ForwardLogJSONBatched(ctx, ls.hub, msgs, 50, 400*time.Millisecond)} else {- cleanChan := make(chan string, 256)- go func() {- defer close(cleanChan)- for line := range lines {- _, msg, _ := ws.NormalizeContainerLine(line)- cleanChan <- msg- }- }()- go ws.ForwardLines(ctx, ls.hub, cleanChan)+ go ws.ForwardLogJSON(ctx, ls.hub, msgs)}- })+ } else {+ cleanChan := make(chan string, 256)+ go func() {+ defer close(cleanChan)+ for line := range lines {+ _, msg, _ := ws.NormalizeContainerLine(line)+ cleanChan <- msg+ }+ }()+ go ws.ForwardLines(ctx, ls.hub, cleanChan)+ }return ls.hub}@@ -571,7 +566,7 @@ type containerStatsStream struct {func (h *ContainerHandler) getOrStartContainerStatsHub(containerID string) *ws.Hub {key := fmt.Sprintf("%s::stats", containerID)- v, _ := h.logStreams.LoadOrStore(key, &containerStatsStream{+ v, _ := h.statsStreams.LoadOrStore(key, &containerStatsStream{hub: ws.NewHub(1024),})ss := v.(*containerStatsStream)@@ -583,7 +578,7 @@ func (h *ContainerHandler) getOrStartContainerStatsHub(containerID string) *ws.Hss.hub.SetOnEmpty(func() {slog.Debug("all clients disconnected, cleaning up stats hub", "containerID", containerID)cancel()- h.logStreams.Delete(key)+ h.statsStreams.Delete(key)})go ss.hub.Run(ctx)diff --git a/backend/internal/api/project_handler.go b/backend/internal/api/project_handler.goindex e85221dcc..9ab060db9 100644--- a/backend/internal/api/project_handler.go+++ b/backend/internal/api/project_handler.go@@ -7,7 +7,6 @@ import ("log/slog""net/http""strings"- "sync""sync/atomic""time"@@ -25,13 +24,11 @@ import (type ProjectHandler struct {projectService *services.ProjectService- logStreams sync.MapwsUpgrader websocket.Upgrader}type projectLogStream struct {hub *ws.Hub- once sync.Oncecancel context.CancelFuncformat stringseq atomic.Uint64@@ -315,68 +312,64 @@ func (h *ProjectHandler) RestartProject(c *gin.Context) {}func (h *ProjectHandler) getOrStartProjectLogHub(projectID, format string, batched bool, follow bool, tail, since string, timestamps bool) *ws.Hub {- key := projectID + "::" + format- v, _ := h.logStreams.LoadOrStore(key, &projectLogStream{+ // Create a new hub for each connection to ensure every client gets historical logs+ ls := &projectLogStream{hub: ws.NewHub(1024),format: format,- })- ls := v.(*projectLogStream)+ }- ls.once.Do(func() {- ctx, cancel := context.WithCancel(context.Background())- ls.cancel = cancel+ ctx, cancel := context.WithCancel(context.Background())+ ls.cancel = cancel- ls.hub.SetOnEmpty(func() {- slog.Debug("all clients disconnected, cleaning up project log hub", "projectID", projectID, "format", format)- cancel()- h.logStreams.Delete(key)- })+ ls.hub.SetOnEmpty(func() {+ slog.Debug("client disconnected, cleaning up project log hub", "projectID", projectID, "format", format, "tail", tail)+ cancel()+ })- go ls.hub.Run(ctx)+ go ls.hub.Run(ctx)- lines := make(chan string, 256)- go func() {- defer close(lines)- _ = h.projectService.StreamProjectLogs(ctx, projectID, lines, follow, tail, since, timestamps)- }()+ lines := make(chan string, 256)+ go func() {+ defer close(lines)+ _ = h.projectService.StreamProjectLogs(ctx, projectID, lines, follow, tail, since, timestamps)+ }()- if format == "json" {- msgs := make(chan ws.LogMessage, 256)- go func() {- defer close(msgs)- for line := range lines {- level, service, msg, ts := ws.NormalizeProjectLine(line)- seq := ls.seq.Add(1)- timestamp := ts- if timestamp == "" {- timestamp = ws.NowRFC3339()- }- msgs <- ws.LogMessage{- Seq: seq,- Level: level,- Message: msg,- Service: service,- Timestamp: timestamp,- }+ if format == "json" {+ msgs := make(chan ws.LogMessage, 256)+ go func() {+ defer close(msgs)+ for line := range lines {+ level, service, msg, ts := ws.NormalizeProjectLine(line)+ seq := ls.seq.Add(1)+ timestamp := ts+ if timestamp == "" {+ timestamp = ws.NowRFC3339()+ }+ msgs <- ws.LogMessage{+ Seq: seq,+ Level: level,+ Message: msg,+ Service: service,+ Timestamp: timestamp,}- }()- if batched {- go ws.ForwardLogJSONBatched(ctx, ls.hub, msgs, 50, 400*time.Millisecond)- } else {- go ws.ForwardLogJSON(ctx, ls.hub, msgs)}+ }()+ if batched {+ go ws.ForwardLogJSONBatched(ctx, ls.hub, msgs, 50, 400*time.Millisecond)} else {- cleanChan := make(chan string, 256)- go func() {- defer close(cleanChan)- for line := range lines {- _, _, msg, _ := ws.NormalizeProjectLine(line)- cleanChan <- msg- }- }()- go ws.ForwardLines(ctx, ls.hub, cleanChan)+ go ws.ForwardLogJSON(ctx, ls.hub, msgs)}- })+ } else {+ cleanChan := make(chan string, 256)+ go func() {+ defer close(cleanChan)+ for line := range lines {+ _, _, msg, _ := ws.NormalizeProjectLine(line)+ cleanChan <- msg+ }+ }()+ go ws.ForwardLines(ctx, ls.hub, cleanChan)+ }return ls.hub}diff --git a/backend/internal/utils/projects/compose.go b/backend/internal/utils/projects/compose.goindex 6d2cf82a3..af4badf81 100644--- a/backend/internal/utils/projects/compose.go+++ b/backend/internal/utils/projects/compose.go@@ -49,17 +49,27 @@ func (w writerConsumer) Log(container, msg string) {if w.out == nil {return}- if !strings.HasSuffix(msg, "\n") {- msg += "\n"+ // Include container/service name in the format expected by NormalizeProjectLine+ output := msg+ if container != "" {+ output = container + " | " + msg}- _, _ = io.WriteString(w.out, msg)+ if !strings.HasSuffix(output, "\n") {+ output += "\n"+ }+ _, _ = io.WriteString(w.out, output)}func (w writerConsumer) Err(container, msg string) {if w.out == nil {return}- if !strings.HasSuffix(msg, "\n") {- msg += "\n"+ // Include container/service name in the format expected by NormalizeProjectLine+ output := msg+ if container != "" {+ output = container + " | " + msg+ }+ if !strings.HasSuffix(output, "\n") {+ output += "\n"}- _, _ = io.WriteString(w.out, msg)+ _, _ = io.WriteString(w.out, output)}diff --git a/frontend/messages/en.json b/frontend/messages/en.jsonindex d3dfa668a..d5092e8fa 100644--- a/frontend/messages/en.json+++ b/frontend/messages/en.json@@ -721,11 +721,20 @@"log_stream_connection_lost": "Connection to {type} log stream lost","log_stream_closed_by_server": "{type} log stream was closed by server","log_stream_failed_connect": "Failed to connect to {type} log stream",+ "log_stream_no_project_selected": "No project selected. Please select a project to view logs.",+ "log_stream_no_container_selected": "No container selected. Please select a container to view logs.","log_viewer_stopping": "Stopping {type} log stream","log_viewer_connected": "{type} log stream connected","log_viewer_no_selection": "No {type} selected","log_viewer_no_logs_available": "No logs available. Start streaming to see logs.","log_viewer_waiting_for_logs": "Waiting for logs...",+ "log_tail_50_lines": "50 lines",+ "log_tail_100_lines": "100 lines",+ "log_tail_200_lines": "200 lines",+ "log_tail_500_lines": "500 lines",+ "log_tail_1000_lines": "1000 lines",+ "log_tail_all_lines": "All logs",+ "log_refresh_aria_label": "Refresh logs","container": "Container","project": "Project","common_loading": "Loading...",diff --git a/frontend/src/lib/components/logs/log-controls.svelte b/frontend/src/lib/components/logs/log-controls.svelteindex 4deea06a1..0136bf3bb 100644--- a/frontend/src/lib/components/logs/log-controls.svelte+++ b/frontend/src/lib/components/logs/log-controls.svelte@@ -1,10 +1,13 @@<script lang="ts">import { Button } from '$lib/components/ui/button/index.js';+ import * as Select from '$lib/components/ui/select';import RefreshCwIcon from '@lucide/svelte/icons/refresh-cw';import { m } from '$lib/paraglide/messages';+ import { PersistedState } from 'runed';let {autoScroll = $bindable(),+ tailLines = $bindable(100),isStreaming = false,disabled = false,onStart,@@ -13,6 +16,7 @@onRefresh}: {autoScroll: boolean;+ tailLines?: number;isStreaming?: boolean;disabled?: boolean;onStart?: () => void;@@ -20,6 +24,30 @@onClear?: () => void;onRefresh?: () => void;} = $props();++ const tailOptions = [+ { value: '50', label: m.log_tail_50_lines() },+ { value: '100', label: m.log_tail_100_lines() },+ { value: '200', label: m.log_tail_200_lines() },+ { value: '500', label: m.log_tail_500_lines() },+ { value: '1000', label: m.log_tail_1000_lines() },+ { value: 'all', label: m.log_tail_all_lines() }+ ];++ const persistedTailLines = new PersistedState('arcane_log_tail_lines', '100');++ let selectedTail = $state<string>(persistedTailLines.current);++ $effect(() => {+ persistedTailLines.current = selectedTail;+ if (selectedTail === 'all') {+ tailLines = 999999;+ } else {+ tailLines = parseInt(selectedTail, 10);+ }+ });++ const selectedLabel = $derived(tailOptions.find((o) => o.value === selectedTail)?.label ?? m.log_tail_100_lines());</script><div class="flex flex-col gap-3 sm:flex-row sm:items-center">@@ -28,6 +56,17 @@<span class="text-sm font-medium">{m.common_autoscroll()}</span></label>+ <Select.Root type="single" bind:value={selectedTail} disabled={isStreaming} onValueChange={(v: string) => (selectedTail = v)}>+ <Select.Trigger class="h-9 w-32 text-xs">+ <span>{selectedLabel}</span>+ </Select.Trigger>+ <Select.Content>+ {#each tailOptions as option (option.value)}+ <Select.Item value={option.value}>{option.label}</Select.Item>+ {/each}+ </Select.Content>+ </Select.Root>+<div class="flex items-center gap-2"><Button variant="outline" size="sm" class="text-xs font-medium" onclick={onClear}>{m.common_clear()}@@ -41,7 +80,14 @@{m.common_start()}</Button>{/if}- <Button variant="outline" size="sm" class="px-2" onclick={onRefresh} aria-label="Refresh logs" title="Refresh">+ <Button+ variant="outline"+ size="sm"+ class="px-2"+ onclick={onRefresh}+ aria-label={m.log_refresh_aria_label()}+ title={m.common_refresh()}+ ><RefreshCwIcon class="size-4" /></Button></div>diff --git a/frontend/src/lib/components/logs/log-viewer.svelte b/frontend/src/lib/components/logs/log-viewer.svelteindex 8cacd96ae..2ee85152c 100644--- a/frontend/src/lib/components/logs/log-viewer.svelte+++ b/frontend/src/lib/components/logs/log-viewer.svelte@@ -24,6 +24,7 @@autoScroll?: boolean;showTimestamps?: boolean;height?: string;+ tailLines?: number;onClear?: () => void;onToggleAutoScroll?: () => void;onStart?: () => void;@@ -39,6 +40,7 @@autoScroll = $bindable(true),showTimestamps = true,height = '400px',+ tailLines = 100,onClear,onToggleAutoScroll,onStart,@@ -146,13 +148,20 @@type === 'project'? `/api/environments/${envId}/projects/${projectId}/logs/ws`: `/api/environments/${envId}/containers/${containerId}/logs/ws`;- return buildWebSocketEndpoint(`${basePath}?follow=true&tail=100×tamps=${showTimestamps}&format=json&batched=true`);+ return buildWebSocketEndpoint(+ `${basePath}?follow=true&tail=${tailLines}×tamps=${showTimestamps}&format=json&batched=true`+ );}export async function startLogStream() {const targetId = type === 'project' ? projectId : containerId;- if (!targetId) return;+ if (!targetId) {+ error = type === 'project' ? m.log_stream_no_project_selected() : m.log_stream_no_container_selected();+ isStreaming = false;+ shouldBeStreaming = false;+ return;+ }// Prevent starting if already streamingif (shouldBeStreaming && wsClient) {diff --git a/frontend/src/routes/projects/components/ProjectLogsPanel.svelte b/frontend/src/routes/projects/components/ProjectLogsPanel.svelteindex 2684892ff..e1185e97b 100644--- a/frontend/src/routes/projects/components/ProjectLogsPanel.svelte+++ b/frontend/src/routes/projects/components/ProjectLogsPanel.svelte@@ -15,6 +15,7 @@let isStreaming = $state(false);let viewer = $state<LogViewer>();+ let tailLines = $state(100);function handleStart() {isStreaming = true;@@ -57,6 +58,7 @@</div><LogControlsbind:autoScroll+ bind:tailLines{isStreaming}disabled={!projectId}onStart={handleStart}@@ -71,6 +73,7 @@bind:this={viewer}bind:autoScroll{projectId}+ {tailLines}type="project"maxLines={500}showTimestamps={true}