mirror of
https://github.com/garethgeorge/backrest.git
synced 2026-08-26 10:56:33 +00:00
fix: use zap logger for restic log formatting
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user