mirror of
https://github.com/Starlio-app/StarlioX
synced 2024-11-05 06:03:57 +03:00
Now an error is written in the console. And also wrote a function that deletes log files older than 7 days
This commit is contained in:
parent
80d3473dd0
commit
6cbe10e9c2
1 changed files with 69 additions and 0 deletions
69
utils/logger.go
Normal file
69
utils/logger.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gopkg.in/toast.v1"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Log(text string) {
|
||||
timestamps := time.Now()
|
||||
logFile := string(timestamps.Format("02.01.06")) + ".log"
|
||||
|
||||
if !FileExists("log") {
|
||||
os.Mkdir("log", 0755)
|
||||
}
|
||||
if !FileExists("log/" + logFile) {
|
||||
file, _ := os.Create("log/" + logFile)
|
||||
|
||||
defer func(file *os.File) {
|
||||
file.Close()
|
||||
}(file)
|
||||
}
|
||||
|
||||
file, _ := os.OpenFile("log/"+logFile, os.O_APPEND|os.O_WRONLY, 0600)
|
||||
fileText := timestamps.Format("15:04:05") + " | " + text + "\n"
|
||||
|
||||
file.Write([]byte(fileText))
|
||||
fmt.Println(fileText)
|
||||
|
||||
dir, _ := os.Getwd()
|
||||
dir = dir + "\\log\\" + logFile
|
||||
fmt.Println("Error has been save to " + dir)
|
||||
|
||||
Notify(
|
||||
"There was an error",
|
||||
"An unknown error occurred, the error was recorded in the log file",
|
||||
toast.Action{Type: "protocol", Label: "View log file", Arguments: dir},
|
||||
)
|
||||
}
|
||||
|
||||
func CheckLogs() {
|
||||
if !FileExists("log") {
|
||||
return
|
||||
}
|
||||
|
||||
files, err := os.ReadDir("log")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
fileTimestamps, _ := strconv.Atoi(f.Name()[:2])
|
||||
timestamps := time.Now().Day()
|
||||
|
||||
if timestamps-fileTimestamps >= 7 {
|
||||
os.Remove("log/" + f.Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
func FileExists(name string) bool {
|
||||
if _, err := os.Stat(name); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
Loading…
Reference in a new issue