| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package audit
- import (
- "context"
- "encoding/json"
- "fmt"
- "os"
- udeModel "udeServer/internal/model"
- )
- // AsyncInsertLog inserts the log entry in a background goroutine. When the
- // insert fails, the entry is appended to log_fallback.jsonl so the audit
- // chain never silently loses data.
- func AsyncInsertLog(logModel udeModel.TSystemLogModel, entry *udeModel.TSystemLog) {
- go func(e *udeModel.TSystemLog) {
- defer func() {
- if rec := recover(); rec != nil {
- fmt.Printf("panic in log insert: %v\n", rec)
- }
- }()
- if _, insErr := logModel.Insert(context.Background(), e); insErr != nil {
- fmt.Printf("system log insert failed: %v\n", insErr)
- data, mErr := json.Marshal(e)
- if mErr != nil {
- fmt.Printf("marshal fallback log error: %v\n", mErr)
- return
- }
- // JSON Line格式
- f, fErr := os.OpenFile("log_fallback.jsonl", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
- if fErr != nil {
- fmt.Printf("open fallback file error: %v\n", fErr)
- return
- }
- defer func() {
- if cErr := f.Close(); cErr != nil {
- fmt.Printf("close fallback file error: %v\n", cErr)
- }
- }()
- if _, wErr := f.WriteString(string(data) + "\n"); wErr != nil {
- fmt.Printf("write fallback log error: %v\n", wErr)
- }
- }
- }(entry)
- }
|