// 清理条件: 将已切割的日志文件按条件(数量or时间)直接删除 //--- MaxAge and RotationCount cannot be both set 两者不能同时设置 //--- RotationCount用来设置最多切割的文件数(超过的会被 从旧到新 清理) //--- MaxAge 是设置文件清理前的最长保存时间 最小分钟为单位 //--- if both are 0, give maxAge a default 7 * 24 * time.Hour // WithRotationCount和WithMaxAge两个选项不能共存,只能设置一个(都设置编译时不会出错,但运行时会报错。也是为了防止影响切分的处理逻辑) //rotatelogs.WithRotationCount(10), // 超过这个数的文件会被清掉 rotatelogs.WithMaxAge(time.Hour*24*30), // 保存多久(设置文件清理前的最长保存时间 最小分钟为单位)
sugarLogger.Infof("测试压缩后少占用的空间,这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本这是填充文本,i is %d", i)
// 清理条件: 将已切割的日志文件按条件(数量or时间)直接删除 //--- MaxAge and RotationCount cannot be both set 两者不能同时设置 //--- RotationCount用来设置最多切割的文件数(超过的会被 从旧到新 清理) //--- MaxAge 是设置文件清理前的最长保存时间 最小分钟为单位 //--- if both are 0, give maxAge a default 7 * 24 * time.Hour // WithRotationCount和WithMaxAge两个选项不能共存,只能设置一个(都设置编译时不会出错,但运行时会报错。也是为了防止影响切分的处理逻辑) //rotatelogs.WithRotationCount(10), // 超过这个数的文件会被清掉 rotatelogs.WithMaxAge(time.Hour*24*30), // 保存多久(设置文件清理前的最长保存时间 最小分钟为单位)
// ZipFiles compresses one or many files into a single zip archive file. // Param 1: filename is the output zip file's name. // Param 2: files is a list of files to add to the zip. funcZipFiles(filename string, files []string)error {
// Using FileInfoHeader() above only uses the basename of the file. If we want // to preserve the folder structure we can overwrite this with the full path. header.Name = filename
// Change to deflate to gain better compression // see http://golang.org/pkg/archive/zip/#pkg-constants header.Method = zip.Deflate
// //// Zip compresses the specified files or dirs to zip archive. //// If a path is a dir don't need to specify the trailing path separator. //// For example calling Zip("archive.zip", "dir", "csv/baz.csv") will get archive.zip and the content of which is //// baz.csv //// dir //// ├── bar.txt //// └── foo.txt //// Note that if a file is a symbolic link it will be skipped. // //// https://blog.csdn.net/K346K346/article/details/122441250 //func Zip(zipPath string, paths ...string) error { // // Create zip file and it's parent dir. // if err := os.MkdirAll(filepath.Dir(zipPath), os.ModePerm); err != nil { // return err // } // archive, err := os.Create(zipPath) // if err != nil { // return err // } // defer archive.Close() // // // New zip writer. // zipWriter := zip.NewWriter(archive) // defer zipWriter.Close() // // // Traverse the file or directory. // for _, rootPath := range paths { // // Remove the trailing path separator if path is a directory. // rootPath = strings.TrimSuffix(rootPath, string(os.PathSeparator)) // // // Visit all the files or directories in the tree. // err = filepath.Walk(rootPath, walkFunc(rootPath, zipWriter)) // if err != nil { // return err // } // } // return nil //} // //func walkFunc(rootPath string, zipWriter *zip.Writer) filepath.WalkFunc { // return func(path string, info fs.FileInfo, err error) error { // if err != nil { // return err // } // // // If a file is a symbolic link it will be skipped. // if info.Mode()&os.ModeSymlink != 0 { // return nil // } // // // Create a local file header. // header, err := zip.FileInfoHeader(info) // if err != nil { // return err // } // // // Set compression method. // header.Method = zip.Deflate // // // Set relative path of a file as the header name. // header.Name, err = filepath.Rel(filepath.Dir(rootPath), path) // if err != nil { // return err // } // if info.IsDir() { // header.Name += string(os.PathSeparator) // } // // // Create writer for the file header and save content of the file. // headerWriter, err := zipWriter.CreateHeader(header) // if err != nil { // return err // } // if info.IsDir() { // return nil // } // f, err := os.Open(path) // if err != nil { // return err // } // defer f.Close() // _, err = io.Copy(headerWriter, f) // return err // } //}