refactor: replace logger usage with common.Log for consistent logging

This commit is contained in:
krau
2025-03-21 21:07:53 +08:00
parent 65fee89e14
commit ed0837a89b
27 changed files with 152 additions and 158 deletions

View File

@@ -7,8 +7,8 @@ import (
"github.com/celestix/gotgproto/ext"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/common"
"github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/logger"
"github.com/krau/SaveAny-Bot/queue"
"github.com/krau/SaveAny-Bot/types"
)
@@ -17,17 +17,17 @@ func worker(queue *queue.TaskQueue, semaphore chan struct{}) {
for {
semaphore <- struct{}{}
task := queue.GetTask()
logger.L.Debugf("Got task: %s", task.String())
common.Log.Debugf("Got task: %s", task.String())
switch task.Status {
case types.Pending:
logger.L.Infof("Processing task: %s", task.String())
common.Log.Infof("Processing task: %s", task.String())
if err := processPendingTask(task); err != nil {
task.Error = err
if errors.Is(err, context.Canceled) {
task.Status = types.Canceled
} else {
logger.L.Errorf("Failed to do task: %s", err)
common.Log.Errorf("Failed to do task: %s", err)
task.Status = types.Failed
}
} else {
@@ -35,10 +35,10 @@ func worker(queue *queue.TaskQueue, semaphore chan struct{}) {
}
queue.AddTask(task)
case types.Succeeded:
logger.L.Infof("Task succeeded: %s", task.String())
common.Log.Infof("Task succeeded: %s", task.String())
extCtx, ok := task.Ctx.(*ext.Context)
if !ok {
logger.L.Errorf("Context is not *ext.Context: %T", task.Ctx)
common.Log.Errorf("Context is not *ext.Context: %T", task.Ctx)
} else {
extCtx.EditMessage(task.ReplyChatID, &tg.MessagesEditMessageRequest{
Message: fmt.Sprintf("文件保存成功\n [%s]: %s", task.StorageName, task.StoragePath),
@@ -46,10 +46,10 @@ func worker(queue *queue.TaskQueue, semaphore chan struct{}) {
})
}
case types.Failed:
logger.L.Errorf("Task failed: %s", task.String())
common.Log.Errorf("Task failed: %s", task.String())
extCtx, ok := task.Ctx.(*ext.Context)
if !ok {
logger.L.Errorf("Context is not *ext.Context: %T", task.Ctx)
common.Log.Errorf("Context is not *ext.Context: %T", task.Ctx)
} else {
extCtx.EditMessage(task.ReplyChatID, &tg.MessagesEditMessageRequest{
Message: "文件保存失败\n" + task.Error.Error(),
@@ -57,10 +57,10 @@ func worker(queue *queue.TaskQueue, semaphore chan struct{}) {
})
}
case types.Canceled:
logger.L.Infof("Task canceled: %s", task.String())
common.Log.Infof("Task canceled: %s", task.String())
extCtx, ok := task.Ctx.(*ext.Context)
if !ok {
logger.L.Errorf("Context is not *ext.Context: %T", task.Ctx)
common.Log.Errorf("Context is not *ext.Context: %T", task.Ctx)
} else {
extCtx.EditMessage(task.ReplyChatID, &tg.MessagesEditMessageRequest{
Message: "任务已取消",
@@ -68,16 +68,16 @@ func worker(queue *queue.TaskQueue, semaphore chan struct{}) {
})
}
default:
logger.L.Errorf("Unknown task status: %s", task.Status)
common.Log.Errorf("Unknown task status: %s", task.Status)
}
<-semaphore
logger.L.Debugf("Task done: %s; status: %s", task.String(), task.Status)
common.Log.Debugf("Task done: %s; status: %s", task.String(), task.Status)
queue.DoneTask(task)
}
}
func Run() {
logger.L.Info("Start processing tasks...")
common.Log.Info("Start processing tasks...")
semaphore := make(chan struct{}, config.Cfg.Workers)
for i := 0; i < config.Cfg.Workers; i++ {
go worker(queue.Queue, semaphore)