diff --git a/internal/orchestrator/repo/logging.go b/internal/orchestrator/repo/logging.go index ade3d435..4c0106ff 100644 --- a/internal/orchestrator/repo/logging.go +++ b/internal/orchestrator/repo/logging.go @@ -1,8 +1,9 @@ package repo import ( + "bufio" "context" - "fmt" + "io" "github.com/garethgeorge/backrest/internal/ioutil" "github.com/garethgeorge/backrest/internal/orchestrator/logging" @@ -12,16 +13,29 @@ import ( // pipeResticLogsToWriter sets the restic logger to write to the provided writer. // returns a new context with the logger set and a function to flush the logs. func forwardResticLogs(ctx context.Context) (context.Context, func()) { - writer := logging.WriterFromContext(ctx) - if writer == nil { + if logging.WriterFromContext(ctx) == nil { return ctx, func() {} } - limitWriter := &ioutil.LimitWriter{W: writer, N: 64 * 1024} - prefixWriter := &ioutil.LinePrefixer{W: limitWriter, Prefix: []byte("[restic] ")} - return restic.ContextWithLogger(ctx, prefixWriter), func() { - if limitWriter.D > 0 { - fmt.Fprintf(prefixWriter, "... Output truncated, %d bytes dropped\n", limitWriter.D) + logger := logging.Logger(ctx, "[restic] ") + + pr, pw := io.Pipe() + + go func() { + scanner := bufio.NewScanner(pr) + for scanner.Scan() { + logger.Sugar().Infof("%s", scanner.Text()) } - prefixWriter.Close() + if err := scanner.Err(); err != nil { + logger.Sugar().Errorf("Error reading restic logs: %v", err) + } + pr.Close() + }() + + limitWriter := &ioutil.LimitWriter{W: pw, N: 64 * 1024} + return restic.ContextWithLogger(ctx, limitWriter), func() { + if limitWriter.D > 0 { + logger.Sugar().Warnf("... Output truncated, %d bytes dropped\n", limitWriter.D) + } + pw.Close() } } diff --git a/pkg/restic/restic.go b/pkg/restic/restic.go index deb5f511..6bfbefe8 100644 --- a/pkg/restic/restic.go +++ b/pkg/restic/restic.go @@ -71,7 +71,7 @@ func (r *Repo) commandWithContext(ctx context.Context, args []string, opts ...Ge sw := &ioutil.SynchronizedWriter{W: logger} cmd.Stderr = sw cmd.Stdout = sw - fmt.Fprintf(logger, "\ncommand: %q\n", fullCmd) + fmt.Fprintf(logger, "command: %q\n", fullCmd) } return cmd