Skip to content

Commit 7602caf

Browse files
committed
fix: Fix task log printing garbled characters issue
1 parent d957990 commit 7602caf

File tree

4 files changed

+41
-43
lines changed

4 files changed

+41
-43
lines changed

agent/app/service/app_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/base64"
77
"encoding/json"
88
"fmt"
9-
"log"
109
"maps"
1110
"math"
1211
"net/http"
@@ -45,6 +44,7 @@ import (
4544
"github.com/compose-spec/compose-go/v2/types"
4645
"github.com/docker/docker/api/types/container"
4746
"github.com/pkg/errors"
47+
"github.com/sirupsen/logrus"
4848
"github.com/subosito/gotenv"
4949
"gopkg.in/yaml.v3"
5050
)
@@ -959,7 +959,7 @@ func handleMap(params map[string]interface{}, envParams map[string]string) {
959959
}
960960
}
961961

962-
func downloadApp(app model.App, appDetail model.AppDetail, appInstall *model.AppInstall, logger *log.Logger) (err error) {
962+
func downloadApp(app model.App, appDetail model.AppDetail, appInstall *model.AppInstall, logger *logrus.Logger) (err error) {
963963
if app.IsLocalApp() || app.IsCustomApp() {
964964
return nil
965965
}

agent/app/task/task.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
package task
22

33
import (
4-
"bufio"
54
"context"
65
"errors"
76
"fmt"
8-
"log"
97
"os"
108
"path"
119
"strconv"
1210
"time"
1311

1412
"github.com/1Panel-dev/1Panel/agent/buserr"
1513
"github.com/1Panel-dev/1Panel/agent/global"
14+
"github.com/sirupsen/logrus"
1615

1716
"github.com/1Panel-dev/1Panel/agent/app/model"
1817
"github.com/1Panel-dev/1Panel/agent/app/repo"
@@ -29,8 +28,7 @@ type Task struct {
2928

3029
Name string
3130
TaskID string
32-
Logger *log.Logger
33-
Writer *bufio.Writer
31+
Logger *logrus.Logger
3432
SubTasks []*SubTask
3533
Rollbacks []RollbackFunc
3634
logFile *os.File
@@ -138,12 +136,13 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e
138136
}
139137
}
140138
logPath := path.Join(global.Dir.TaskDir, taskScope, taskID+".log")
141-
file, err := os.OpenFile(logPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
139+
logger := logrus.New()
140+
logger.SetFormatter(&SimpleFormatter{})
141+
logFile, err := os.OpenFile(logPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
142142
if err != nil {
143143
return nil, fmt.Errorf("failed to open log file: %w", err)
144144
}
145-
writer := bufio.NewWriter(file)
146-
logger := log.New(file, "", log.LstdFlags)
145+
logger.SetOutput(logFile)
147146
taskModel := &model.Task{
148147
ID: taskID,
149148
Name: name,
@@ -156,7 +155,7 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e
156155
taskRepo := repo.NewITaskRepo()
157156
ctx, cancle := context.WithCancel(context.Background())
158157
global.TaskCtxMap[taskID] = cancle
159-
task := &Task{TaskCtx: ctx, Name: name, logFile: file, Logger: logger, taskRepo: taskRepo, Task: taskModel, Writer: writer}
158+
task := &Task{TaskCtx: ctx, Name: name, logFile: logFile, Logger: logger, taskRepo: taskRepo, Task: taskModel}
160159
return task, nil
161160
}
162161

@@ -175,16 +174,18 @@ func ReNewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task,
175174
return nil, fmt.Errorf("failed to create log directory: %w", err)
176175
}
177176
}
177+
178178
logPath := path.Join(global.Dir.TaskDir, taskScope, taskID+".log")
179-
file, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
179+
logger := logrus.New()
180+
logger.SetFormatter(&SimpleFormatter{})
181+
logFile, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
180182
if err != nil {
181183
return nil, fmt.Errorf("failed to open log file: %w", err)
182184
}
183-
writer := bufio.NewWriter(file)
184-
logger := log.New(file, "", log.LstdFlags)
185+
logger.SetOutput(logFile)
185186
logger.Print("\n --------------------------------------------------- \n")
186187
taskItem.Status = constant.StatusExecuting
187-
task := &Task{Name: name, logFile: file, Logger: logger, taskRepo: taskRepo, Task: &taskItem, Writer: writer}
188+
task := &Task{Name: name, logFile: logFile, Logger: logger, taskRepo: taskRepo, Task: &taskItem}
188189
task.updateTask(&taskItem)
189190
return task, nil
190191
}
@@ -353,3 +354,11 @@ func (t *Task) LogSuccessWithOps(operate, msg string) {
353354
func (t *Task) LogFailedWithOps(operate, msg string, err error) {
354355
t.Logger.Printf("%s%s%s : %s ", i18n.GetMsgByKey(operate), msg, i18n.GetMsgByKey("Failed"), err.Error())
355356
}
357+
358+
type SimpleFormatter struct{}
359+
360+
func (f *SimpleFormatter) Format(entry *logrus.Entry) ([]byte, error) {
361+
timestamp := entry.Time.Format("2006/01/02 15:04:05")
362+
message := fmt.Sprintf("%s %s\n", timestamp, entry.Message)
363+
return []byte(message), nil
364+
}

agent/utils/docker/docker.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,7 @@ func (c Client) PushImageWithProcessAndOptions(task *task.Task, imageName string
243243
status, _ := progress["status"].(string)
244244
switch status {
245245
case "Pushing":
246-
id, _ := progress["id"].(string)
247-
progressDetail, _ := progress["progressDetail"].(map[string]interface{})
248-
current, _ := progressDetail["current"].(float64)
249-
progressStr := ""
250-
total, ok := progressDetail["total"].(float64)
251-
if ok {
252-
progressStr = fmt.Sprintf("%s [%s] --- %.2f%%", status, id, (current/total)*100)
253-
} else {
254-
progressStr = fmt.Sprintf("%s [%s] --- %.2f%%", status, id, current)
255-
}
256-
257-
_ = setLog(id, progressStr, task)
246+
logProcess(progress, task)
258247
case "Pushed":
259248
id, _ := progress["id"].(string)
260249
progressStr := fmt.Sprintf("%s [%s] --- %.2f%%", status, id, 100.0)
@@ -298,17 +287,7 @@ func (c Client) BuildImageWithProcessAndOptions(task *task.Task, tar io.ReadClos
298287
}
299288
switch status {
300289
case "Downloading", "Extracting":
301-
id, _ := progress["id"].(string)
302-
progressDetail, _ := progress["progressDetail"].(map[string]interface{})
303-
current, _ := progressDetail["current"].(float64)
304-
progressStr := ""
305-
total, ok := progressDetail["total"].(float64)
306-
if ok {
307-
progressStr = fmt.Sprintf("%s [%s] --- %.2f%%", status, id, (current/total)*100)
308-
} else {
309-
progressStr = fmt.Sprintf("%s [%s] --- %.2f%%", status, id, current)
310-
}
311-
_ = setLog(id, progressStr, task)
290+
logProcess(progress, task)
312291
case "Pull complete", "Download complete", "Verifying Checksum":
313292
id, _ := progress["id"].(string)
314293
progressStr := fmt.Sprintf("%s [%s] --- %.2f%%", status, id, 100.0)

core/app/task/task.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package task
33
import (
44
"context"
55
"fmt"
6-
"log"
76
"os"
87
"path"
98
"strconv"
@@ -16,6 +15,7 @@ import (
1615
"github.com/1Panel-dev/1Panel/core/global"
1716
"github.com/1Panel-dev/1Panel/core/i18n"
1817
"github.com/google/uuid"
18+
"github.com/sirupsen/logrus"
1919
)
2020

2121
type ActionFunc func(*Task) error
@@ -24,7 +24,7 @@ type RollbackFunc func(*Task)
2424
type Task struct {
2525
Name string
2626
TaskID string
27-
Logger *log.Logger
27+
Logger *logrus.Logger
2828
SubTasks []*SubTask
2929
Rollbacks []RollbackFunc
3030
logFile *os.File
@@ -59,7 +59,7 @@ const (
5959

6060
const (
6161
TaskScopeSystem = "System"
62-
TaskScopeScript = "Script"
62+
TaskScopeScript = "ScriptLibrary"
6363
TaskScopeNodeFile = "NodeFile"
6464
TaskScopeAppBackup = "AppBackup"
6565
TaskScopeCluster = "Cluster"
@@ -85,11 +85,13 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e
8585
}
8686
}
8787
logPath := path.Join(logItem, taskScope, taskID+".log")
88-
file, err := os.OpenFile(logPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
88+
logger := logrus.New()
89+
logger.SetFormatter(&SimpleFormatter{})
90+
logFile, err := os.OpenFile(logPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
8991
if err != nil {
9092
return nil, fmt.Errorf("failed to open log file: %w", err)
9193
}
92-
logger := log.New(file, "", log.LstdFlags)
94+
logger.SetOutput(logFile)
9395
taskModel := &model.Task{
9496
ID: taskID,
9597
Name: name,
@@ -100,7 +102,7 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e
100102
Operate: operate,
101103
}
102104
taskRepo := repo.NewITaskRepo()
103-
task := &Task{Name: name, logFile: file, Logger: logger, taskRepo: taskRepo, Task: taskModel}
105+
task := &Task{Name: name, logFile: logFile, Logger: logger, taskRepo: taskRepo, Task: taskModel}
104106
return task, nil
105107
}
106108

@@ -263,3 +265,11 @@ func (t *Task) LogSuccessWithOps(operate, msg string) {
263265
func (t *Task) LogFailedWithOps(operate, msg string, err error) {
264266
t.Logger.Printf("%s%s%s : %s ", i18n.GetMsgByKey(operate), msg, i18n.GetMsgByKey("Failed"), err.Error())
265267
}
268+
269+
type SimpleFormatter struct{}
270+
271+
func (f *SimpleFormatter) Format(entry *logrus.Entry) ([]byte, error) {
272+
timestamp := entry.Time.Format("2006/01/02 15:04:05")
273+
message := fmt.Sprintf("%s %s\n", timestamp, entry.Message)
274+
return []byte(message), nil
275+
}

0 commit comments

Comments
 (0)