about summary refs log tree commit diff
path: root/tools/nixery/server/logs.go
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 /tools/nixery/server/logs.go
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.
Diffstat (limited to 'tools/nixery/server/logs.go')
-rw-r--r--tools/nixery/server/logs.go34
1 files changed, 32 insertions, 2 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