重构:优化记录格式并修复核心功能
- 修改记录存储格式为 [4B len][8B offset][4B CRC][16B UUID][data] - 修复 TopicProcessor 中 WaitGroup 使用错误导致 handler 不执行的问题 - 修复写入保护逻辑,避免 dirtyOffset=-1 时误判为写入中 - 添加统计信息定期持久化功能 - 改进 UTF-8 字符截断处理,防止 CJK 字符乱码 - 优化 Web UI:显示人类可读的文件大小,支持点击外部关闭弹窗 - 重构示例代码,添加 webui 和 webui_integration 示例 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
91
example/webui_integration/main.go
Normal file
91
example/webui_integration/main.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"code.tczkiot.com/seqlog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 创建日志目录
|
||||
baseDir := "./logs"
|
||||
if err := os.MkdirAll(baseDir, 0755); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// 创建 LogHub
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: slog.LevelInfo,
|
||||
}))
|
||||
|
||||
handler := func(topic string, record *seqlog.Record) error {
|
||||
fmt.Printf("[%s] 处理记录: %s\n", topic, string(record.Data))
|
||||
return nil
|
||||
}
|
||||
|
||||
hub := seqlog.NewLogHub(baseDir, logger, handler)
|
||||
|
||||
// 启动 LogHub
|
||||
if err := hub.Start(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer hub.Stop()
|
||||
|
||||
// 创建自己的 ServeMux
|
||||
mux := http.NewServeMux()
|
||||
|
||||
// 注册自己的业务端点
|
||||
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
fmt.Fprintf(w, `{"status":"ok","service":"my-app"}`)
|
||||
})
|
||||
|
||||
mux.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
fmt.Fprintf(w, "# HELP my_app_requests_total Total requests\n")
|
||||
fmt.Fprintf(w, "my_app_requests_total 12345\n")
|
||||
})
|
||||
|
||||
// 在 /logs 路径下集成 seqlog Web UI
|
||||
// 方法 1:使用子路径(需要创建一个包装 ServeMux)
|
||||
logsMux := http.NewServeMux()
|
||||
if err := hub.RegisterWebUIRoutes(logsMux); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
mux.Handle("/logs/", http.StripPrefix("/logs", logsMux))
|
||||
|
||||
// 启动模拟写日志
|
||||
go func() {
|
||||
topics := []string{"app", "system", "access"}
|
||||
count := 0
|
||||
ticker := time.NewTicker(500 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
count++
|
||||
topic := topics[count%len(topics)]
|
||||
message := fmt.Sprintf("日志消息 %d - %s", count, time.Now().Format(time.RFC3339))
|
||||
|
||||
if _, err := hub.Write(topic, []byte(message)); err != nil {
|
||||
fmt.Printf("写入失败: %v\n", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// 启动服务器
|
||||
fmt.Println("\n==== Seqlog 集成示例 ====")
|
||||
fmt.Println("业务端点:")
|
||||
fmt.Println(" - http://localhost:8080/health")
|
||||
fmt.Println(" - http://localhost:8080/metrics")
|
||||
fmt.Println("Web UI:")
|
||||
fmt.Println(" - http://localhost:8080/logs/")
|
||||
fmt.Println("按 Ctrl+C 退出")
|
||||
|
||||
if err := http.ListenAndServe(":8080", mux); err != nil {
|
||||
fmt.Printf("服务器错误: %v\n", err)
|
||||
}
|
||||
}
|
||||
BIN
example/webui_integration/webui_integration
Executable file
BIN
example/webui_integration/webui_integration
Executable file
Binary file not shown.
Reference in New Issue
Block a user