瀏覽代碼

Improve logging

Brendan Abolivier 7 年之前
父節點
當前提交
a32c62d5ed
簽署人: Brendan Abolivier <contact@brendanabolivier.com> GPG 金鑰 ID: 8EF1500759F70623
共有 1 個檔案被更改,包括 12 行新增5 行删除
  1. 12
    5
      src/metrics-alerting/alert/alert.go

+ 12
- 5
src/metrics-alerting/alert/alert.go 查看文件

@@ -21,11 +21,7 @@ func (a *Alerter) Alert(
21 21
 	labels map[string]string,
22 22
 	data script_data.Data,
23 23
 ) error {
24
-	alertLog := fmt.Sprintf("Test for script \"%s\" failed", script.Key)
25
-	if len(data.Key) > 0 {
26
-		alertLog = alertLog + fmt.Sprintf(" (data: %s=%s)", data.Key, data.Value)
27
-	}
28
-	logrus.Info(alertLog)
24
+	logFailure(script, data)
29 25
 
30 26
 	switch script.Action {
31 27
 	case "http":
@@ -36,3 +32,14 @@ func (a *Alerter) Alert(
36 32
 		return fmt.Errorf("invalid action type: %s", script.Action)
37 33
 	}
38 34
 }
35
+
36
+func logFailure(script config.Script, data script_data.Data) {
37
+	var entry *logrus.Entry
38
+	if len(data.Key) > 0 {
39
+		entry = logrus.WithField(data.Key, data.Value)
40
+	} else {
41
+		entry = logrus.NewEntry(logrus.New())
42
+	}
43
+
44
+	entry.Infof("Test for script \"%s\" failed", script.Key)
45
+}