Skip to content

Commit

Permalink
增加获取最近log
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Apr 5, 2020
1 parent 36be84c commit 56b8787
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import (
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"strings"
"time"
)

//true=>no error
func Min(x, y int) int {
if x < y {
return x
}
return y
}
func CheckErr(err error) bool {
if err != nil {
log.Println(err)
Expand Down Expand Up @@ -63,3 +71,42 @@ func GetMD5Encode(data string) string {
func Get16MD5Encode(data string) string {
return GetMD5Encode(data)[8:24]
}

//只返回文件名
func GetPathFiles(path string) []string {
files, _ := ioutil.ReadDir(path)
var t []string
for _, file := range files {
if file.IsDir() {
continue
} else {
t = append(t, file.Name())
}
}
return t
}

//输入文件夹路径,返回最近n个log的路径,不到n个返回所有
func GetRecentLogs(path string, n int) []string {
var paths []string
if !PathExists(path) {
return paths
}
//path末尾检查/
if path[len(path)-1:] != "/" {
path += "/"
}
data := time.Now()
d, _ := time.ParseDuration("-24h")
//不到n个返回所有
nt := Min(n, len(GetPathFiles(path)))
//fmt.Println(nt)
for i := 1; i <= nt; {
if PathExists(path + data.Format("2006-01-02") + ".log") {
paths = append(paths, path+data.Format("2006-01-02")+".log")
i++
}
data = data.Add(d)
}
return paths
}

0 comments on commit 56b8787

Please sign in to comment.