问题:当我尝试使用错误报告功能记录错误时,它不会发送到错误报告器。
我这样实例化:
const projectID = "correct-project-id"
var ErrorClient *errorreporting.Client
func MakeErrorReporter() {
var err error
ctx := context.Background()
ErrorClient, err = errorreporting.NewClient(ctx, projectID, errorreporting.Config{
ServiceName: "easyLanding",
OnError: func(err error) {
log.Printf("Could not log error: %v", err)
},
})
if err != nil {
msg := fmt.Sprintf("this is the fatal err", err.Error())
log.Fatal(msg)
}
defer ErrorClient.Close()
}
然后我这样称呼它:
func LogErr(err error, location string, userId uint) string {
errCode := RandStringRunes(4)
msg := fmt.Sprintf("Code: %v | Error: %v | Location: %v", errCode, err.Error(), location)
var email string
if userId == 0 {
email = "system"
} else {
email, _ = GetUserEmail(userId)
}
if viper.GetString("debug") == "false" {
logger.Logger.Error().Msgf("Just ran the error with client %v", errorReporting.ErrorClient.Report)
if errorReporting.ErrorClient == nil {
logger.Logger.Error().Msg("Error client is nil")
} else {
logger.Logger.Error().Msg("Error client is NOT nil")
}
email = fmt.Sprintf("%s - %s", email, location)
errorReporting.ErrorClient.Report(errorreporting.Entry{
Error: err,
User: email,
})
label := map[string]string{}
label["key"] = "simpleMessage"
logger.InfoLogger.Log(logging.Entry{
Severity: logging.Info,
Payload: msg,
Labels: label,
InsertID: "",
})
} else {
logger.Logger.Error().Msg(msg)
logger.Logger.Error().Msg(email)
}
return errCode
}
日志Error client is NOT nil
运行,所以我知道它已经启动,但没有出现任何警报。我还通过一个特殊的 URL 端点触发它,该端点在每次点击时记录一个错误(或应该)。