about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-10-05T21·48+0100
committerVincent Ambo <github@tazj.in>2019-10-06T22·05+0100
commitf77c93b6aeb3e847fe00099ea5c52dc98cf74b4d (patch)
tree15114dc7666afa10aa49b2f260d5bfee7515c54f
parent6912658c72291caefd7c7ea6312a35c3a686cf61 (diff)
feat(server): Add log level to severity mapping
The output format now writes a `severity` field that follows that
format that should be recognised by Stackdriver Logging.
-rw-r--r--tools/nixery/server/logs.go34
-rw-r--r--tools/nixery/server/main.go5
2 files changed, 36 insertions, 3 deletions
diff --git a/tools/nixery/server/logs.go b/tools/nixery/server/logs.go
index 9d1f17aed5cf..dec4a410fb06 100644
--- a/tools/nixery/server/logs.go
+++ b/tools/nixery/server/logs.go
@@ -40,16 +40,46 @@ func isError(e *log.Entry) bool {
 		e.HasCaller()
 }
 
+// logSeverity formats the entry's severity into a format compatible
+// with Stackdriver Logging.
+//
+// The two formats that are being mapped do not have an equivalent set
+// of severities/levels, so the mapping is somewhat arbitrary for a
+// handful of them.
+//
+// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
+func logSeverity(l log.Level) string {
+	switch l {
+	case log.TraceLevel:
+		return "DEBUG"
+	case log.DebugLevel:
+		return "DEBUG"
+	case log.InfoLevel:
+		return "INFO"
+	case log.WarnLevel:
+		return "WARNING"
+	case log.ErrorLevel:
+		return "ERROR"
+	case log.FatalLevel:
+		return "CRITICAL"
+	case log.PanicLevel:
+		return "EMERGENCY"
+	default:
+		return "DEFAULT"
+	}
+}
+
 func (f stackdriverFormatter) Format(e *log.Entry) ([]byte, error) {
 	msg := e.Data
 	msg["serviceContext"] = &nixeryContext
 	msg["message"] = &e.Message
 	msg["eventTime"] = &e.Time
+	msg["severity"] = logSeverity(e.Level)
 
 	if isError(e) {
 		loc := reportLocation{
-			FilePath: e.Caller.File,
-			LineNumber: e.Caller.Line,
+			FilePath:     e.Caller.File,
+			LineNumber:   e.Caller.Line,
 			FunctionName: e.Caller.Function,
 		}
 		msg["context"] = &loc
diff --git a/tools/nixery/server/main.go b/tools/nixery/server/main.go
index ca1f3c69f2d8..06de6a96a143 100644
--- a/tools/nixery/server/main.go
+++ b/tools/nixery/server/main.go
@@ -247,7 +247,10 @@ func main() {
 		Pop:    pop,
 	}
 
-	log.Printf("Starting Nixery (version %s) on port %s\n", version, cfg.Port)
+	log.WithFields(log.Fields{
+		"version": version,
+		"port":    cfg.Port,
+	}).Info("Starting Nixery")
 
 	// All /v2/ requests belong to the registry handler.
 	http.Handle("/v2/", &registryHandler{