utils/json_utils.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package utils import ( "bufio" "encoding/json" "fmt" "os" "time" ) func WriteLog(log LoggerProps) { file, _ := os.OpenFile("log.json", os.O_APPEND|os.O_WRONLY, 0644) defer file.Close() jsonBytes, err := json.Marshal(log) if err != nil { fmt.Println("Error marshalling to JSON:", err) return } jsonString := string(jsonBytes) fmt.Fprintf(file, "%v\n", jsonString) } func ReadLogs() ([]LoggerProps, error) { file, err := os.Open("log.json") if err != nil { return nil, err } defer file.Close() var logs []LoggerProps scanner := bufio.NewScanner(file) for scanner.Scan() { var log LoggerProps err := json.Unmarshal(scanner.Bytes(), &log) if err != nil { return nil, err } logs = append(logs, log) } if err := scanner.Err(); err != nil { return nil, err } return logs, nil } func TimeStampToHumanReadable(timestamp int64) string { t := time.Unix(timestamp, 0) dateFormat := "2006-01-02 15:04:05" return t.Format(dateFormat) } |