日志初步结构

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

@@ -1,21 +1,40 @@
package auth
import (
"crypto/md5"
"fmt"
"math/rand"
"strconv"
"time"
)
var myRand = rand.New(rand.NewSource(time.Now().UnixNano()))
func GenerateVerificationCode() string {
var code string
for i := 0; i < 3; i++ {
// 生成两个 10-99 之间的随机数
a := rand.Intn(90-10+1) + 10
b := rand.Intn(90-10+1) + 10
c := rand.Intn(90-10+1) + 10
a := myRand.Intn(90-10+1) + 10
b := myRand.Intn(90-10+1) + 10
c := myRand.Intn(90-10+1) + 10
// 将随机数拼接到验证码字符串
code += fmt.Sprintf("%d%d%d", a, b, c)
}
return code
}
func GenerateMD5() string {
// 将时间戳转换为字节slice
timestampBytes := []byte(strconv.FormatInt(time.Now().UnixNano(), 10))
// 计算MD5哈希
hashBytes := md5.Sum(timestampBytes)
// 将哈希值转换为十六进制字符串
hashHex := fmt.Sprintf("%x", hashBytes)
return hashHex
}

View File

@@ -8,7 +8,8 @@ type ErrorCode struct {
var (
ErrRespNotNil = &ErrorCode{Code: 10000, Message: "resp must not nil"}
ErrEncGcm = &ErrorCode{Code: 10001, Message: "gmc加密错误"}
ErrEncGcm = &ErrorCode{Code: 10001, Message: "gmc加密错误"}
ErrJSONUnMarshal = &ErrorCode{Code: 10002, Message: "json 解析错误"}
ErrParamParse = &ErrorCode{Code: 10100, Message: "参数解析错误"}

View File

@@ -1,6 +1,9 @@
package basic
import "reflect"
import (
"reflect"
"strings"
)
func GetLangString(param any) string {
// 获取参数的反射值
@@ -18,10 +21,11 @@ func GetLangString(param any) string {
// 如果字段存在并且是字符串类型
if langField.IsValid() && langField.Kind() == reflect.String {
return langField.String()
return strings.TrimSpace(langField.String())
}
}
// 如果无法获取有效的字符串值则返回zh_cn
return "zh_cn"
}

View File

@@ -37,6 +37,7 @@ func (resp *Response) ErrorErr(Code int, err error, Data ...interface{}) *Respon
resp.ErrorText = err.Error()
resp.IsSuccess = false
resp.setData(Data)
log.Error(resp.ErrorText)
return resp
}

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()