Skip to content

Commit bdf4950

Browse files
authored
Fix missing trailing newline for multiline message (#16)
Previously, messages containing a newline were being ignored by journald.
1 parent 372cb9d commit bdf4950

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

journal.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ func (h *Handler) appendKV(b []byte, k string, v []byte) []byte {
206206
b = append(b, '\n')
207207
b = binary.LittleEndian.AppendUint64(b, uint64(len(v)))
208208
b = append(b, v...)
209+
b = append(b, '\n')
209210
} else {
210211
b = append(b, k...)
211212
b = append(b, '=')

journal_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,30 @@ func TestReplaceGroup(t *testing.T) {
203203
}
204204
}
205205

206+
func TestMessagesWithNewlines(t *testing.T) {
207+
buf := new(bytes.Buffer)
208+
handler, err := NewHandler(nil)
209+
if err != nil {
210+
t.Fatal(err)
211+
}
212+
handler.w = buf
213+
214+
record := slog.NewRecord(time.Now(), slog.LevelInfo, "Line 1\nLine 2\nLine 3", 0)
215+
record.AddAttrs(slog.Attr{Key: "MULTILINE_ATTR", Value: slog.StringValue("value1\nvalue2")})
216+
217+
_ = handler.Handle(context.TODO(), record)
218+
kv, err := deserializeKeyValue(buf)
219+
if err != nil {
220+
t.Fatal(err)
221+
}
222+
if kv["MESSAGE"] != "Line 1\nLine 2\nLine 3" {
223+
t.Errorf("Expected multiline message, got: %q", kv["MESSAGE"])
224+
}
225+
if kv["MULTILINE_ATTR"] != "value1\nvalue2" {
226+
t.Errorf("Expected multiline attribute, got: %q", kv["MULTILINE_ATTR"])
227+
}
228+
}
229+
206230
func createNestedMap(m map[string]any, keys []string, value any) {
207231
for i, key := range keys {
208232
if i == len(keys)-1 {

0 commit comments

Comments
 (0)