Tidy temporary message log format

This commit is contained in:
Fred Boniface 2023-07-19 14:22:29 +01:00
parent 3266b147ee
commit f02e3ce644
3 changed files with 17 additions and 4 deletions

View File

@ -7,7 +7,7 @@
*.dll *.dll
*.so *.so
*.dylib *.dylib
*.txt message-logs
# Test binary, built with `go test -c` # Test binary, built with `go test -c`
*.test *.test

2
.gitignore vendored
View File

@ -7,7 +7,7 @@
*.dll *.dll
*.so *.so
*.dylib *.dylib
*.txt message-logs
# Test binary, built with `go test -c` # Test binary, built with `go test -c`
*.test *.test

View File

@ -1,6 +1,7 @@
package messaging package messaging
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"time" "time"
@ -43,9 +44,21 @@ func handle(msg *stomp.Message) {
func saveToFile(msg string) { func saveToFile(msg string) {
timestamp := time.Now().Format("2006-01-02T15:04:05") timestamp := time.Now().Format("2006-01-02T15:04:05")
path := fmt.Sprintf("%s-msg.txt", timestamp) path := fmt.Sprintf("message-logs/%s-msg.txt", timestamp)
err := ioutil.WriteFile(path, []byte(msg), 0644) var decodedData interface{}
if err := json.Unmarshal([]byte(msg), &decodedData); err != nil {
log.Msg.Error("Error decoding JSON: " + err.Error())
return
}
prettyJSON, err := json.MarshalIndent(decodedData, "", " ")
if err != nil {
log.Msg.Error("Error marshaling data to JSON: " + err.Error())
return
}
err = ioutil.WriteFile(path, prettyJSON, 0644)
if err != nil { if err != nil {
log.Msg.Error("Error saving message: " + err.Error()) log.Msg.Error("Error saving message: " + err.Error())
return return