mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-10-09 23:17:21 +08:00
89 lines
2.2 KiB
Go
89 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/1Panel-dev/1Panel/agent/buserr"
|
|
"github.com/1Panel-dev/1Panel/agent/global"
|
|
)
|
|
|
|
type LogService struct{}
|
|
|
|
type ILogService interface {
|
|
ListSystemLogFile() ([]string, error)
|
|
LoadSystemLog(name string) (string, error)
|
|
}
|
|
|
|
func NewILogService() ILogService {
|
|
return &LogService{}
|
|
}
|
|
|
|
func (u *LogService) ListSystemLogFile() ([]string, error) {
|
|
var listFile []string
|
|
files, err := os.ReadDir(global.Dir.LogDir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
listMap := make(map[string]struct{})
|
|
for _, item := range files {
|
|
if !item.IsDir() && strings.HasPrefix(item.Name(), "1Panel") {
|
|
if item.Name() == "1Panel.log" || item.Name() == "1Panel-Core.log" {
|
|
itemName := time.Now().Format("2006-01-02")
|
|
if _, ok := listMap[itemName]; ok {
|
|
continue
|
|
}
|
|
listMap[itemName] = struct{}{}
|
|
listFile = append(listFile, itemName)
|
|
continue
|
|
}
|
|
itemFileName := strings.TrimPrefix(item.Name(), "1Panel-Core-")
|
|
itemFileName = strings.TrimPrefix(itemFileName, "1Panel-")
|
|
itemFileName = strings.TrimSuffix(itemFileName, ".gz")
|
|
itemFileName = strings.TrimSuffix(itemFileName, ".log")
|
|
if len(itemFileName) == 0 {
|
|
continue
|
|
}
|
|
if _, ok := listMap[itemFileName]; ok {
|
|
continue
|
|
}
|
|
listMap[itemFileName] = struct{}{}
|
|
listFile = append(listFile, itemFileName)
|
|
}
|
|
}
|
|
if len(listFile) < 2 {
|
|
return listFile, nil
|
|
}
|
|
sort.Slice(listFile, func(i, j int) bool {
|
|
return listFile[i] > listFile[j]
|
|
})
|
|
|
|
return listFile, nil
|
|
}
|
|
|
|
func (u *LogService) LoadSystemLog(name string) (string, error) {
|
|
if name == time.Now().Format("2006-01-02") {
|
|
name = "1Panel.log"
|
|
} else {
|
|
name = "1Panel-" + name + ".log"
|
|
}
|
|
filePath := path.Join(global.Dir.LogDir, name)
|
|
if _, err := os.Stat(filePath); err != nil {
|
|
fileGzPath := path.Join(global.Dir.LogDir, name+".gz")
|
|
if _, err := os.Stat(fileGzPath); err != nil {
|
|
return "", buserr.New("ErrHttpReqNotFound")
|
|
}
|
|
if err := handleGunzip(fileGzPath); err != nil {
|
|
return "", fmt.Errorf("handle ungzip file %s failed, err: %v", fileGzPath, err)
|
|
}
|
|
}
|
|
content, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(content), nil
|
|
}
|