From b9586c3e0392ec67e0f7ccc30ec8e1a6ddc7ba6f Mon Sep 17 00:00:00 2001 From: Tingsong Xu Date: Mon, 8 Dec 2025 14:05:19 +0800 Subject: [PATCH] fs/log: fix PID not included in JSON log output When using `--log-format pid,json`, the PID was not being added to the JSON log output. This fix adds PID support to JSON logging. --- fs/log/slog.go | 4 ++++ fs/log/slog_test.go | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/fs/log/slog.go b/fs/log/slog.go index 0efbcbee6..36592c87f 100644 --- a/fs/log/slog.go +++ b/fs/log/slog.go @@ -310,6 +310,10 @@ func (h *OutputHandler) jsonLog(ctx context.Context, buf *bytes.Buffer, r slog.R r.AddAttrs( slog.String("source", getCaller(2)), ) + // Add PID if requested + if h.format&logFormatPid != 0 { + r.AddAttrs(slog.Int("pid", os.Getpid())) + } h.mu.Lock() err = h.jsonHandler.Handle(ctx, r) if err == nil { diff --git a/fs/log/slog_test.go b/fs/log/slog_test.go index 5cfa52131..e0389e3e4 100644 --- a/fs/log/slog_test.go +++ b/fs/log/slog_test.go @@ -198,6 +198,17 @@ func TestAddOutputUseJSONLog(t *testing.T) { assert.Equal(t, "2020/01/02 03:04:05 INFO : world\n", extraText) } +// Test JSON log includes PID when logFormatPid is set. +func TestJSONLogWithPid(t *testing.T) { + buf := &bytes.Buffer{} + h := NewOutputHandler(buf, nil, logFormatJSON|logFormatPid) + + r := slog.NewRecord(t0, slog.LevelInfo, "hello", 0) + require.NoError(t, h.Handle(context.Background(), r)) + output := buf.String() + assert.Contains(t, output, fmt.Sprintf(`"pid":%d`, os.Getpid())) +} + // Test WithAttrs and WithGroup return new handlers with same settings. func TestWithAttrsAndGroup(t *testing.T) { buf := &bytes.Buffer{}