package cutil import ( "bufio" "fmt" "github.com/spf13/cast" "os" "tools/entity" ) func Logs(taskId int64, msg string) { go func() { logPath := entity.GetConf().Log if logPath == "" { logPath = "/data/logs/ww2/" } filePath := logPath + cast.ToString(taskId) + ".log" file, err := os.OpenFile(filePath, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666) if err != nil { fmt.Println("文件打开失败", err) file, err = os.Create(filePath) } //及时关闭file句柄 defer func(file *os.File) { err := file.Close() if err != nil { } }(file) //写入文件时,使用带缓存的 *Writer write := bufio.NewWriter(file) msg = msg + "\r\n" write.WriteString(msg) //Flush将缓存的文件真正写入到文件中 write.Flush() }() } func DeleteLog(taskId int64) { logPath := entity.GetConf().Log if logPath == "" { logPath = "/data/logs/ww2/" } filePath := logPath + cast.ToString(taskId) + ".log" _ = os.Remove(filePath) }