日志初步结构

This commit is contained in:
2024-04-15 16:08:17 +08:00
parent 16967381d8
commit de7cd23deb
16 changed files with 414 additions and 68 deletions

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"runtime"
"strings"
"sync"
@@ -13,14 +14,13 @@ import (
)
type levelSkip struct {
Skip int
Once sync.Once
SkipLogrus int
SkipBasic int
}
// JSONFormatter formats logs into parsable json
type JSONFormatter struct {
skip []*levelSkip
once sync.Once
}
@@ -28,36 +28,79 @@ type JSONFormatter struct {
func (h *JSONFormatter) Format(e *logrus.Entry) ([]byte, error) {
skipOnce := h.skip[int(e.Level)]
skipOnce.Once.Do(func() {
for i := 4; i < 100; i++ {
if skipOnce.SkipLogrus == 0 {
var skipLogrus OpenClose
for i := 4; i < 50; i++ {
// log.Println(i)
if pc, _, _, ok := runtime.Caller(i); ok {
if pc, file, line, ok := runtime.Caller(i); ok {
funcStruct := runtime.FuncForPC(pc)
// log.Println(funcStruct.Name(), file, line)
if !strings.Contains(funcStruct.Name(), "github.com/sirupsen/logrus.") {
skipOnce.Skip++
if skipOnce.Skip >= 2 {
skipOnce.Skip = i - 3
break
}
}
log.Println(funcStruct.Name(), file, line)
skipLogrus.OpenFunc(func() bool {
return strings.Contains(funcStruct.Name(), "github.com/sirupsen/logrus.")
}, func() {
// skip = i
skipOnce.SkipLogrus = i
i = 10000
})
} else {
break
}
}
})
}
if skipOnce.SkipBasic == 0 {
var skipBasic OpenClose
for i := 4; i < 50; i++ {
// log.Println(i)
if pc, file, line, ok := runtime.Caller(i); ok {
funcStruct := runtime.FuncForPC(pc)
log.Println(funcStruct.Name(), file, line)
skipBasic.OpenFunc(func() bool {
return strings.Contains(funcStruct.Name(), "basic.(*Response).")
}, func() {
skipOnce.SkipBasic = i + 1
})
} else {
break
}
}
}
var fileinfo string
if _, file, line, ok := runtime.Caller(skipOnce.Skip); ok {
if e.Level == logrus.InfoLevel {
fileinfo = fmt.Sprintf("%s:%d", file, line)
} else {
ps := strings.Split(file, "/")
ps = ps[len(ps)-4:]
fileinfo = fmt.Sprintf("%s:%d", strings.Join(ps, "/"), line)
if pc, _, _, ok := runtime.Caller(skipOnce.SkipBasic - 1); ok {
funcStruct := runtime.FuncForPC(pc)
log.Println(funcStruct.Name())
if strings.Contains(funcStruct.Name(), "basic.(*Response).") {
if _, file, line, ok := runtime.Caller(skipOnce.SkipBasic); ok {
if e.Level == logrus.InfoLevel {
fileinfo = fmt.Sprintf("%s:%d", file, line)
} else {
ps := strings.Split(file, "/")
// ps = ps[len(ps)-4:]
fileinfo = fmt.Sprintf("%s:%d", strings.Join(ps, "/"), line)
}
}
}
}
if fileinfo != "" {
if _, file, line, ok := runtime.Caller(skipOnce.SkipLogrus); ok {
if e.Level == logrus.InfoLevel {
fileinfo = fmt.Sprintf("%s:%d", file, line)
} else {
ps := strings.Split(file, "/")
// ps = ps[len(ps)-4:]
fileinfo = fmt.Sprintf("%s:%d", strings.Join(ps, "/"), line)
}
}
}
var Data map[string]any = make(map[string]any, 4)

View File

@@ -37,7 +37,6 @@ func init() {
}
type SkipHook struct {
autoSkip int
Formatter func(*logrus.Hook, *logrus.Entry) error
once sync.Once
}
@@ -46,28 +45,61 @@ func (h *SkipHook) Levels() []logrus.Level {
return logrus.AllLevels
}
// OpenClose 开闭空间
type OpenClose struct {
Open bool
Close bool
Skip int
}
func (oc *OpenClose) OpenFunc(opendo func() bool, closedo func()) {
if oc.Open && oc.Close {
return
}
if opendo() {
if !oc.Open {
oc.Open = true
return
}
} else {
if oc.Open && !oc.Close {
oc.Close = true
closedo()
}
}
}
func (h *SkipHook) Fire(e *logrus.Entry) error {
h.once.Do(func() {
for i := 4; i < 100; i++ {
log.Println(i)
if pc, file, line, ok := runtime.Caller(i); ok {
funcStruct := runtime.FuncForPC(pc)
log.Println(funcStruct.Name(), file, line)
if !strings.Contains(funcStruct.Name(), "github.com/sirupsen/logrus.") {
h.autoSkip++
if h.autoSkip >= 2 {
h.autoSkip = i - 3
break
}
}
} else {
break
}
var skipLogrus, skipBasic OpenClose
var skip int
for i := 4; i < 25; i++ {
log.Println(i)
if pc, file, line, ok := runtime.Caller(i); ok {
funcStruct := runtime.FuncForPC(pc)
log.Println(funcStruct.Name(), file, line)
skipLogrus.OpenFunc(func() bool {
return strings.Contains(funcStruct.Name(), "github.com/sirupsen/logrus.")
}, func() {
skip = i + 1
})
skipBasic.OpenFunc(func() bool {
return strings.Contains(funcStruct.Name(), "basic.(*Response).")
}, func() {
skip = i
i = 100
})
}
})
if _, file, line, ok := runtime.Caller(h.autoSkip); ok {
}
if _, file, line, ok := runtime.Caller(skip); ok {
// funcStruct := runtime.FuncForPC(pc)
// log.Println(file, line, funcStruct.Name())
// funcName := funcStruct.Name()