feat(discord): added in a way to get panic messages that would crash the server only or fatal
This commit is contained in:
@@ -15,6 +15,7 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/bensch777/discord-webhook-golang v0.0.6 // indirect
|
||||||
github.com/bytedance/sonic v1.13.3 // indirect
|
github.com/bytedance/sonic v1.13.3 // indirect
|
||||||
github.com/bytedance/sonic/loader v0.2.4 // indirect
|
github.com/bytedance/sonic/loader v0.2.4 // indirect
|
||||||
github.com/cloudwego/base64x v0.1.5 // indirect
|
github.com/cloudwego/base64x v0.1.5 // indirect
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ func main() {
|
|||||||
// Initialize DB
|
// Initialize DB
|
||||||
if _, err := db.InitDB(); err != nil {
|
if _, err := db.InitDB(); err != nil {
|
||||||
|
|
||||||
log.Panic("Database intialize failed", "db", map[string]interface{}{
|
log.Panic("Database intialize failed, please check the server asap.", "db", map[string]interface{}{
|
||||||
"error": err.Error(),
|
"error": err.Error(),
|
||||||
"casue": errors.Unwrap(err),
|
"cause": errors.Unwrap(err),
|
||||||
"timeout": "30s",
|
"timeout": "30s",
|
||||||
"details": fmt.Sprintf("%+v", err), // Full stack trace if available
|
"details": fmt.Sprintf("%+v", err), // Full stack trace if available
|
||||||
})
|
})
|
||||||
|
|||||||
77
backend/pkg/logger/discord.go
Normal file
77
backend/pkg/logger/discord.go
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package logger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
discordwebhook "github.com/bensch777/discord-webhook-golang"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CreateDiscordMsg(message string) {
|
||||||
|
// we will only run the discord bot if we actaully put a url in the.
|
||||||
|
if os.Getenv("WEBHOOK") != "" {
|
||||||
|
var webhookurl = os.Getenv("WEBHOOK")
|
||||||
|
host, _ := os.Hostname()
|
||||||
|
embed := discordwebhook.Embed{
|
||||||
|
Title: "A new crash report from lst.",
|
||||||
|
Color: 15277667,
|
||||||
|
Url: "https://avatars.githubusercontent.com/u/6016509?s=48&v=4",
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
// Thumbnail: discordwebhook.Thumbnail{
|
||||||
|
// Url: "https://avatars.githubusercontent.com/u/6016509?s=48&v=4",
|
||||||
|
// },
|
||||||
|
// Author: discordwebhook.Author{
|
||||||
|
// Name: "Author Name",
|
||||||
|
// Icon_URL: "https://avatars.githubusercontent.com/u/6016509?s=48&v=4",
|
||||||
|
// },
|
||||||
|
Fields: []discordwebhook.Field{
|
||||||
|
discordwebhook.Field{
|
||||||
|
Name: host,
|
||||||
|
Value: message,
|
||||||
|
Inline: false,
|
||||||
|
},
|
||||||
|
// discordwebhook.Field{
|
||||||
|
// Name: "Error reason",
|
||||||
|
// Value: stack,
|
||||||
|
// Inline: false,
|
||||||
|
// },
|
||||||
|
// discordwebhook.Field{
|
||||||
|
// Name: "Field 3",
|
||||||
|
// Value: "Field Value 3",
|
||||||
|
// Inline: false,
|
||||||
|
// },
|
||||||
|
},
|
||||||
|
// Footer: discordwebhook.Footer{
|
||||||
|
// Text: "Footer Text",
|
||||||
|
// Icon_url: "https://avatars.githubusercontent.com/u/6016509?s=48&v=4",
|
||||||
|
// },
|
||||||
|
}
|
||||||
|
|
||||||
|
SendEmbed(webhookurl, embed)
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SendEmbed(link string, embeds discordwebhook.Embed) error {
|
||||||
|
logging := New()
|
||||||
|
logging.Info("new messege being posted to discord", "logger", map[string]interface{}{
|
||||||
|
"message": "Message",
|
||||||
|
})
|
||||||
|
hook := discordwebhook.Hook{
|
||||||
|
Username: "Captain Hook",
|
||||||
|
Avatar_url: "https://avatars.githubusercontent.com/u/6016509?s=48&v=4",
|
||||||
|
Content: "Message",
|
||||||
|
Embeds: []discordwebhook.Embed{embeds},
|
||||||
|
}
|
||||||
|
|
||||||
|
payload, err := json.Marshal(hook)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
err = discordwebhook.ExecuteWebhook(link, payload)
|
||||||
|
return err
|
||||||
|
|
||||||
|
}
|
||||||
@@ -107,6 +107,7 @@ func (l *CustomLogger) Panic(message, service string, fields map[string]interfac
|
|||||||
l.consoleLogger.Warn().Msg("Additional panic context captured")
|
l.consoleLogger.Warn().Msg("Additional panic context captured")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CreateDiscordMsg(message)
|
||||||
panic(message)
|
panic(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user