v0.26.3 web viewer improvement
This commit is contained in:
1 parent
d745a128a2
commit
c00c076d9b
14 files changed
+159
-85
No files matched your search
@@ -2,6 +2,7 @@ package core
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -24,3 +25,41 @@ type TransportEvent struct {
|
||||
// Structured entry for re-serializing sinks (chain links). Zero Time => absent
|
||||
Entry LogEntry
|
||||
}
|
||||
|
||||
// Levels are the entry levels by rising severity, each with the words naming
|
||||
// it in a line: the sources, the console sink's painter and the viewer share them
|
||||
var Levels = []struct {
|
||||
Level string `json:"level"`
|
||||
Names []string `json:"names"`
|
||||
}{
|
||||
{"TRACE", []string{"TRACE"}},
|
||||
{"DEBUG", []string{"DEBUG", "DBG"}},
|
||||
{"INFO", []string{"INFO", "INF"}},
|
||||
{"WARN", []string{"WARN", "WARNING"}},
|
||||
{"ERROR", []string{"ERROR", "ERR", "FATAL"}},
|
||||
}
|
||||
|
||||
// LevelWord finds the first whole word of text, in any case, that names a
|
||||
// level, or names want when it is set; it returns the level and the word's
|
||||
// bounds, and "" when no word does.
|
||||
func LevelWord[T string | []byte](text T, want string) (level string, start, end int) {
|
||||
for i := 0; i < len(text); i = end {
|
||||
for i < len(text) && !wordByte(text[i]) {
|
||||
i++
|
||||
}
|
||||
for end = i; end < len(text) && wordByte(text[end]); end++ {
|
||||
}
|
||||
for _, l := range Levels {
|
||||
for _, name := range l.Names {
|
||||
if end-i == len(name) && (want == "" || want == l.Level) && strings.EqualFold(string(text[i:end]), name) {
|
||||
return l.Level, i, end
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", 0, 0
|
||||
}
|
||||
|
||||
func wordByte(c byte) bool {
|
||||
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_'
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package core
|
||||
|
||||
import "testing"
|
||||
|
||||
// A line's level is its first whole word, in any case, naming a level, not the
|
||||
// most severe one named; a wanted level skips the names of the others.
|
||||
func TestLevelIsTheFirstWordNamingOne(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
text, want, level string
|
||||
start, end int
|
||||
}{
|
||||
{"warning disk full", "", "WARN", 0, 7},
|
||||
{"DBG x", "", "DEBUG", 0, 3},
|
||||
{"2026-10-05 [Info] retry after error", "", "INFO", 12, 16},
|
||||
{`{"level":"fatal","msg":"x"}`, "", "ERROR", 10, 15},
|
||||
{"terror informal x_ERR", "", "", 0, 0},
|
||||
{"INFO then ERR", "ERROR", "ERROR", 10, 13},
|
||||
} {
|
||||
level, start, end := LevelWord(c.text, c.want)
|
||||
if level != c.level || start != c.start || end != c.end {
|
||||
t.Errorf("%q want %q: got %q [%d:%d], want %q [%d:%d]", c.text, c.want, level, start, end, c.level, c.start, c.end)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,18 +69,14 @@ const (
|
||||
DefaultConsoleColor = "auto"
|
||||
)
|
||||
|
||||
// levelColors paints the first of a level's names in an entry, ANSI 16 so
|
||||
// the terminal's theme picks the shades and a text console renders them.
|
||||
// Names run longest first; cyan, not blue, stays readable on black.
|
||||
var levelColors = map[string]struct {
|
||||
names []string
|
||||
style inline.Style
|
||||
}{
|
||||
"ERROR": {[]string{"ERROR", "FATAL", "ERR"}, inline.FgANSI(color.ANSIRed).Bold()},
|
||||
"WARN": {[]string{"WARNING", "WARN"}, inline.FgANSI(color.ANSIYellow)},
|
||||
"INFO": {[]string{"INFO", "INF"}, inline.FgANSI(color.ANSIGreen)},
|
||||
"DEBUG": {[]string{"DEBUG", "DBG"}, inline.FgANSI(color.ANSICyan)},
|
||||
"TRACE": {[]string{"TRACE"}, inline.FgANSI(color.ANSIBrightBlack)},
|
||||
// levelStyles colors the levels in ANSI 16, so the terminal's theme picks the
|
||||
// shades and a text console renders them; cyan, not blue, stays readable on black
|
||||
var levelStyles = map[string]inline.Style{
|
||||
"ERROR": inline.FgANSI(color.ANSIRed).Bold(),
|
||||
"WARN": inline.FgANSI(color.ANSIYellow),
|
||||
"INFO": inline.FgANSI(color.ANSIGreen),
|
||||
"DEBUG": inline.FgANSI(color.ANSICyan),
|
||||
"TRACE": inline.FgANSI(color.ANSIBrightBlack),
|
||||
}
|
||||
|
||||
// NewConsoleSinkPlugin creates a console sink through plugin factory
|
||||
@@ -273,42 +269,23 @@ func (cs *ConsoleSink) write(event core.TransportEvent) {
|
||||
cs.lastProcessed.Store(time.Now())
|
||||
}
|
||||
|
||||
// paintLevel colors the first whole-word, case-insensitive occurrence of a
|
||||
// name of level, after escaping: the escape sequences are the sink's own.
|
||||
// paintLevel colors the first word naming the entry's level, after escaping:
|
||||
// the escape sequences are the sink's own
|
||||
func paintLevel(p *inline.Printer, payload []byte, level string) []byte {
|
||||
lc, ok := levelColors[level]
|
||||
level, _, _ = core.LevelWord(level, "")
|
||||
style, ok := levelStyles[level]
|
||||
if !ok {
|
||||
return payload
|
||||
}
|
||||
for _, name := range lc.names {
|
||||
for i := 0; i+len(name) <= len(payload); i++ {
|
||||
j := i + len(name)
|
||||
if !nameAt(payload[i:j], name) || i > 0 && word(payload[i-1]) || j < len(payload) && word(payload[j]) {
|
||||
continue
|
||||
}
|
||||
out := make([]byte, 0, len(payload)+16)
|
||||
out = append(out, payload[:i]...)
|
||||
out = append(out, p.Paint(string(payload[i:j]), lc.style)...)
|
||||
return append(out, payload[j:]...)
|
||||
}
|
||||
if _, i, j := core.LevelWord(payload, level); i < j {
|
||||
out := make([]byte, 0, len(payload)+16)
|
||||
out = append(out, payload[:i]...)
|
||||
out = append(out, p.Paint(string(payload[i:j]), style)...)
|
||||
return append(out, payload[j:]...)
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
// nameAt compares ASCII letters without case; name is upper case
|
||||
func nameAt(b []byte, name string) bool {
|
||||
for k := range len(name) {
|
||||
if c := b[k]; c != name[k] && c != name[k]+'a'-'A' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func word(c byte) bool {
|
||||
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '_'
|
||||
}
|
||||
|
||||
// escapeControls writes what a terminal would act on or reorder (C0 and C1
|
||||
// controls but tab, DEL, bidi controls, line separators, invalid UTF-8) as
|
||||
// <hex>, keeping the final newline: a log line cannot drive the terminal.
|
||||
|
||||
@@ -29,8 +29,8 @@ func TestEscapeControlsKeepsOnlyLayout(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The entry's level is painted where its line first names it as a whole
|
||||
// word, in any case, longest name first; other words and levels stay plain.
|
||||
// The entry's level, under any of its names, is painted where its line first
|
||||
// names it as a whole word, in any case; other words and levels stay plain.
|
||||
func TestPaintLevelColorsTheFirstWholeName(t *testing.T) {
|
||||
p := inline.New(io.Discard)
|
||||
p.SetColor(true)
|
||||
@@ -38,6 +38,7 @@ func TestPaintLevelColorsTheFirstWholeName(t *testing.T) {
|
||||
for _, c := range []struct{ in, level, want string }{
|
||||
{"2026 ERROR boom ERROR\n", "ERROR", "2026 " + red + "ERROR\x1b[0m boom ERROR\n"},
|
||||
{"[warning] disk\n", "WARN", "[" + yellow + "warning\x1b[0m] disk\n"},
|
||||
{"INFO: WARN disk\n", "warning", "INFO: " + yellow + "WARN\x1b[0m disk\n"},
|
||||
{"terror_x ERR:1\n", "ERROR", "terror_x " + red + "ERR\x1b[0m:1\n"},
|
||||
{"INFOS informal\n", "INFO", "INFOS informal\n"},
|
||||
{"ERROR without level\n", "", "ERROR without level\n"},
|
||||
|
||||
@@ -220,7 +220,7 @@ func proxySink(t *testing.T, proxies []any, overrides map[string]any) *HTTPSink
|
||||
|
||||
// Behind a proxy the sink serves the client library and the enabled pages
|
||||
// under /auth/, each with its type and the pages' CSP; the viewer learns a
|
||||
// custom status path from its meta tag.
|
||||
// custom status path and the level table from its meta tags.
|
||||
func TestProxyModeServesBrowserFiles(t *testing.T) {
|
||||
h := proxySink(t, []any{"127.0.0.1"}, map[string]any{"login_page": true, "viewer_page": true, "status_path": "/api/status"})
|
||||
if !slices.Contains(h.Capabilities(), core.CapProxyTLS) {
|
||||
@@ -245,6 +245,9 @@ func TestProxyModeServesBrowserFiles(t *testing.T) {
|
||||
if file == "view" && !bytes.Contains(body, []byte(`content="api/status"`)) {
|
||||
t.Error("the viewer page does not carry the custom status path")
|
||||
}
|
||||
if file == "view" && !bytes.Contains(body, []byte(`"WARNING"`)) {
|
||||
t.Error("the viewer page does not carry the level table")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"html"
|
||||
"net/http"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
"github.com/lixenwraith/logwisp/internal/authz"
|
||||
"github.com/lixenwraith/logwisp/internal/chain"
|
||||
"github.com/lixenwraith/logwisp/internal/config"
|
||||
"github.com/lixenwraith/logwisp/internal/core"
|
||||
"github.com/lixenwraith/logwisp/internal/netacl"
|
||||
)
|
||||
|
||||
@@ -49,7 +51,8 @@ var webRoutes = []struct {
|
||||
// webHandlers maps each GET path a browser uses to its handler. Without a
|
||||
// login (no auth, or mtls: the certificate is the login) the viewer is always
|
||||
// served; under scram the pages need proxy mode. The viewer learns the status
|
||||
// path and whether it logs in from meta tags, as the CSP allows no inline script.
|
||||
// path, whether it logs in and the level table from meta tags, as the CSP
|
||||
// allows no inline script.
|
||||
func webHandlers(o *config.HTTPSinkOptions, p *authz.Policy) (map[string]http.Handler, error) {
|
||||
open := !p.NeedsLogin()
|
||||
on := map[string]bool{"": open || p.BehindProxy(), "login": o.LoginPage, "view": open || o.ViewerPage}
|
||||
@@ -68,6 +71,8 @@ func webHandlers(o *config.HTTPSinkOptions, p *authz.Policy) (map[string]http.Ha
|
||||
if open {
|
||||
data = bytes.Replace(data, []byte(`name="logwisp-login" content="scram"`), []byte(`name="logwisp-login" content="none"`), 1)
|
||||
}
|
||||
levels, _ := json.Marshal(core.Levels)
|
||||
data = bytes.Replace(data, []byte(`name="logwisp-levels" content=""`), []byte(`name="logwisp-levels" content="`+html.EscapeString(string(levels))+`"`), 1)
|
||||
}
|
||||
handlers[chain.AuthPath+"/"+r.name] = serveWebFile(data, webTypes[path.Ext(r.file)])
|
||||
}
|
||||
|
||||
@@ -67,7 +67,8 @@ label {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
input {
|
||||
input,
|
||||
select {
|
||||
font: inherit;
|
||||
padding: 0.45rem 0.6rem;
|
||||
border: 1px solid var(--line);
|
||||
@@ -95,6 +96,7 @@ progress {
|
||||
|
||||
.viewer header {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
padding: 0.6rem 1rem;
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
<title>LogWisp stream</title>
|
||||
<meta name="logwisp-status" content="status">
|
||||
<meta name="logwisp-login" content="scram">
|
||||
<meta name="logwisp-levels" content="">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<link rel="icon" href="favicon.svg" type="image/svg+xml">
|
||||
<script type="module" src="view.js"></script>
|
||||
@@ -13,6 +14,8 @@
|
||||
<body class="viewer">
|
||||
<header>
|
||||
<h1>LogWisp</h1>
|
||||
<input id="search" type="search" placeholder="Search" aria-label="Show lines containing">
|
||||
<select id="level" aria-label="Lowest level shown"><option value="">All lines</option></select>
|
||||
<span id="state" role="status">connecting…</span>
|
||||
<button id="logout" type="button">Sign out</button>
|
||||
</header>
|
||||
|
||||
@@ -13,6 +13,13 @@ const signOut = document.getElementById("logout");
|
||||
const form = document.getElementById("login");
|
||||
const progress = document.getElementById("progress");
|
||||
const status = document.getElementById("status");
|
||||
const search = document.getElementById("search");
|
||||
const lowest = document.getElementById("level");
|
||||
// The server's levels by rising severity, each with the words naming it
|
||||
const levels = JSON.parse(meta("levels"));
|
||||
const named = new Map(levels.flatMap(({ level, names }) => names.map((name) => [name, level])));
|
||||
const rank = new Map(levels.map(({ level }, i) => [level, i]));
|
||||
for (const { level } of levels) lowest.add(new Option(`${level} and above`, level));
|
||||
|
||||
// A page without a login needs no WebCrypto, so plain http works too
|
||||
const unavailable = open ? "" : loginUnavailable();
|
||||
@@ -27,10 +34,35 @@ if (fresh) history.replaceState(null, "", location.pathname + location.search);
|
||||
let stopStream = null;
|
||||
let timer = null;
|
||||
|
||||
function append(line) {
|
||||
// An entry's level is its first word naming one, as the server reads a line
|
||||
function levelOf(text) {
|
||||
for (const [word] of text.matchAll(/\w+/g)) {
|
||||
const level = named.get(word.toUpperCase());
|
||||
if (level) return level;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// A line shows when it contains the search, in any case, and its entry's level
|
||||
// reaches the lowest chosen; with none chosen, lines without a level show too
|
||||
function shown(row) {
|
||||
const floor = rank.get(lowest.value);
|
||||
if (floor !== undefined && !(rank.get(row.dataset.level) >= floor)) return false;
|
||||
const text = search.value.trim().toLowerCase();
|
||||
return !text || row.textContent.toLowerCase().includes(text);
|
||||
}
|
||||
|
||||
function refilter() {
|
||||
for (const row of log.children) row.hidden = !shown(row);
|
||||
log.scrollTop = log.scrollHeight;
|
||||
}
|
||||
|
||||
function append(line, level) {
|
||||
const atBottom = log.scrollTop + log.clientHeight >= log.scrollHeight - 4;
|
||||
const row = document.createElement("div");
|
||||
row.textContent = line;
|
||||
row.dataset.level = level;
|
||||
row.hidden = !shown(row);
|
||||
log.append(row);
|
||||
while (log.childElementCount > MAX_LINES) log.firstElementChild.remove();
|
||||
if (atBottom) log.scrollTop = log.scrollHeight;
|
||||
@@ -73,7 +105,10 @@ function onEvent(type, data) {
|
||||
stopStream();
|
||||
retry("server shut down");
|
||||
}
|
||||
if (type === "message") data.split("\n").forEach(append);
|
||||
if (type === "message") {
|
||||
const level = levelOf(data);
|
||||
for (const line of data.split("\n")) append(line, level);
|
||||
}
|
||||
}
|
||||
|
||||
function streamWithCookie(url) {
|
||||
@@ -129,6 +164,9 @@ function retry(reason) {
|
||||
timer = setTimeout(connect, 5000);
|
||||
}
|
||||
|
||||
search.addEventListener("input", refilter);
|
||||
lowest.addEventListener("change", refilter);
|
||||
|
||||
form.addEventListener("submit", async (event) => {
|
||||
event.preventDefault();
|
||||
const fields = new FormData(form);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package source
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/lixenwraith/logwisp/internal/core"
|
||||
@@ -36,27 +35,8 @@ type SourceStats struct {
|
||||
Details map[string]any
|
||||
}
|
||||
|
||||
// ExtractLogLevel heuristically determines the log level from a line of text
|
||||
// ExtractLogLevel returns the level the first word naming one marks, or ""
|
||||
func ExtractLogLevel(line string) string {
|
||||
patterns := []struct {
|
||||
patterns []string
|
||||
level string
|
||||
}{
|
||||
{[]string{"[ERROR]", "ERROR:", " ERROR ", "ERR:", "[ERR]", "FATAL:", "[FATAL]"}, "ERROR"},
|
||||
{[]string{"[WARN]", "WARN:", " WARN ", "WARNING:", "[WARNING]"}, "WARN"},
|
||||
{[]string{"[INFO]", "INFO:", " INFO ", "[INF]", "INF:"}, "INFO"},
|
||||
{[]string{"[DEBUG]", "DEBUG:", " DEBUG ", "[DBG]", "DBG:"}, "DEBUG"},
|
||||
{[]string{"[TRACE]", "TRACE:", " TRACE "}, "TRACE"},
|
||||
}
|
||||
|
||||
upperLine := strings.ToUpper(line)
|
||||
for _, group := range patterns {
|
||||
for _, pattern := range group.patterns {
|
||||
if strings.Contains(upperLine, pattern) {
|
||||
return group.level
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
level, _, _ := core.LevelWord(line, "")
|
||||
return level
|
||||
}
|
||||
Reference in new issue
Block a user