Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

fs/log: fix PID not included in JSON log output

Tingsong Xub9586c3e0392
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{}