Skip to content

Log Viewer

A scrolling mono output area with a toolbar. Lines are appended, colored by level, bounded to maxLines, and scrolled into view while Auto scroll is on.

Decision

When Use
A session log or the output of a run Log Viewer
A long history Paginated Table

Usage

import { createLogViewer } from './configurationpage?name=myplugin_stolon.js';

var log = createLogViewer({ maxLines: 500 });
container.appendChild(log.element);
log.append({ text: 'Started', level: 'info' });

Classes: stolon-log, stolon-log-output, stolon-log-line. The handle is { element, output, append, clear, getValue, setValue, setAutoScroll, setHeight, copyAll, destroy }.

Parts: stolon-log-empty, stolon-log-time, stolon-log-toggle, stolon-log-toolbar.

States and Modifiers

  • lines sets the first lines. append takes one line or an array. A line is a string or { text, level, time }, where level is debug, info, warn, or error.
  • maxLines is 500 by default, and the oldest lines go first.
  • The output is a List, so height, maxHeight, minHeight, and setHeight work as they do there. height is 280 by default, and 'fill' lets a log take the rest of the page.
  • Scrolling up turns Auto scroll off. autoScroll sets the first state.
  • The toolbar has the "Auto scroll" toggle and the "Clear" and "Copy all" buttons. toolbar: false hides it. autoScrollText, clearText, copyText, copiedText ("Copied"), and failedText ("Copy failed.") replace the labels, and onClear runs after Clear. emptyText is "No output yet." by default, and shows in the output as the List's empty sentence.
  • The output is role="log" with aria-live="off", so a stream does not flood a screen reader. label ("Log") names it, and it takes focus for keyboard scrolling. Copy all uses copyToClipboard.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createLogViewer

function createLogViewer(options?: LogViewerOptions): LogViewerHandle

A scrolling mono output area with a toolbar. Lines are appended, colored by level, bounded to maxLines, and scrolled into view while Auto scroll is on. Scrolling up turns Auto scroll off.

Option Type What it does
lines LogLineInput[] array of strings or { text, level, time } where level is debug, info, warn, or error [optional]
maxLines number most lines kept, where the oldest go first (default 500)
autoScroll boolean true follows new lines (default true)
label string aria-label of the output (default 'Log')
autoScrollText string label of the toggle (default 'Auto scroll')
clearText string text of the clear button (default 'Clear')
copyText string text of the copy button (default 'Copy all')
copiedText string feedback after a copy (default 'Copied')
failedText string feedback when the copy failed (default 'Copy failed.')
emptyText string sentence shown while there are no lines (default 'No output yet.')
toolbar boolean false hides the toolbar (default true)
height ListHeight the output's height, 'auto', 'fill', a number of pixels, or a CSS length (default 280)
maxHeight number | string | null the most the output grows to before it scrolls [optional]
minHeight number | string | null the least a filling output takes (default 160px) [optional]
onClear (): void function() called after Clear is pressed [optional]
Handle member Type
output HTMLDivElement
append (next: LogLineInput | LogLineInput[]): void
clear (): void
getValue (): LogLine[]
setValue (next: LogLineInput[]): void
setAutoScroll (on: boolean): void
setHeight (height: ListHeight, maxHeight?: number | string | null): void
copyAll (): Promise<boolean>
destroy (): void
Type What it is
LogLine One line as the log viewer keeps it. level is debug, info, warn, or error.
LogLineInput A line that append takes, as plain text or as an object with a text, a level, and a time.
Class What it looks like
stolon-log Log viewer with a toolbar over a scrolling mono output area
stolon-log-output Area of lines in the mono font, given its height and scrolling by createList

Example

Example Code

<!-- title: Log Viewer -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const mount = document.getElementById('mount');
const log = base.createLogViewer({ maxLines: 50, lines: [
    { text: 'Starting the session', level: 'info', time: '10:00:01' },
    { text: 'Retrying in 5 seconds', level: 'warn', time: '10:00:03' },
    { text: 'Could not reach the server', level: 'error', time: '10:00:08' },
    { text: 'Detail line', level: 'debug', time: '10:00:08' }
] });
mount.appendChild(log.element);
let n = 0;
const button = document.createElement('button');
button.type = 'button';
button.className = 'raised stolon-button stolon-small';
button.style.marginTop = '10px';
button.textContent = 'Append Lines';
button.addEventListener('click', () => { for (let i = 0; i < 5; i++) log.append('Line ' + (++n)); });
mount.appendChild(button);
</script>