完善非成功请求的日志处理

This commit is contained in:
2025-05-09 15:06:22 +08:00
parent 7c684ca0ad
commit 189f523693
2 changed files with 18 additions and 12 deletions

View File

@@ -1,7 +1,5 @@
package auth package auth
import "platform/pkg/u"
// Context 定义认证信息 // Context 定义认证信息
type Context struct { type Context struct {
Payload Payload `json:"payload"` Payload Payload `json:"payload"`
@@ -39,8 +37,9 @@ type Agent struct {
type PayloadType int type PayloadType int
const ( const (
PayloadNone PayloadType = iota
// PayloadUser 用户类型 // PayloadUser 用户类型
PayloadUser PayloadType = iota + 1 PayloadUser
// PayloadAdmin 管理员类型 // PayloadAdmin 管理员类型
PayloadAdmin PayloadAdmin
// PayloadClientPublic 公共客户端类型 // PayloadClientPublic 公共客户端类型
@@ -59,20 +58,22 @@ func (t PayloadType) ToStr() string {
return "cpub" return "cpub"
case PayloadClientConfidential: case PayloadClientConfidential:
return "ccnf" return "ccnf"
} default:
return "none" return "none"
} }
}
func PayloadTypeFromStr(name string) *PayloadType { func PayloadTypeFromStr(name string) PayloadType {
switch name { switch name {
case "user": case "user":
return u.P(PayloadUser) return PayloadUser
case "admn": case "admn":
return u.P(PayloadAdmin) return PayloadAdmin
case "cpub": case "cpub":
return u.P(PayloadClientPublic) return PayloadClientPublic
case "ccnf": case "ccnf":
return u.P(PayloadClientConfidential) return PayloadClientConfidential
default:
return PayloadNone
} }
return nil
} }

View File

@@ -114,6 +114,11 @@ func useLogger() fiber.Handler {
Format: "🚀 ${time} | ${locals:authtype} ${locals:authid} | ${method} ${path} | ${status} | ${latency} | ${error}\n", Format: "🚀 ${time} | ${locals:authtype} ${locals:authid} | ${method} ${path} | ${status} | ${latency} | ${error}\n",
TimeFormat: "2006-01-02 15:04:05", TimeFormat: "2006-01-02 15:04:05",
TimeZone: "Asia/Shanghai", TimeZone: "Asia/Shanghai",
Next: func(c *fiber.Ctx) bool {
c.Locals("authtype", auth.PayloadNone.ToStr())
c.Locals("authid", 0)
return false
},
Done: func(c *fiber.Ctx, logBytes []byte) { Done: func(c *fiber.Ctx, logBytes []byte) {
var logStr = strings.TrimPrefix(string(logBytes), "🚀") var logStr = strings.TrimPrefix(string(logBytes), "🚀")
var logVars = strings.Split(logStr, "|") var logVars = strings.Split(logStr, "|")
@@ -147,8 +152,8 @@ func useLogger() fiber.Handler {
Error: errStr, Error: errStr,
Time: core.LocalDateTime(reqTime), Time: core.LocalDateTime(reqTime),
} }
if authType != nil { if authType != auth.PayloadNone {
item.Identity = int32(*authType) item.Identity = int32(authType)
} }
if authID != 0 { if authID != 0 {
item.Visitor = int32(authID) item.Visitor = int32(authID)