11package service
22
33import (
4- "fmt"
54 "os"
6- "path"
75 "sort"
86 "strings"
97 "time"
108
11- "github.com/1Panel-dev/1Panel/agent/buserr"
129 "github.com/1Panel-dev/1Panel/agent/global"
1310)
1411
1512type LogService struct {}
1613
1714type ILogService interface {
1815 ListSystemLogFile () ([]string , error )
19- LoadSystemLog (name string ) (string , error )
2016}
2117
2218func NewILogService () ILogService {
@@ -31,29 +27,30 @@ func (u *LogService) ListSystemLogFile() ([]string, error) {
3127 }
3228 listMap := make (map [string ]struct {})
3329 for _ , item := range files {
34- if ! item .IsDir () && strings .HasPrefix (item .Name (), "1Panel" ) {
35- if item .Name () == "1Panel.log" || item .Name () == "1Panel-Core.log" {
36- itemName := time .Now ().Format ("2006-01-02" )
37- if _ , ok := listMap [itemName ]; ok {
38- continue
39- }
40- listMap [itemName ] = struct {}{}
41- listFile = append (listFile , itemName )
42- continue
43- }
44- itemFileName := strings .TrimPrefix (item .Name (), "1Panel-Core-" )
45- itemFileName = strings .TrimPrefix (itemFileName , "1Panel-" )
46- itemFileName = strings .TrimSuffix (itemFileName , ".gz" )
47- itemFileName = strings .TrimSuffix (itemFileName , ".log" )
48- if len (itemFileName ) == 0 {
49- continue
50- }
51- if _ , ok := listMap [itemFileName ]; ok {
30+ if item .IsDir () || ! strings .HasPrefix (item .Name (), "1Panel" ) {
31+ continue
32+ }
33+ if item .Name () == "1Panel.log" || item .Name () == "1Panel-Core.log" {
34+ itemName := time .Now ().Format ("2006-01-02" )
35+ if _ , ok := listMap [itemName ]; ok {
5236 continue
5337 }
54- listMap [itemFileName ] = struct {}{}
55- listFile = append (listFile , itemFileName )
38+ listMap [itemName ] = struct {}{}
39+ listFile = append (listFile , itemName )
40+ continue
41+ }
42+ itemFileName := strings .TrimPrefix (item .Name (), "1Panel-Core-" )
43+ itemFileName = strings .TrimPrefix (itemFileName , "1Panel-" )
44+ itemFileName = strings .TrimSuffix (itemFileName , ".gz" )
45+ itemFileName = strings .TrimSuffix (itemFileName , ".log" )
46+ if len (itemFileName ) == 0 {
47+ continue
5648 }
49+ if _ , ok := listMap [itemFileName ]; ok {
50+ continue
51+ }
52+ listMap [itemFileName ] = struct {}{}
53+ listFile = append (listFile , itemFileName )
5754 }
5855 if len (listFile ) < 2 {
5956 return listFile , nil
@@ -64,26 +61,3 @@ func (u *LogService) ListSystemLogFile() ([]string, error) {
6461
6562 return listFile , nil
6663}
67-
68- func (u * LogService ) LoadSystemLog (name string ) (string , error ) {
69- if name == time .Now ().Format ("2006-01-02" ) {
70- name = "1Panel.log"
71- } else {
72- name = "1Panel-" + name + ".log"
73- }
74- filePath := path .Join (global .Dir .LogDir , name )
75- if _ , err := os .Stat (filePath ); err != nil {
76- fileGzPath := path .Join (global .Dir .LogDir , name + ".gz" )
77- if _ , err := os .Stat (fileGzPath ); err != nil {
78- return "" , buserr .New ("ErrHttpReqNotFound" )
79- }
80- if err := handleGunzip (fileGzPath ); err != nil {
81- return "" , fmt .Errorf ("handle ungzip file %s failed, err: %v" , fileGzPath , err )
82- }
83- }
84- content , err := os .ReadFile (filePath )
85- if err != nil {
86- return "" , err
87- }
88- return string (content ), nil
89- }
0 commit comments