Files
portainer/app/react/docker/containers/LogView/LogView.tsx
T
agent_coder 7f02d20e54 feat(logs): React log viewer for container logs
Replace the legacy AngularJS <log-viewer> on the container logs page with a
modern React log viewer, reusing the existing streaming (#6) and formatting/
coloring pipeline. Features: line-number gutter, zerolog level + key=value
coloring (from the existing formatter spans), from/to datetime range, Lines
limit, Line numbers / Timestamp / Wrap lines toggles, Auto refresh (live tail
on/off), Search + 'Filter search results', Copy, Download logs, and fullscreen.

The viewer is source-agnostic (StreamLogsFn), so service/task logs can adopt it
later; this PR wires container logs only. containerLogsController.js no longer
opens its own live stream (React owns fetching now), preventing a double stream.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-01 19:05:56 +03:00

99 lines
2.9 KiB
TypeScript

import { useCurrentStateAndParams } from '@uirouter/react';
import { useContainer } from '@/react/docker/containers/queries/useContainer';
import {
streamContainerLogs,
StreamLogsParams,
} from '@/react/docker/containers/containers.service';
import { ContainerDetailsViewModel } from '@/docker/models/containerDetails';
import { trimContainerName } from '@/docker/filters/utils';
import { InformationPanel } from '@@/InformationPanel';
import { TextTip } from '@@/Tip/TextTip';
import { Link } from '@@/Link';
import { LogViewer } from '@@/LogViewer';
export function LogView() {
const {
params: { endpointId: environmentId, id: containerId, nodeName },
} = useCurrentStateAndParams();
const containerQuery = useContainer(
{ environmentId, containerId, nodeName },
{
select: (c) => new ContainerDetailsViewModel(c),
}
);
if (!containerQuery.data || containerQuery.isLoading) {
return null;
}
const container = containerQuery.data;
const logsEnabled =
container.HostConfig?.LogConfig?.Type && // if a portion of the object path doesn't exist, logging is likely disabled
container.HostConfig.LogConfig.Type !== 'none'; // if type === none logging is disabled
if (!logsEnabled) {
return <LogsDisabledInfoPanel />;
}
// A TTY container emits a single raw stream; a non-TTY container multiplexes
// stdout/stderr with 8-byte frame headers the stream processor must demux.
const hasFrameHeaders = !container.Config?.Tty;
const resourceName = trimContainerName(container.Name || 'container');
// Bind the environment/container so the viewer only knows the generic
// "open a log stream" contract (StreamLogsFn), not Docker proxy specifics.
function streamLogs(
params: StreamLogsParams,
onChunk: (bytes: Uint8Array) => void,
signal: AbortSignal
) {
return streamContainerLogs(
environmentId,
containerId,
params,
onChunk,
signal
);
}
return (
<LogViewer
streamLogs={streamLogs}
resourceId={containerId}
hasFrameHeaders={hasFrameHeaders}
resourceName={resourceName}
/>
);
}
function LogsDisabledInfoPanel() {
const {
params: { id: containerId, nodeName },
} = useCurrentStateAndParams();
return (
<div className="row">
<div className="col-sm-12">
<InformationPanel>
<TextTip color="blue">
Logging is disabled for this container. If you want to re-enable
logging, please{' '}
<Link
to="docker.containers.new"
params={{ from: containerId, nodeName }}
data-cy="redeploy-container-link"
>
redeploy your container
</Link>{' '}
and select a logging driver in the &quot;Command & logging&quot;
panel.
</TextTip>
</InformationPanel>
</div>
</div>
);
}