Files
nginx-proxy-manager/frontend/src/pages/Logs/LogLines.tsx
T
carlosalbertorg 4282d6c6e9 feat: Add a Logs viewer to the admin UI
Nginx Proxy Manager had no way to inspect application or nginx logs
from the web UI - admins had to shell into the container or read
`docker logs`. This adds an admin-only Logs page that can tail:

- the backend application log, now also mirrored to
  /data/logs/backend.log (in addition to stdout) and rotated by the
  logrotate timer that already runs every 2 days
- the Let's Encrypt/certbot log, which certbot already writes to
  /data/logs/letsencrypt.log via its existing --logs-dir flag
- per-host nginx access/error logs (proxy, redirection, 404 and
  stream hosts), with the file path always resolved server-side from
  a validated host_type enum + numeric host_id, never from client
  input

Reads use a reverse chunked scan (64KB chunks, capped at 5MB scanned
per request) instead of loading whole files into memory, and the
frontend polls every 5s only while the tab is focused and "Live" is
on, so this stays cheap on both CPU and memory. No new runtime
dependencies were added on either side.

Purely additive: two new admin-only endpoints
(GET /api/logs/sources, GET /api/logs/tail), no existing behaviour
changed.
2026-09-22 13:11:38 -03:00

77 lines
1.9 KiB
TypeScript

import cn from "classnames";
import { useEffect, useRef } from "react";
import { T } from "src/locale";
import styles from "./LogViewer.module.css";
const LEVEL_PATTERN = /\b(ERROR|FATAL|WARN|SUCCESS|COMPLETE|DEBUG|INFO)\b/;
const levelClassName = (line: string): string | undefined => {
const match = line.match(LEVEL_PATTERN);
if (!match) {
return undefined;
}
switch (match[1]) {
case "ERROR":
case "FATAL":
return "text-danger";
case "WARN":
return "text-warning";
case "SUCCESS":
case "COMPLETE":
return "text-success";
case "DEBUG":
return "text-secondary";
default:
return undefined;
}
};
// How close to the bottom (in pixels) the user has to be for auto-scroll to keep
// following new lines. Scrolling further up than this to read history disables it,
// so newly polled lines never yank the viewport away from what's being read.
const AUTO_SCROLL_THRESHOLD_PX = 40;
interface Props {
lines: string[];
}
export default function LogLines({ lines }: Props) {
const containerRef = useRef<HTMLPreElement>(null);
const stickToBottomRef = useRef(true);
const handleScroll = () => {
const el = containerRef.current;
if (!el) {
return;
}
const distanceFromBottom = el.scrollHeight - el.scrollTop - el.clientHeight;
stickToBottomRef.current = distanceFromBottom < AUTO_SCROLL_THRESHOLD_PX;
};
// biome-ignore lint/correctness/useExhaustiveDependencies: lines is a re-scroll trigger, not read in the body
useEffect(() => {
const el = containerRef.current;
if (el && stickToBottomRef.current) {
el.scrollTop = el.scrollHeight;
}
}, [lines]);
if (!lines.length) {
return (
<div className={cn(styles.logBox, "text-secondary")}>
<T id="logs.empty" />
</div>
);
}
return (
<pre ref={containerRef} onScroll={handleScroll} className={styles.logBox}>
{lines.map((line, idx) => (
<span key={idx} className={cn(styles.logLine, levelClassName(line))}>
{line}
</span>
))}
</pre>
);
}