Files
Zakk 15c727e1e1 fix(log): show the log in the web viewer without --log-file, keep the footer on screen
Without --log-file the viewer said "log printed to console, please see log in
console", which the Windows service, the launchd job and every "log to
console" setup cannot do. The logger now also keeps its last 4 MiB in memory
and the viewer reads that tail when there is no file; the lines carry the
same timestamp and level prefix as the file adapter's.

The log area also gave way to nothing: at 50vh a short window pushed the
filters and the export button below the fold with only a scrollbar to hint at
them. It now shrinks so the footer stays visible.
2026-09-18 15:29:18 +10:00

59 lines
1.7 KiB
Go

package log
import (
"bytes"
"strings"
"testing"
"time"
"github.com/v2rayA/beego/v2/logs"
)
func newMsg(text string) *logs.LogMsg {
return &logs.LogMsg{Level: logs.LevelInfo, Msg: text, When: time.Date(2026, 9, 18, 12, 0, 0, 0, time.UTC)}
}
func TestMemoryLogKeepsTailAndOffsets(t *testing.T) {
w := &memoryWriter{max: 64}
for i := 0; i < 5; i++ {
_ = w.WriteMsg(newMsg(strings.Repeat("x", 10)))
}
if len(w.buf) > w.max {
t.Fatalf("buffer holds %d bytes, cap is %d", len(w.buf), w.max)
}
if w.base == 0 {
t.Fatal("nothing was evicted although the cap was exceeded")
}
if !bytes.HasSuffix(w.buf, []byte("\n")) || bytes.IndexByte(w.buf, '2') != 0 {
t.Fatalf("eviction must cut on line boundaries, got %q", w.buf)
}
// A reader that is behind the oldest line gets the oldest line onward.
old := memory
memory = w
defer func() { memory = old }()
data, start := ReadMemory(0)
if start != w.base || !bytes.Equal(data, w.buf) {
t.Fatalf("read from 0: start=%d base=%d", start, w.base)
}
// A reader at the end gets nothing new.
data, start = ReadMemory(w.base + int64(len(w.buf)))
if len(data) != 0 {
t.Fatalf("read at end returned %d bytes", len(data))
}
// A reader in the middle gets exactly the remainder.
mid := w.base + 5
data, start = ReadMemory(mid)
if start != mid || !bytes.Equal(data, w.buf[5:]) {
t.Fatalf("read from the middle returned start=%d len=%d", start, len(data))
}
}
func TestMemoryLogLineShapeMatchesFile(t *testing.T) {
w := &memoryWriter{max: 1 << 10}
_ = w.WriteMsg(newMsg("hello"))
line := string(w.buf)
if !strings.HasPrefix(line, "2026/09/18 12:00:00.000 ") || !strings.Contains(line, "[I]") || !strings.HasSuffix(line, "hello\n") {
t.Fatalf("unexpected line %q", line)
}
}