2022-04-20 16:41:20 +02:00
|
|
|
// Copyright 2022 The TVL Contributors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2019-11-11 22:07:16 +01:00
|
|
|
package logs
|
2019-10-05 15:55:40 +02:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2019-10-05 23:48:19 +02:00
|
|
|
// 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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-05 15:55:40 +02:00
|
|
|
func (f stackdriverFormatter) Format(e *log.Entry) ([]byte, error) {
|
|
|
|
msg := e.Data
|
|
|
|
msg["serviceContext"] = &nixeryContext
|
|
|
|
msg["message"] = &e.Message
|
|
|
|
msg["eventTime"] = &e.Time
|
2019-10-05 23:48:19 +02:00
|
|
|
msg["severity"] = logSeverity(e.Level)
|
2019-10-05 15:55:40 +02:00
|
|
|
|
2019-10-28 22:38:27 +01:00
|
|
|
if e, ok := msg[log.ErrorKey]; ok {
|
|
|
|
if err, isError := e.(error); isError {
|
|
|
|
msg[log.ErrorKey] = err.Error()
|
|
|
|
} else {
|
|
|
|
delete(msg, log.ErrorKey)
|
|
|
|
}
|
2019-10-28 18:20:04 +01:00
|
|
|
}
|
|
|
|
|
2019-10-05 15:55:40 +02:00
|
|
|
if isError(e) {
|
|
|
|
loc := reportLocation{
|
2019-10-05 23:48:19 +02:00
|
|
|
FilePath: e.Caller.File,
|
|
|
|
LineNumber: e.Caller.Line,
|
2019-10-05 15:55:40 +02:00
|
|
|
FunctionName: e.Caller.Function,
|
|
|
|
}
|
|
|
|
msg["context"] = &loc
|
|
|
|
}
|
|
|
|
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
err := json.NewEncoder(b).Encode(&msg)
|
|
|
|
|
|
|
|
return b.Bytes(), err
|
|
|
|
}
|
|
|
|
|
2019-11-11 22:07:16 +01:00
|
|
|
func Init(version string) {
|
2019-10-05 23:33:41 +02:00
|
|
|
nixeryContext.Version = version
|
2019-10-05 15:55:40 +02:00
|
|
|
log.SetReportCaller(true)
|
|
|
|
log.SetFormatter(stackdriverFormatter{})
|
|
|
|
}
|