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.
This commit is contained in:
Tingsong Xu
2025-12-08 14:05:19 +08:00
committed by dougal
parent 0dc0ab1330
commit b9586c3e03
2 changed files with 15 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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{}