cmd: logging using logrus
This commit is contained in:
+23
-17
@@ -3,7 +3,6 @@ package cmd
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zrepl/zrepl/util"
|
||||
"github.com/zrepl/zrepl/zfs"
|
||||
"sort"
|
||||
"time"
|
||||
@@ -31,11 +30,11 @@ func (a *IntervalAutosnap) Run(ctx context.Context, didSnaps chan struct{}) {
|
||||
|
||||
ds, err := zfs.ZFSListMapping(a.DatasetFilter)
|
||||
if err != nil {
|
||||
a.log.Printf("error listing datasets: %s", err)
|
||||
a.log.WithError(err).Error("cannot list datasets")
|
||||
return
|
||||
}
|
||||
if len(ds) == 0 {
|
||||
a.log.Printf("no datasets matching dataset filter")
|
||||
a.log.WithError(err).Error("no datasets matching dataset filter")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -43,18 +42,18 @@ func (a *IntervalAutosnap) Run(ctx context.Context, didSnaps chan struct{}) {
|
||||
|
||||
now := time.Now()
|
||||
|
||||
a.log.Printf("examining filesystem state")
|
||||
a.log.Debug("examine filesystem state")
|
||||
for i, d := range ds {
|
||||
|
||||
l := util.NewPrefixLogger(a.log, d.ToString())
|
||||
l := a.log.WithField("filesystem", d.ToString())
|
||||
|
||||
fsvs, err := zfs.ZFSListFilesystemVersions(d, &PrefixSnapshotFilter{a.Prefix})
|
||||
if err != nil {
|
||||
l.Printf("error listing filesystem versions of %s")
|
||||
l.WithError(err).Error("cannot list filesystem versions")
|
||||
continue
|
||||
}
|
||||
if len(fsvs) <= 0 {
|
||||
l.Printf("no filesystem versions with prefix '%s'", a.Prefix)
|
||||
l.WithField("prefix", a.Prefix).Info("no filesystem versions with prefix")
|
||||
a.snaptimes[i] = snapTime{d, now}
|
||||
continue
|
||||
}
|
||||
@@ -65,11 +64,14 @@ func (a *IntervalAutosnap) Run(ctx context.Context, didSnaps chan struct{}) {
|
||||
})
|
||||
|
||||
latest := fsvs[len(fsvs)-1]
|
||||
l.Printf("latest snapshot at %s (%s old)", latest.Creation.Format(LOG_TIME_FMT), now.Sub(latest.Creation))
|
||||
l.WithField("creation", latest.Creation).
|
||||
Debug("found latest snapshot")
|
||||
|
||||
since := now.Sub(latest.Creation)
|
||||
if since < 0 {
|
||||
l.Printf("error: snapshot is from future (created at %s)", latest.Creation.Format(LOG_TIME_FMT))
|
||||
l.WithField("snapshot", latest.Name).
|
||||
WithField("creation", latest.Creation).
|
||||
Error("snapshot is from the future")
|
||||
continue
|
||||
}
|
||||
next := now
|
||||
@@ -84,15 +86,16 @@ func (a *IntervalAutosnap) Run(ctx context.Context, didSnaps chan struct{}) {
|
||||
})
|
||||
|
||||
syncPoint := a.snaptimes[0]
|
||||
a.log.Printf("sync point at %s (in %s)", syncPoint.time.Format(LOG_TIME_FMT), syncPoint.time.Sub(now))
|
||||
a.log.WithField("sync_point", syncPoint.time.Format(LOG_TIME_FMT)).
|
||||
Info("wait for sync point")
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
a.log.Printf("context: %s", ctx.Err())
|
||||
a.log.WithError(ctx.Err()).Info("context done")
|
||||
return
|
||||
|
||||
case <-time.After(syncPoint.time.Sub(now)):
|
||||
a.log.Printf("snapshotting all filesystems to enable further snaps in lockstep")
|
||||
a.log.Debug("snapshot all filesystems to enable further snaps in lockstep")
|
||||
a.doSnapshots(didSnaps)
|
||||
}
|
||||
|
||||
@@ -102,7 +105,7 @@ func (a *IntervalAutosnap) Run(ctx context.Context, didSnaps chan struct{}) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
ticker.Stop()
|
||||
a.log.Printf("context: %s", ctx.Err())
|
||||
a.log.WithError(ctx.Err()).Info("context done")
|
||||
return
|
||||
|
||||
case <-ticker.C:
|
||||
@@ -117,7 +120,7 @@ func (a *IntervalAutosnap) doSnapshots(didSnaps chan struct{}) {
|
||||
// fetch new dataset list in case user added new dataset
|
||||
ds, err := zfs.ZFSListMapping(a.DatasetFilter)
|
||||
if err != nil {
|
||||
a.log.Printf("error listing datasets: %s", err)
|
||||
a.log.WithError(err).Error("cannot list datasets")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -126,17 +129,20 @@ func (a *IntervalAutosnap) doSnapshots(didSnaps chan struct{}) {
|
||||
suffix := time.Now().In(time.UTC).Format("20060102_150405_000")
|
||||
snapname := fmt.Sprintf("%s%s", a.Prefix, suffix)
|
||||
|
||||
a.log.Printf("snapshotting %s@%s", d.ToString(), snapname)
|
||||
a.log.WithField("filesystem", d.ToString()).
|
||||
WithField("snapname", snapname).
|
||||
Info("create snapshot")
|
||||
|
||||
err := zfs.ZFSSnapshot(d, snapname, false)
|
||||
if err != nil {
|
||||
a.log.Printf("error snapshotting %s: %s", d.ToString(), err)
|
||||
a.log.WithError(err).Error("cannot create snapshot")
|
||||
}
|
||||
}
|
||||
|
||||
select {
|
||||
case didSnaps <- struct{}{}:
|
||||
default:
|
||||
a.log.Printf("warning: callback channel is full, discarding")
|
||||
a.log.Warn("warning: callback channel is full, discarding")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user