Skip to content

BillionMail gives you open-source MailServer, NewsLetter, Email Marketing — fully self-hosted, dev-friendly, and free from monthly fees. Join the discord: https://discord.gg/asfXzBUhZr

Obtain the latest 1,000 lines of error logs

lotk19440d774a5b
diff --git a/core/api/operation_log/operation_log.go b/core/api/operation_log/operation_log.go
index e0834fc4..6872e4ab 100644
--- a/core/api/operation_log/operation_log.go
+++ b/core/api/operation_log/operation_log.go
@@ -14,4 +14,5 @@ type IOperationLogV1 interface {
GetOperationLog(ctx context.Context, req *v1.GetOperationLogReq) (res *v1.GetOperationLogRes, err error)
GetOperationType(ctx context.Context, req *v1.GetOperationTypeReq) (res *v1.GetOperationTypeRes, err error)
GetOutputLog(ctx context.Context, req *v1.GetOutputLogReq) (res *v1.GetOutputLogRes, err error)
+ GetLatestOutputLog(ctx context.Context, req *v1.GetLatestOutputLogReq) (res *v1.GetLatestOutputLogRes, err error)
}
diff --git a/core/api/operation_log/v1/operation_log.go b/core/api/operation_log/v1/operation_log.go
index 7e9b51ea..9dbfb75f 100644
--- a/core/api/operation_log/v1/operation_log.go
+++ b/core/api/operation_log/v1/operation_log.go
@@ -38,8 +38,17 @@ type GetOutputLogReq struct {
StartDate string `json:"start_date" v:"required" dc:"Start date(YYYY-MM-DD)"`
EndDate string `json:"end_date" v:"required" dc:"End date(YYYY-MM-DD)"`
Keyword string `json:"keyword" dc:"keyword"`
+ Page int `json:"page" v:"min:1" dc:"Page Number" d:"1"`
+ PageSize int `json:"page_size" dc:"page size"`
}
type GetOutputLogRes struct {
api_v1.StandardRes
}
+
+type GetLatestOutputLogReq struct {
+ g.Meta `path:"/operation_log/output/latest" method:"get" tags:"Output Log" summary:"Get latest output log"`
+}
+type GetLatestOutputLogRes struct {
+ api_v1.StandardRes
+}
diff --git a/core/internal/controller/operation_log/operation_log_v1_get_latest_output_log.go b/core/internal/controller/operation_log/operation_log_v1_get_latest_output_log.go
new file mode 100644
index 00000000..d37ca90b
--- /dev/null
+++ b/core/internal/controller/operation_log/operation_log_v1_get_latest_output_log.go
@@ -0,0 +1,80 @@
+package operation_log
+
+import (
+ "billionmail-core/api/operation_log/v1"
+ "billionmail-core/internal/service/public"
+ "context"
+ "errors"
+ "os"
+ "path/filepath"
+ "sort"
+ "strings"
+)
+
+const latestLogLinesLimit = 1000
+
+func (c *ControllerV1) GetLatestOutputLog(ctx context.Context, req *v1.GetLatestOutputLogReq) (res *v1.GetLatestOutputLogRes, err error) {
+ res = &v1.GetLatestOutputLogRes{}
+
+ files, err := getSortedLogFiles()
+ if err != nil {
+ res.SetError(errors.New(public.LangCtx(ctx, "No log files found")))
+ return res, nil
+ }
+
+ var collectedLines []string
+
+ for _, file := range files {
+
+ lines, readErr := readLines(file, "")
+ if readErr != nil {
+ continue
+ }
+
+ for j := len(lines) - 1; j >= 0; j-- {
+ collectedLines = append(collectedLines, lines[j])
+
+ if len(collectedLines) >= latestLogLinesLimit {
+ goto endLoop
+ }
+ }
+ }
+
+endLoop:
+
+ reverseLines(collectedLines)
+
+ merged := strings.Join(collectedLines, "\n")
+
+ res.SetSuccess(public.LangCtx(ctx, "Success"))
+ res.Data = merged
+ return res, nil
+}
+
+func getSortedLogFiles() ([]string, error) {
+ logDir := public.AbsPath("../logs/core/out")
+ entries, err := os.ReadDir(logDir)
+ if err != nil {
+ return nil, err
+ }
+
+ var fileNames []string
+ for _, entry := range entries {
+ if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".log") {
+ fileNames = append(fileNames, entry.Name())
+ }
+ }
+
+ if len(fileNames) == 0 {
+ return nil, errors.New("no log files found")
+ }
+
+ sort.Sort(sort.Reverse(sort.StringSlice(fileNames)))
+
+ var paths []string
+ for _, file := range fileNames {
+ paths = append(paths, filepath.Join(logDir, file))
+ }
+
+ return paths, nil
+}
diff --git a/core/internal/controller/operation_log/operation_log_v1_get_output_log.go b/core/internal/controller/operation_log/operation_log_v1_get_output_log.go
index 4cc0af71..36e4ccbc 100644
--- a/core/internal/controller/operation_log/operation_log_v1_get_output_log.go
+++ b/core/internal/controller/operation_log/operation_log_v1_get_output_log.go
@@ -4,6 +4,7 @@ import (
"billionmail-core/internal/service/public"
"bufio"
"context"
+ "errors"
"fmt"
"os"
"path/filepath"
@@ -14,63 +15,99 @@ import (
)
func (c *ControllerV1) GetOutputLog(ctx context.Context, req *v1.GetOutputLogReq) (res *v1.GetOutputLogRes, err error) {
+ res = &v1.GetOutputLogRes{}
+
files, err := getLogFilesInRange(ctx, req.StartDate, req.EndDate)
if err != nil {
- return nil, err
+ res.SetError(errors.New(public.LangCtx(ctx, "No log files found in the given date range")))
+ return res, nil
}
+
keyword := strings.TrimSpace(req.Keyword)
+ page := req.Page
+ pageSize := req.PageSize
+ if page < 1 {
+ page = 1
+ }
+ if pageSize <= 0 {
+
+ pageSize = 1000
+ }
+
+ var collectedLines []string
+ var linesToSkip int
+ if pageSize > 0 {
+ linesToSkip = (page - 1) * pageSize
+ }
- var allLines []string
-
- // // Reverse order
- // for i := len(files) - 1; i >= 0; i-- {
- // file := files[i]
- // f, err := os.Open(file)
- // if err != nil {
- // continue
- // }
- // var lines []string
- // scanner := bufio.NewScanner(f)
- // for scanner.Scan() {
- // line := scanner.Text()
- // if keyword == "" || strings.Contains(line, keyword) {
- // lines = append(lines, line)
- // }
- // }
- // f.Close()
- // for j := len(lines) - 1; j >= 0; j-- {
- // allLines = append(allLines, lines[j])
- // }
- // }
-
- // Normal order
- for _, file := range files {
- f, err := os.Open(file)
- if err != nil {
+ for i := len(files) - 1; i >= 0; i-- {
+ file := files[i]
+
+ lines, readErr := readLines(file, keyword)
+ if readErr != nil {
+ continue
+ }
+
+ if pageSize <= 0 {
+
+ collectedLines = append(lines, collectedLines...)
continue
}
- scanner := bufio.NewScanner(f)
- for scanner.Scan() {
- line := scanner.Text()
- if keyword == "" || strings.Contains(line, keyword) {
- allLines = append(allLines, line)
+
+ for j := len(lines) - 1; j >= 0; j-- {
+
+ if linesToSkip > 0 {
+ linesToSkip--
+ continue
+ }
+
+ collectedLines = append(collectedLines, lines[j])
+
+ if len(collectedLines) >= pageSize {
+ goto endLoop
}
}
- f.Close()
}
- merged := strings.Join(allLines, "\n")
+endLoop:
+
+ reverseLines(collectedLines)
+
+ merged := strings.Join(collectedLines, "\n")
- res = &v1.GetOutputLogRes{}
res.SetSuccess(public.LangCtx(ctx, "Success"))
res.Data = merged
+ return res, nil
+}
- return
+func readLines(path, keyword string) ([]string, error) {
+ file, err := os.Open(path)
+ if err != nil {
+ return nil, err
+ }
+ defer file.Close()
+
+ var lines []string
+ scanner := bufio.NewScanner(file)
+ for scanner.Scan() {
+ line := scanner.Text()
+ if keyword == "" || strings.Contains(line, keyword) {
+ lines = append(lines, line)
+ }
+ }
+ return lines, scanner.Err()
+}
+
+func reverseLines(lines []string) {
+ for i, j := 0, len(lines)-1; i < j; i, j = i+1, j-1 {
+ lines[i], lines[j] = lines[j], lines[i]
+ }
}
func getLogFilesInRange(ctx context.Context, start, end string) ([]string, error) {
- var logDir = public.HostWorkDir + "/logs/core/out"
+ logDir := public.AbsPath("../logs/core/out")
layout := "2006-01-02"
+
startDate, err := time.Parse(layout, start)
if err != nil {
return nil, fmt.Errorf("invalid start_date: %v", err)
@@ -84,6 +121,7 @@ func getLogFilesInRange(ctx context.Context, start, end string) ([]string, error
}
var files []string
+
for d := startDate; !d.After(endDate); d = d.AddDate(0, 0, 1) {
fname := filepath.Join(logDir, d.Format("2006-01-02")+".log")
if _, err := os.Stat(fname); err == nil {
@@ -92,7 +130,7 @@ func getLogFilesInRange(ctx context.Context, start, end string) ([]string, error
}
if len(files) == 0 {
- return nil, fmt.Errorf("no log files found in the given date range")
+ return nil, fmt.Errorf("no log files found")
}
return files, nil
}
diff --git a/core/internal/service/batch_mail/task_executor.go b/core/internal/service/batch_mail/task_executor.go
index 70d2fbfb..99914e58 100644
--- a/core/internal/service/batch_mail/task_executor.go
+++ b/core/internal/service/batch_mail/task_executor.go
@@ -15,7 +15,6 @@ import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"github.com/panjf2000/ants/v2"
- "regexp"
"strings"
"sync"
"sync/atomic"