From 1f1c16ca4eaef136deac8dfaed0fbaa356eb9f2f Mon Sep 17 00:00:00 2001 From: vaporvee Date: Sun, 17 Mar 2024 17:16:47 +0100 Subject: [PATCH] added github links to logs --- custom/custom.go | 25 +++++++++++++++++++++++++ go.mod | 2 ++ log2webhook/log2webhook.go | 18 ++++++++++++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 custom/custom.go diff --git a/custom/custom.go b/custom/custom.go new file mode 100644 index 0000000..f1a7174 --- /dev/null +++ b/custom/custom.go @@ -0,0 +1,25 @@ +package custom + +import ( + "strconv" + "strings" +) + +var color map[string]string = map[string]string{ + "red": "#FF6B6B", + "yellow": "#FFD93D", + "green": "#6BCB77", + "blue": "#008DDA", + "primary": "#211951", +} + +var Gh_url string = "https://github.com/vaporvee/acecore/blob/main/" + +func GetColor(s string) int { + hexColor := strings.TrimPrefix(color[s], "#") + decimal, err := strconv.ParseInt(hexColor, 16, 64) + if err != nil { + return 0 + } + return int(decimal) +} diff --git a/go.mod b/go.mod index 1e29e29..222e87f 100644 --- a/go.mod +++ b/go.mod @@ -15,3 +15,5 @@ require ( golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect ) + +replace github.com/vaporvee/acecore/custom => ./custom diff --git a/log2webhook/log2webhook.go b/log2webhook/log2webhook.go index 0447e16..8af0e20 100644 --- a/log2webhook/log2webhook.go +++ b/log2webhook/log2webhook.go @@ -6,7 +6,12 @@ import ( "fmt" "net/http" "os" + "path" + "path/filepath" + "runtime" "strings" + + "github.com/vaporvee/acecore/custom" ) type Log struct { @@ -23,6 +28,7 @@ type Message struct { type Embed struct { Author Author `json:"author"` Title string `json:"title"` + URL string `json:"url"` Color string `json:"color"` Description string `json:"description"` Footer Footer `json:"footer"` @@ -43,6 +49,12 @@ func (cw *WebhookWriter) Write(p []byte) (n int, err error) { return len(p), nil } +func RootDir() string { + _, b, _, _ := runtime.Caller(0) + d := path.Join(path.Dir(b)) + return filepath.Dir(d) +} + func webhook(p []byte) { webhookURL := os.Getenv("LOG_WEBHOOK") if webhookURL == "" || !strings.HasPrefix(webhookURL, "http://") && !strings.HasPrefix(webhookURL, "https://") { @@ -54,13 +66,15 @@ func webhook(p []byte) { if logJson.Level == "error" { color = "16739179" } + fileArray := strings.Split(strings.TrimPrefix(logJson.File, RootDir()), ":") m := Message{ Embeds: []Embed{ { Author: Author{ - Name: logJson.File, + Name: logJson.Function, }, - Title: logJson.Function, + Title: "\"" + fileArray[0] + "\" on line " + fileArray[1], + URL: strings.TrimSuffix(custom.Gh_url, "/") + fileArray[0] + "#L" + fileArray[1], Color: color, Description: logJson.Message, Footer: Footer{