v0.22.1 cli flag and behavior, and test output improvement

This commit is contained in:
lixen committed 2026-10-03 21:57:41 -04:00
1 parent 71d1b82f83
commit 4eba6fc834
89 files changed
+1531 -661

No files matched your search

+12 -6
View File
@@ -6,8 +6,8 @@ import (
"fmt"
"sync"
"logwisp/internal/config"
"logwisp/internal/core"
"github.com/lixenwraith/logwisp/internal/config"
"github.com/lixenwraith/logwisp/internal/core"
lconfig "github.com/lixenwraith/config"
"github.com/lixenwraith/log/formatter"
@@ -98,15 +98,21 @@ func (a *FormatterAdapter) FormatWithFlags(entry core.LogEntry, customFlags int6
return a.serialize(entry, customFlags), nil
}
// serialize renders an entry under the given flags. The returned slice is a
// copy: the underlying formatter reuses one buffer and sinks retain payloads.
// serialize renders an entry under the given flags as one record ending in
// one newline, which raw output lacks unless the message carried it. The
// result is a copy: the formatter reuses one buffer and sinks keep payloads.
func (a *FormatterAdapter) serialize(entry core.LogEntry, flags int64) []byte {
args, flags := formatArgs(entry, flags)
a.mu.Lock()
out := bytes.Clone(a.formatter.Format(flags, entry.Time, mapLevel(entry.Level), sourceLabel(entry), args))
out := a.formatter.Format(flags, entry.Time, mapLevel(entry.Level), sourceLabel(entry), args)
record := make([]byte, len(out), len(out)+1)
copy(record, out)
a.mu.Unlock()
return out
if !bytes.HasSuffix(record, []byte{'\n'}) {
record = append(record, '\n')
}
return record
}
// formatArgs pairs the entry with its flags. FlagRaw keeps the fields JSON
+34
View File
@@ -0,0 +1,34 @@
package format
import (
"bytes"
"testing"
"time"
"github.com/lixenwraith/logwisp/internal/config"
"github.com/lixenwraith/logwisp/internal/core"
)
// Every payload is one record ending in exactly one newline, whatever the
// type and whether or not the message carried its own.
func TestEveryRecordEndsInOneNewline(t *testing.T) {
entries := []core.LogEntry{
{Message: "plain"},
{Message: "carried\n"},
{Fields: []byte(`{"k":1}`)},
{Message: "with", Fields: []byte(`{"k":1}`)},
}
for _, typ := range []string{"raw", "txt", "json"} {
f, err := NewFormatter(&config.FormatConfig{Type: typ})
if err != nil {
t.Fatal(err)
}
for _, e := range entries {
e.Time = time.Now()
out, _ := f.Format(e)
if !bytes.HasSuffix(out, []byte("\n")) || bytes.HasSuffix(out, []byte("\n\n")) {
t.Errorf("%s %+v: %q", typ, e, out)
}
}
}
}
+2 -2
View File
@@ -1,8 +1,8 @@
package format
import (
"logwisp/internal/config"
"logwisp/internal/core"
"github.com/lixenwraith/logwisp/internal/config"
"github.com/lixenwraith/logwisp/internal/core"
)
// Formatter defines the interface for transforming a LogEntry into a byte slice