mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2026-01-12 01:55:15 +08:00
fix: Fix task log printing garbled characters issue
This commit is contained in:
parent
d9579901fe
commit
7602caf235
4 changed files with 41 additions and 43 deletions
|
|
@ -6,7 +6,6 @@ import (
|
|||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"maps"
|
||||
"math"
|
||||
"net/http"
|
||||
|
|
@ -45,6 +44,7 @@ import (
|
|||
"github.com/compose-spec/compose-go/v2/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/subosito/gotenv"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
|
@ -959,7 +959,7 @@ func handleMap(params map[string]interface{}, envParams map[string]string) {
|
|||
}
|
||||
}
|
||||
|
||||
func downloadApp(app model.App, appDetail model.AppDetail, appInstall *model.AppInstall, logger *log.Logger) (err error) {
|
||||
func downloadApp(app model.App, appDetail model.AppDetail, appInstall *model.AppInstall, logger *logrus.Logger) (err error) {
|
||||
if app.IsLocalApp() || app.IsCustomApp() {
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
package task
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
|
|
@ -13,6 +11,7 @@ import (
|
|||
|
||||
"github.com/1Panel-dev/1Panel/agent/buserr"
|
||||
"github.com/1Panel-dev/1Panel/agent/global"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/1Panel-dev/1Panel/agent/app/model"
|
||||
"github.com/1Panel-dev/1Panel/agent/app/repo"
|
||||
|
|
@ -29,8 +28,7 @@ type Task struct {
|
|||
|
||||
Name string
|
||||
TaskID string
|
||||
Logger *log.Logger
|
||||
Writer *bufio.Writer
|
||||
Logger *logrus.Logger
|
||||
SubTasks []*SubTask
|
||||
Rollbacks []RollbackFunc
|
||||
logFile *os.File
|
||||
|
|
@ -138,12 +136,13 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e
|
|||
}
|
||||
}
|
||||
logPath := path.Join(global.Dir.TaskDir, taskScope, taskID+".log")
|
||||
file, err := os.OpenFile(logPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
|
||||
logger := logrus.New()
|
||||
logger.SetFormatter(&SimpleFormatter{})
|
||||
logFile, err := os.OpenFile(logPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open log file: %w", err)
|
||||
}
|
||||
writer := bufio.NewWriter(file)
|
||||
logger := log.New(file, "", log.LstdFlags)
|
||||
logger.SetOutput(logFile)
|
||||
taskModel := &model.Task{
|
||||
ID: taskID,
|
||||
Name: name,
|
||||
|
|
@ -156,7 +155,7 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e
|
|||
taskRepo := repo.NewITaskRepo()
|
||||
ctx, cancle := context.WithCancel(context.Background())
|
||||
global.TaskCtxMap[taskID] = cancle
|
||||
task := &Task{TaskCtx: ctx, Name: name, logFile: file, Logger: logger, taskRepo: taskRepo, Task: taskModel, Writer: writer}
|
||||
task := &Task{TaskCtx: ctx, Name: name, logFile: logFile, Logger: logger, taskRepo: taskRepo, Task: taskModel}
|
||||
return task, nil
|
||||
}
|
||||
|
||||
|
|
@ -175,16 +174,18 @@ func ReNewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task,
|
|||
return nil, fmt.Errorf("failed to create log directory: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
logPath := path.Join(global.Dir.TaskDir, taskScope, taskID+".log")
|
||||
file, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
|
||||
logger := logrus.New()
|
||||
logger.SetFormatter(&SimpleFormatter{})
|
||||
logFile, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open log file: %w", err)
|
||||
}
|
||||
writer := bufio.NewWriter(file)
|
||||
logger := log.New(file, "", log.LstdFlags)
|
||||
logger.SetOutput(logFile)
|
||||
logger.Print("\n --------------------------------------------------- \n")
|
||||
taskItem.Status = constant.StatusExecuting
|
||||
task := &Task{Name: name, logFile: file, Logger: logger, taskRepo: taskRepo, Task: &taskItem, Writer: writer}
|
||||
task := &Task{Name: name, logFile: logFile, Logger: logger, taskRepo: taskRepo, Task: &taskItem}
|
||||
task.updateTask(&taskItem)
|
||||
return task, nil
|
||||
}
|
||||
|
|
@ -353,3 +354,11 @@ func (t *Task) LogSuccessWithOps(operate, msg string) {
|
|||
func (t *Task) LogFailedWithOps(operate, msg string, err error) {
|
||||
t.Logger.Printf("%s%s%s : %s ", i18n.GetMsgByKey(operate), msg, i18n.GetMsgByKey("Failed"), err.Error())
|
||||
}
|
||||
|
||||
type SimpleFormatter struct{}
|
||||
|
||||
func (f *SimpleFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
timestamp := entry.Time.Format("2006/01/02 15:04:05")
|
||||
message := fmt.Sprintf("%s %s\n", timestamp, entry.Message)
|
||||
return []byte(message), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -243,18 +243,7 @@ func (c Client) PushImageWithProcessAndOptions(task *task.Task, imageName string
|
|||
status, _ := progress["status"].(string)
|
||||
switch status {
|
||||
case "Pushing":
|
||||
id, _ := progress["id"].(string)
|
||||
progressDetail, _ := progress["progressDetail"].(map[string]interface{})
|
||||
current, _ := progressDetail["current"].(float64)
|
||||
progressStr := ""
|
||||
total, ok := progressDetail["total"].(float64)
|
||||
if ok {
|
||||
progressStr = fmt.Sprintf("%s [%s] --- %.2f%%", status, id, (current/total)*100)
|
||||
} else {
|
||||
progressStr = fmt.Sprintf("%s [%s] --- %.2f%%", status, id, current)
|
||||
}
|
||||
|
||||
_ = setLog(id, progressStr, task)
|
||||
logProcess(progress, task)
|
||||
case "Pushed":
|
||||
id, _ := progress["id"].(string)
|
||||
progressStr := fmt.Sprintf("%s [%s] --- %.2f%%", status, id, 100.0)
|
||||
|
|
@ -298,17 +287,7 @@ func (c Client) BuildImageWithProcessAndOptions(task *task.Task, tar io.ReadClos
|
|||
}
|
||||
switch status {
|
||||
case "Downloading", "Extracting":
|
||||
id, _ := progress["id"].(string)
|
||||
progressDetail, _ := progress["progressDetail"].(map[string]interface{})
|
||||
current, _ := progressDetail["current"].(float64)
|
||||
progressStr := ""
|
||||
total, ok := progressDetail["total"].(float64)
|
||||
if ok {
|
||||
progressStr = fmt.Sprintf("%s [%s] --- %.2f%%", status, id, (current/total)*100)
|
||||
} else {
|
||||
progressStr = fmt.Sprintf("%s [%s] --- %.2f%%", status, id, current)
|
||||
}
|
||||
_ = setLog(id, progressStr, task)
|
||||
logProcess(progress, task)
|
||||
case "Pull complete", "Download complete", "Verifying Checksum":
|
||||
id, _ := progress["id"].(string)
|
||||
progressStr := fmt.Sprintf("%s [%s] --- %.2f%%", status, id, 100.0)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package task
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
|
|
@ -16,6 +15,7 @@ import (
|
|||
"github.com/1Panel-dev/1Panel/core/global"
|
||||
"github.com/1Panel-dev/1Panel/core/i18n"
|
||||
"github.com/google/uuid"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type ActionFunc func(*Task) error
|
||||
|
|
@ -24,7 +24,7 @@ type RollbackFunc func(*Task)
|
|||
type Task struct {
|
||||
Name string
|
||||
TaskID string
|
||||
Logger *log.Logger
|
||||
Logger *logrus.Logger
|
||||
SubTasks []*SubTask
|
||||
Rollbacks []RollbackFunc
|
||||
logFile *os.File
|
||||
|
|
@ -59,7 +59,7 @@ const (
|
|||
|
||||
const (
|
||||
TaskScopeSystem = "System"
|
||||
TaskScopeScript = "Script"
|
||||
TaskScopeScript = "ScriptLibrary"
|
||||
TaskScopeNodeFile = "NodeFile"
|
||||
TaskScopeAppBackup = "AppBackup"
|
||||
TaskScopeCluster = "Cluster"
|
||||
|
|
@ -85,11 +85,13 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e
|
|||
}
|
||||
}
|
||||
logPath := path.Join(logItem, taskScope, taskID+".log")
|
||||
file, err := os.OpenFile(logPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
|
||||
logger := logrus.New()
|
||||
logger.SetFormatter(&SimpleFormatter{})
|
||||
logFile, err := os.OpenFile(logPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, constant.FilePerm)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open log file: %w", err)
|
||||
}
|
||||
logger := log.New(file, "", log.LstdFlags)
|
||||
logger.SetOutput(logFile)
|
||||
taskModel := &model.Task{
|
||||
ID: taskID,
|
||||
Name: name,
|
||||
|
|
@ -100,7 +102,7 @@ func NewTask(name, operate, taskScope, taskID string, resourceID uint) (*Task, e
|
|||
Operate: operate,
|
||||
}
|
||||
taskRepo := repo.NewITaskRepo()
|
||||
task := &Task{Name: name, logFile: file, Logger: logger, taskRepo: taskRepo, Task: taskModel}
|
||||
task := &Task{Name: name, logFile: logFile, Logger: logger, taskRepo: taskRepo, Task: taskModel}
|
||||
return task, nil
|
||||
}
|
||||
|
||||
|
|
@ -263,3 +265,11 @@ func (t *Task) LogSuccessWithOps(operate, msg string) {
|
|||
func (t *Task) LogFailedWithOps(operate, msg string, err error) {
|
||||
t.Logger.Printf("%s%s%s : %s ", i18n.GetMsgByKey(operate), msg, i18n.GetMsgByKey("Failed"), err.Error())
|
||||
}
|
||||
|
||||
type SimpleFormatter struct{}
|
||||
|
||||
func (f *SimpleFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
||||
timestamp := entry.Time.Format("2006/01/02 15:04:05")
|
||||
message := fmt.Sprintf("%s %s\n", timestamp, entry.Message)
|
||||
return []byte(message), nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue