Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
956cf69a82 | |||
61a115852e | |||
bb5dd2583d |
@ -2,17 +2,20 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"gitea.loveuer.com/yizhisec/packages/opt"
|
|
||||||
uuid2 "github.com/google/uuid"
|
uuid2 "github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type _traceId struct{}
|
||||||
|
|
||||||
|
var TraceId = _traceId{}
|
||||||
|
|
||||||
func traceId(ctx context.Context) string {
|
func traceId(ctx context.Context) string {
|
||||||
if ctx == nil {
|
if ctx == nil {
|
||||||
uuid, _ := uuid2.NewV7()
|
uuid, _ := uuid2.NewV7()
|
||||||
return uuid.String()
|
return uuid.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
if id, _ := ctx.Value(opt.TraceKey).(string); id != "" {
|
if id, _ := ctx.Value(TraceId).(string); id != "" {
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,30 +24,30 @@ func traceId(ctx context.Context) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DebugCtx(ctx context.Context, msg string, data ...any) {
|
func DebugCtx(ctx context.Context, msg string, data ...any) {
|
||||||
msg = "[" + traceId(ctx) + "] " + msg
|
msg = traceId(ctx) + " | " + msg
|
||||||
DefaultLogger.Debug(msg, data...)
|
DefaultLogger.Debug(msg, data...)
|
||||||
}
|
}
|
||||||
func InfoCtx(ctx context.Context, msg string, data ...any) {
|
func InfoCtx(ctx context.Context, msg string, data ...any) {
|
||||||
msg = "[" + traceId(ctx) + "] " + msg
|
msg = traceId(ctx) + " | " + msg
|
||||||
DefaultLogger.Info(msg, data...)
|
DefaultLogger.Info(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func WarnCtx(ctx context.Context, msg string, data ...any) {
|
func WarnCtx(ctx context.Context, msg string, data ...any) {
|
||||||
msg = "[" + traceId(ctx) + "] " + msg
|
msg = traceId(ctx) + " | " + msg
|
||||||
DefaultLogger.Warn(msg, data...)
|
DefaultLogger.Warn(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ErrorCtx(ctx context.Context, msg string, data ...any) {
|
func ErrorCtx(ctx context.Context, msg string, data ...any) {
|
||||||
msg = "[" + traceId(ctx) + "] " + msg
|
msg = traceId(ctx) + " | " + msg
|
||||||
DefaultLogger.Error(msg, data...)
|
DefaultLogger.Error(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func PanicCtx(ctx context.Context, msg string, data ...any) {
|
func PanicCtx(ctx context.Context, msg string, data ...any) {
|
||||||
msg = "[" + traceId(ctx) + "] " + msg
|
msg = traceId(ctx) + " | " + msg
|
||||||
DefaultLogger.Panic(msg, data...)
|
DefaultLogger.Panic(msg, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FatalCtx(ctx context.Context, msg string, data ...any) {
|
func FatalCtx(ctx context.Context, msg string, data ...any) {
|
||||||
msg = "[" + traceId(ctx) + "] " + msg
|
msg = traceId(ctx) + " | " + msg
|
||||||
DefaultLogger.Fatal(msg, data...)
|
DefaultLogger.Fatal(msg, data...)
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"gitea.loveuer.com/yizhisec/packages/opt"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -11,7 +10,7 @@ func TestCtxLog(t *testing.T) {
|
|||||||
InfoCtx(nil, "hello %s", "world")
|
InfoCtx(nil, "hello %s", "world")
|
||||||
WarnCtx(context.Background(), "hello %s", "world")
|
WarnCtx(context.Background(), "hello %s", "world")
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx = context.WithValue(ctx, opt.TraceKey, "value")
|
ctx = context.WithValue(ctx, TraceId, "value")
|
||||||
SetLogLevel(LogLevelDebug)
|
SetLogLevel(LogLevelDebug)
|
||||||
DebugCtx(ctx, "hello %s", "world")
|
DebugCtx(ctx, "hello %s", "world")
|
||||||
ErrorCtx(ctx, "hello %s", "world")
|
ErrorCtx(ctx, "hello %s", "world")
|
||||||
|
20
resp/resp.go
20
resp/resp.go
@ -30,6 +30,18 @@ func R200(c *gin.Context, data any, msgs ...string) {
|
|||||||
c.AbortWithStatusJSON(200, r)
|
c.AbortWithStatusJSON(200, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RC(c *gin.Context, status int, args ...any) {
|
||||||
|
code := 1
|
||||||
|
if status != 200 {
|
||||||
|
code = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
_r(c, &res{
|
||||||
|
Status: status,
|
||||||
|
Code: code,
|
||||||
|
}, args...)
|
||||||
|
}
|
||||||
|
|
||||||
func RE(c *gin.Context, err error) {
|
func RE(c *gin.Context, err error) {
|
||||||
var re *Error
|
var re *Error
|
||||||
|
|
||||||
@ -68,11 +80,7 @@ H4:
|
|||||||
r.Code = code
|
r.Code = code
|
||||||
}
|
}
|
||||||
H3:
|
H3:
|
||||||
if es, ok := args[2].(error); ok {
|
|
||||||
r.Err = es.Error()
|
|
||||||
} else {
|
|
||||||
r.Err = args[2]
|
r.Err = args[2]
|
||||||
}
|
|
||||||
H2:
|
H2:
|
||||||
r.Data = args[1]
|
r.Data = args[1]
|
||||||
H1:
|
H1:
|
||||||
@ -86,6 +94,10 @@ END:
|
|||||||
r.Msg = Msg(r.Status)
|
r.Msg = Msg(r.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ce, ok := r.Err.(error); ok {
|
||||||
|
r.Err = ce.Error()
|
||||||
|
}
|
||||||
|
|
||||||
c.AbortWithStatusJSON(r.Status, r)
|
c.AbortWithStatusJSON(r.Status, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user