mirror of
https://github.com/rclone/rclone.git
synced 2025-12-11 22:14:05 +01:00
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:
@@ -310,6 +310,10 @@ func (h *OutputHandler) jsonLog(ctx context.Context, buf *bytes.Buffer, r slog.R
|
|||||||
r.AddAttrs(
|
r.AddAttrs(
|
||||||
slog.String("source", getCaller(2)),
|
slog.String("source", getCaller(2)),
|
||||||
)
|
)
|
||||||
|
// Add PID if requested
|
||||||
|
if h.format&logFormatPid != 0 {
|
||||||
|
r.AddAttrs(slog.Int("pid", os.Getpid()))
|
||||||
|
}
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
err = h.jsonHandler.Handle(ctx, r)
|
err = h.jsonHandler.Handle(ctx, r)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|||||||
@@ -198,6 +198,17 @@ func TestAddOutputUseJSONLog(t *testing.T) {
|
|||||||
assert.Equal(t, "2020/01/02 03:04:05 INFO : world\n", extraText)
|
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.
|
// Test WithAttrs and WithGroup return new handlers with same settings.
|
||||||
func TestWithAttrsAndGroup(t *testing.T) {
|
func TestWithAttrsAndGroup(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
|
|||||||
Reference in New Issue
Block a user