about summary refs log tree commit diff
path: root/tools/nixery/server/builder/cache.go
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-10-06T13·48+0100
committerVincent Ambo <github@tazj.in>2019-10-06T22·05+0100
commitd7ffbbdea47738acac24593f7e4448dd9c1df8ff (patch)
treef01fe2b9b0e491d6dbae84152a950df1414812d7 /tools/nixery/server/builder/cache.go
parentc1020754a2a2a4058fd70d4b0a8276ccadd9545f (diff)
refactor(server): Use logrus convenience functions for logs
Makes use of the `.WithError` and `.WithField` convenience functions
in logrus to simplify log statement construction.

This has the added benefit of making it easier to correctly log
errors.
Diffstat (limited to 'tools/nixery/server/builder/cache.go')
-rw-r--r--tools/nixery/server/builder/cache.go76
1 files changed, 25 insertions, 51 deletions
diff --git a/tools/nixery/server/builder/cache.go b/tools/nixery/server/builder/cache.go
index 916de3af168c..88bf30de4812 100644
--- a/tools/nixery/server/builder/cache.go
+++ b/tools/nixery/server/builder/cache.go
@@ -63,10 +63,8 @@ func (c *LocalCache) manifestFromLocalCache(key string) (json.RawMessage, bool)
 		// This is a debug log statement because failure to
 		// read the manifest key is currently expected if it
 		// is not cached.
-		log.WithFields(log.Fields{
-			"manifest": key,
-			"error":    err,
-		}).Debug("failed to read manifest from local cache")
+		log.WithError(err).WithField("manifest", key).
+			Debug("failed to read manifest from local cache")
 
 		return nil, false
 	}
@@ -74,10 +72,8 @@ func (c *LocalCache) manifestFromLocalCache(key string) (json.RawMessage, bool)
 
 	m, err := ioutil.ReadAll(f)
 	if err != nil {
-		log.WithFields(log.Fields{
-			"manifest": key,
-			"error":    err,
-		}).Error("failed to read manifest from local cache")
+		log.WithError(err).WithField("manifest", key).
+			Error("failed to read manifest from local cache")
 
 		return nil, false
 	}
@@ -96,10 +92,8 @@ func (c *LocalCache) localCacheManifest(key string, m json.RawMessage) {
 
 	err := ioutil.WriteFile(c.mdir+key, []byte(m), 0644)
 	if err != nil {
-		log.WithFields(log.Fields{
-			"manifest": key,
-			"error":    err,
-		}).Error("failed to locally cache manifest")
+		log.WithError(err).WithField("manifest", key).
+			Error("failed to locally cache manifest")
 	}
 }
 
@@ -136,10 +130,8 @@ func manifestFromCache(ctx context.Context, s *State, key string) (json.RawMessa
 
 	r, err := obj.NewReader(ctx)
 	if err != nil {
-		log.WithFields(log.Fields{
-			"manifest": key,
-			"error":    err,
-		}).Error("failed to retrieve manifest from bucket cache")
+		log.WithError(err).WithField("manifest", key).
+			Error("failed to retrieve manifest from bucket cache")
 
 		return nil, false
 	}
@@ -147,18 +139,14 @@ func manifestFromCache(ctx context.Context, s *State, key string) (json.RawMessa
 
 	m, err := ioutil.ReadAll(r)
 	if err != nil {
-		log.WithFields(log.Fields{
-			"manifest": key,
-			"error":    err,
-		}).Error("failed to read cached manifest from bucket")
+		log.WithError(err).WithField("manifest", key).
+			Error("failed to read cached manifest from bucket")
 
 		return nil, false
 	}
 
 	go s.Cache.localCacheManifest(key, m)
-	log.WithFields(log.Fields{
-		"manifest": key,
-	}).Info("retrieved manifest from GCS")
+	log.WithField("manifest", key).Info("retrieved manifest from GCS")
 
 	return json.RawMessage(m), true
 }
@@ -173,19 +161,15 @@ func cacheManifest(ctx context.Context, s *State, key string, m json.RawMessage)
 
 	size, err := io.Copy(w, r)
 	if err != nil {
-		log.WithFields(log.Fields{
-			"manifest": key,
-			"error":    err,
-		}).Error("failed to cache manifest to GCS")
+		log.WithError(err).WithField("manifest", key).
+			Error("failed to cache manifest to GCS")
 
 		return
 	}
 
 	if err = w.Close(); err != nil {
-		log.WithFields(log.Fields{
-			"manifest": key,
-			"error":    err,
-		}).Error("failed to cache manifest to GCS")
+		log.WithError(err).WithField("manifest", key).
+			Error("failed to cache manifest to GCS")
 
 		return
 	}
@@ -211,10 +195,8 @@ func layerFromCache(ctx context.Context, s *State, key string) (*manifest.Entry,
 
 	r, err := obj.NewReader(ctx)
 	if err != nil {
-		log.WithFields(log.Fields{
-			"layer": key,
-			"error": err,
-		}).Error("failed to retrieve cached layer from GCS")
+		log.WithError(err).WithField("layer", key).
+			Error("failed to retrieve cached layer from GCS")
 
 		return nil, false
 	}
@@ -223,10 +205,8 @@ func layerFromCache(ctx context.Context, s *State, key string) (*manifest.Entry,
 	jb := bytes.NewBuffer([]byte{})
 	_, err = io.Copy(jb, r)
 	if err != nil {
-		log.WithFields(log.Fields{
-			"layer": key,
-			"error": err,
-		}).Error("failed to read cached layer from GCS")
+		log.WithError(err).WithField("layer", key).
+			Error("failed to read cached layer from GCS")
 
 		return nil, false
 	}
@@ -234,10 +214,8 @@ func layerFromCache(ctx context.Context, s *State, key string) (*manifest.Entry,
 	var entry manifest.Entry
 	err = json.Unmarshal(jb.Bytes(), &entry)
 	if err != nil {
-		log.WithFields(log.Fields{
-			"layer": key,
-			"error": err,
-		}).Error("failed to unmarshal cached layer")
+		log.WithError(err).WithField("layer", key).
+			Error("failed to unmarshal cached layer")
 
 		return nil, false
 	}
@@ -257,19 +235,15 @@ func cacheLayer(ctx context.Context, s *State, key string, entry manifest.Entry)
 
 	_, err := io.Copy(w, bytes.NewReader(j))
 	if err != nil {
-		log.WithFields(log.Fields{
-			"layer": key,
-			"error": err,
-		}).Error("failed to cache layer")
+		log.WithError(err).WithField("layer", key).
+			Error("failed to cache layer")
 
 		return
 	}
 
 	if err = w.Close(); err != nil {
-		log.WithFields(log.Fields{
-			"layer": key,
-			"error": err,
-		}).Error("failed to cache layer")
+		log.WithError(err).WithField("layer", key).
+			Error("failed to cache layer")
 
 		return
 	}