This commit is contained in:
1 parent
b50e5bdfa2
commit
dd6258c98b
34 files changed
+734
-87
No files matched your search
@@ -149,17 +149,19 @@ func (l *logFormatter) Format(entry *log.Entry) ([]byte, error) {
|
||||
b.WriteString(fmt.Sprintf("[%s]", entry.Level.String()))
|
||||
b.WriteString(fmt.Sprintf("[%s]", entry.Time.Format("2006-01-02 15:04:05")))
|
||||
if entry.Context != nil {
|
||||
ctx := entry.Context.(*context.Context)
|
||||
if ctx != nil {
|
||||
ctx, ok := entry.Context.(*context.Context)
|
||||
if ok && ctx != nil {
|
||||
b.WriteString(fmt.Sprintf("[%s]", ctx.GetValue(context.LogID)))
|
||||
}
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("[%s:%d]", entry.Caller.File, entry.Caller.Line))
|
||||
if entry.Caller != nil {
|
||||
b.WriteString(fmt.Sprintf("[%s:%d]", entry.Caller.File, entry.Caller.Line))
|
||||
}
|
||||
b.WriteString(entry.Message)
|
||||
// 错误字段也是有效内容,不能在消息为空时被格式器丢弃。
|
||||
if len(entry.Data) > 0 {
|
||||
fields := make(map[string]any, len(entry.Data))
|
||||
for key, value := range entry.Data {
|
||||
{
|
||||
fields := runtimeEventFields(entry)
|
||||
for key, value := range fields {
|
||||
if cause, ok := value.(error); ok {
|
||||
fields[key] = cause.Error()
|
||||
} else if _, err := json.Marshal(value); err != nil {
|
||||
@@ -172,7 +174,8 @@ func (l *logFormatter) Format(entry *log.Entry) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b.WriteByte(' ')
|
||||
// 固定边界使页面不必把消息中恰好出现的 JSON 当作可信事件元数据。
|
||||
b.WriteString(" @event ")
|
||||
b.Write(payload)
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,8 @@ func (RuntimeConfigLogWriter) WriteConfigAudit(_ context.Context, event ConfigAu
|
||||
ConfigOperationMigration: "迁移配置",
|
||||
}[event.Operation]
|
||||
entry := log.WithFields(log.Fields{
|
||||
"action": action,
|
||||
"object": event.Section,
|
||||
"operation": event.Operation,
|
||||
"section": event.Section,
|
||||
"actorId": event.ActorID,
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// runtimeEventFields 为文件日志提供稳定的事件边界;没有完成信号时只记为状态记录。
|
||||
// 不从任意消息中的“成功”或日志级别猜测业务结果,也不改变调用方的字段。
|
||||
func runtimeEventFields(entry *log.Entry) map[string]any {
|
||||
fields := make(map[string]any, len(entry.Data)+4)
|
||||
for key, value := range entry.Data {
|
||||
fields[key] = value
|
||||
}
|
||||
fields["logSchema"] = 1
|
||||
if action, ok := fields["action"].(string); !ok || strings.TrimSpace(action) == "" {
|
||||
action = "运行事件"
|
||||
if entry.Caller != nil && entry.Caller.Function != "" {
|
||||
function := entry.Caller.Function
|
||||
if index := strings.LastIndex(function, "/"); index >= 0 {
|
||||
function = function[index+1:]
|
||||
}
|
||||
action += " · " + function
|
||||
}
|
||||
fields["action"] = action
|
||||
}
|
||||
if result, ok := fields["result"].(string); !ok || strings.TrimSpace(result) == "" {
|
||||
fields["result"] = "recorded"
|
||||
}
|
||||
return fields
|
||||
}
|
||||
Reference in new issue
Block a user