nixery/server/logs.go
Vincent Ambo 2268e6fa88 feat(server): Use hash of Nixery source as version
Uses a hash of Nixery's sources as the version displayed when Nixery
launches or logs an error. This makes it possible to distinguish
between errors logged from different versions.

The source hashes should be reproducible between different checkouts
of the same source tree.
2019-10-06 23:05:23 +01:00

69 lines
1.6 KiB
Go

package main
// This file configures different log formatters via logrus. The
// standard formatter uses a structured JSON format that is compatible
// with Stackdriver Error Reporting.
//
// https://cloud.google.com/error-reporting/docs/formatting-error-messages
import (
"bytes"
"encoding/json"
log "github.com/sirupsen/logrus"
)
type stackdriverFormatter struct{}
type serviceContext struct {
Service string `json:"service"`
Version string `json:"version"`
}
type reportLocation struct {
FilePath string `json:"filePath"`
LineNumber int `json:"lineNumber"`
FunctionName string `json:"functionName"`
}
var nixeryContext = serviceContext{
Service: "nixery",
}
// isError determines whether an entry should be logged as an error
// (i.e. with attached `context`).
//
// This requires the caller information to be present on the log
// entry, as stacktraces are not available currently.
func isError(e *log.Entry) bool {
l := e.Level
return (l == log.ErrorLevel || l == log.FatalLevel || l == log.PanicLevel) &&
e.HasCaller()
}
func (f stackdriverFormatter) Format(e *log.Entry) ([]byte, error) {
msg := e.Data
msg["serviceContext"] = &nixeryContext
msg["message"] = &e.Message
msg["eventTime"] = &e.Time
if isError(e) {
loc := reportLocation{
FilePath: e.Caller.File,
LineNumber: e.Caller.Line,
FunctionName: e.Caller.Function,
}
msg["context"] = &loc
}
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(&msg)
return b.Bytes(), err
}
func init() {
nixeryContext.Version = version
log.SetReportCaller(true)
log.SetFormatter(stackdriverFormatter{})
}