|
1 | 1 | package internal |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "github.com/flipped-aurora/gin-vue-admin/server/global" |
5 | | - "go.uber.org/zap" |
6 | | - "go.uber.org/zap/zapcore" |
7 | | - "os" |
8 | | - "time" |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/flipped-aurora/gin-vue-admin/server/global" |
| 7 | + "github.com/flipped-aurora/gin-vue-admin/server/model/system" |
| 8 | + "github.com/flipped-aurora/gin-vue-admin/server/service" |
| 9 | + astutil "github.com/flipped-aurora/gin-vue-admin/server/utils/ast" |
| 10 | + "github.com/flipped-aurora/gin-vue-admin/server/utils/stacktrace" |
| 11 | + "go.uber.org/zap" |
| 12 | + "go.uber.org/zap/zapcore" |
| 13 | + "os" |
| 14 | + "strings" |
| 15 | + "time" |
9 | 16 | ) |
10 | 17 |
|
11 | 18 | type ZapCore struct { |
@@ -54,13 +61,75 @@ func (z *ZapCore) Check(entry zapcore.Entry, check *zapcore.CheckedEntry) *zapco |
54 | 61 | } |
55 | 62 |
|
56 | 63 | func (z *ZapCore) Write(entry zapcore.Entry, fields []zapcore.Field) error { |
57 | | - for i := 0; i < len(fields); i++ { |
58 | | - if fields[i].Key == "business" || fields[i].Key == "folder" || fields[i].Key == "directory" { |
59 | | - syncer := z.WriteSyncer(fields[i].String) |
60 | | - z.Core = zapcore.NewCore(global.GVA_CONFIG.Zap.Encoder(), syncer, z.level) |
61 | | - } |
62 | | - } |
63 | | - return z.Core.Write(entry, fields) |
| 64 | + for i := 0; i < len(fields); i++ { |
| 65 | + if fields[i].Key == "business" || fields[i].Key == "folder" || fields[i].Key == "directory" { |
| 66 | + syncer := z.WriteSyncer(fields[i].String) |
| 67 | + z.Core = zapcore.NewCore(global.GVA_CONFIG.Zap.Encoder(), syncer, z.level) |
| 68 | + } |
| 69 | + } |
| 70 | + // 先写入原日志目标 |
| 71 | + err := z.Core.Write(entry, fields) |
| 72 | + |
| 73 | + // 捕捉 Error 及以上级别日志并入库,且可提取 zap.Error(err) 的错误内容 |
| 74 | + if entry.Level >= zapcore.ErrorLevel { |
| 75 | + // 避免与 GORM zap 写入互相递归:跳过由 gorm logger writer 触发的日志 |
| 76 | + if strings.Contains(entry.Caller.File, "gorm_logger_writer.go") { |
| 77 | + return err |
| 78 | + } |
| 79 | + // 避免重复记录 panic 恢复日志,panic 由 GinRecovery 单独捕捉入库 |
| 80 | + if strings.Contains(entry.Message, "[Recovery from panic]") { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + form := "后端" |
| 85 | + level := entry.Level.String() |
| 86 | + // 生成基础信息 |
| 87 | + info := entry.Message |
| 88 | + |
| 89 | + // 提取 zap.Error(err) 内容 |
| 90 | + var errStr string |
| 91 | + for i := 0; i < len(fields); i++ { |
| 92 | + f := fields[i] |
| 93 | + if f.Type == zapcore.ErrorType || f.Key == "error" || f.Key == "err" { |
| 94 | + if f.Interface != nil { |
| 95 | + errStr = fmt.Sprintf("%v", f.Interface) |
| 96 | + } else if f.String != "" { |
| 97 | + errStr = f.String |
| 98 | + } |
| 99 | + break |
| 100 | + } |
| 101 | + } |
| 102 | + if errStr != "" { |
| 103 | + info = fmt.Sprintf("%s | 错误: %s", info, errStr) |
| 104 | + } |
| 105 | + |
| 106 | + // 附加来源与堆栈信息 |
| 107 | + if entry.Caller.File != "" { |
| 108 | + info = fmt.Sprintf("%s \n 源文件:%s:%d", info, entry.Caller.File, entry.Caller.Line) |
| 109 | + } |
| 110 | + stack := entry.Stack |
| 111 | + if stack != "" { |
| 112 | + info = fmt.Sprintf("%s \n 调用栈:%s", info, stack) |
| 113 | + // 解析最终业务调用方,并提取其方法源码 |
| 114 | + if frame, ok := stacktrace.FindFinalCaller(stack); ok { |
| 115 | + fnName, fnSrc, sLine, eLine, exErr := astutil.ExtractFuncSourceByPosition(frame.File, frame.Line) |
| 116 | + if exErr == nil { |
| 117 | + info = fmt.Sprintf("%s \n 最终调用方法:%s:%d (%s lines %d-%d)\n----- 产生日志的方法代码如下 -----\n%s", info, frame.File, frame.Line, fnName, sLine, eLine, fnSrc) |
| 118 | + } else { |
| 119 | + info = fmt.Sprintf("%s \n 最终调用方法:%s:%d (%s) | extract_err=%v", info, frame.File, frame.Line, fnName, exErr) |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // 使用后台上下文,避免依赖 gin.Context |
| 125 | + ctx := context.Background() |
| 126 | + _ = service.ServiceGroupApp.SystemServiceGroup.SysErrorService.CreateSysError(ctx, &system.SysError{ |
| 127 | + Form: &form, |
| 128 | + Info: &info, |
| 129 | + Level: level, |
| 130 | + }) |
| 131 | + } |
| 132 | + return err |
64 | 133 | } |
65 | 134 |
|
66 | 135 | func (z *ZapCore) Sync() error { |
|
0 commit comments