cmd: clean up usage of contextKeyLog through getter and setter functions

This commit is contained in:
Christian Schwarz 2018-08-26 14:58:57 +02:00
parent 666ead2646
commit f6be5b776b
7 changed files with 14 additions and 12 deletions

View File

@ -46,7 +46,7 @@ const (
func (j *ControlJob) JobStart(ctx context.Context) {
log := ctx.Value(contextKeyLog).(Logger)
log := getLogger(ctx)
defer log.Info("control job finished")
daemon := ctx.Value(contextKeyDaemon).(*Daemon)

View File

@ -84,7 +84,7 @@ func (j *LocalJob) JobType() JobType { return JobTypeLocal }
func (j *LocalJob) JobStart(ctx context.Context) {
rootLog := ctx.Value(contextKeyLog).(Logger)
rootLog := getLogger(ctx)
j.snapperTask = NewTask("snapshot", j, rootLog)
j.mainTask = NewTask("main", j, rootLog)

View File

@ -69,7 +69,7 @@ func (j *PrometheusJob) JobStart(ctx context.Context) {
panic(err)
}
log := ctx.Value(contextKeyLog).(Logger)
log := getLogger(ctx)
task := NewTask("main", j, log)
log = task.Log()

View File

@ -105,7 +105,7 @@ func (j *PullJob) JobType() JobType { return JobTypePull }
func (j *PullJob) JobStart(ctx context.Context) {
log := ctx.Value(contextKeyLog).(Logger)
log := getLogger(ctx)
defer log.Info("exiting")
j.task = NewTask("main", j, log)

View File

@ -89,7 +89,7 @@ func (j *SourceJob) JobType() JobType { return JobTypeSource }
func (j *SourceJob) JobStart(ctx context.Context) {
log := ctx.Value(contextKeyLog).(Logger)
log := getLogger(ctx)
defer log.Info("exiting")
j.autosnapTask = NewTask("autosnap", j, log)
@ -196,7 +196,6 @@ outer:
}
return
}
func (j *SourceJob) handleConnection(conn net.Conn, task *Task) {
@ -209,7 +208,7 @@ func (j *SourceJob) handleConnection(conn net.Conn, task *Task) {
senderEP := endpoint.NewSender(j.Filesystems, NewPrefixFilter(j.SnapshotPrefix))
ctx := context.Background()
ctx = context.WithValue(ctx, contextKeyLog, task.Log().WithField(logSubsysField, "rpc.endpoint"))
ctx = endpoint.WithLogger(ctx, task.Log().WithField(logSubsysField, "rpc.endpoint"))
ctx = streamrpc.ContextWithLogger(ctx, streamrpcLogAdaptor{task.Log().WithField(logSubsysField, "rpc.protocol")})
handler := endpoint.NewHandler(senderEP)
if err := streamrpc.ServeConn(ctx, conn, STREAMRPC_CONFIG, handler.Handle); err != nil {

View File

@ -73,8 +73,7 @@ func doDaemon(cmd *cobra.Command, args []string) {
log.Info(NewZreplVersionInformation().String())
log.Debug("starting daemon")
ctx := context.WithValue(context.Background(), contextKeyLog, log)
ctx = context.WithValue(ctx, contextKeyLog, log)
ctx := WithLogger(context.Background(), log)
d := NewDaemon(conf)
d.Loop(ctx)
@ -92,6 +91,10 @@ func getLogger(ctx context.Context) Logger {
return ctx.Value(contextKeyLog).(Logger)
}
func WithLogger(ctx context.Context, l Logger) context.Context {
return context.WithValue(ctx, contextKeyLog, l)
}
type Daemon struct {
conf *Config
startedAt time.Time
@ -105,7 +108,7 @@ func (d *Daemon) Loop(ctx context.Context) {
d.startedAt = time.Now()
log := ctx.Value(contextKeyLog).(Logger)
log := getLogger(ctx)
ctx, cancel := context.WithCancel(ctx)
ctx = context.WithValue(ctx, contextKeyDaemon, d)
@ -121,7 +124,7 @@ func (d *Daemon) Loop(ctx context.Context) {
logger := log.WithField(logJobField, job.JobName())
logger.Info("starting")
i++
jobCtx := context.WithValue(ctx, contextKeyLog, logger)
jobCtx := WithLogger(ctx, logger)
go func(j Job) {
j.JobStart(jobCtx)
finishs <- j

View File

@ -179,7 +179,7 @@ func doTestPrunePolicy(cmd *cobra.Command, args []string) {
log.Printf("start pruning")
ctx := context.WithValue(context.Background(), contextKeyLog, log)
ctx := WithLogger(context.Background(), log)
result, err := pruner.Run(ctx)
if err != nil {
log.Printf("error running pruner: %s", err)