- OPEN_API_DEBUG 记录鉴权、查单、推送业务字段与 req_id - LOG_FILE 默认 logs/app.log,标准日志/Gin/GORM 同时输出控制台与文件
120 lines
2.1 KiB
Go
120 lines
2.1 KiB
Go
package openlog
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const (
|
|
CtxReqID = "open_req_id"
|
|
CtxDebug = "open_debug"
|
|
CtxBody = "open_body"
|
|
CtxStart = "open_start"
|
|
CtxAPIKey = "open_api_key"
|
|
)
|
|
|
|
// Enabled 是否开启开放接口详细日志
|
|
var Enabled bool
|
|
|
|
func Init(enabled bool) {
|
|
Enabled = enabled
|
|
if enabled {
|
|
log.Printf("[open] 开放接口调试日志已开启 (OPEN_API_DEBUG)")
|
|
}
|
|
}
|
|
|
|
func NewReqID() string {
|
|
return strings.ReplaceAll(uuid.NewString(), "-", "")[:12]
|
|
}
|
|
|
|
func GetReqID(c *gin.Context) string {
|
|
if v, ok := c.Get(CtxReqID); ok {
|
|
if s, ok := v.(string); ok {
|
|
return s
|
|
}
|
|
}
|
|
return "-"
|
|
}
|
|
|
|
func EnsureReqID(c *gin.Context) string {
|
|
if id := GetReqID(c); id != "-" {
|
|
return id
|
|
}
|
|
id := NewReqID()
|
|
c.Set(CtxReqID, id)
|
|
return id
|
|
}
|
|
|
|
func IsDebug(c *gin.Context) bool {
|
|
if !Enabled {
|
|
return false
|
|
}
|
|
if v, ok := c.Get(CtxDebug); ok {
|
|
if b, ok := v.(bool); ok {
|
|
return b
|
|
}
|
|
}
|
|
return Enabled
|
|
}
|
|
|
|
// MaskKey 脱敏 api_key:保留前缀与后 4 位
|
|
func MaskKey(key string) string {
|
|
if key == "" {
|
|
return "(empty)"
|
|
}
|
|
if len(key) <= 8 {
|
|
return key[:2] + "***"
|
|
}
|
|
return key[:8] + "***" + key[len(key)-4:]
|
|
}
|
|
|
|
// MaskSign 脱敏签名:只保留前 8 位
|
|
func MaskSign(sign string) string {
|
|
if sign == "" {
|
|
return "(empty)"
|
|
}
|
|
if len(sign) <= 8 {
|
|
return sign + "..."
|
|
}
|
|
return sign[:8] + "..."
|
|
}
|
|
|
|
// Truncate 截断过长字符串
|
|
func Truncate(s string, max int) string {
|
|
if max <= 0 || len(s) <= max {
|
|
return s
|
|
}
|
|
return s[:max] + fmt.Sprintf("...(%d bytes)", len(s))
|
|
}
|
|
|
|
func Info(c *gin.Context, format string, args ...interface{}) {
|
|
if !IsDebug(c) {
|
|
return
|
|
}
|
|
prefix := fmt.Sprintf("[open] req_id=%s ", GetReqID(c))
|
|
log.Printf(prefix+format, args...)
|
|
}
|
|
|
|
func Warn(c *gin.Context, format string, args ...interface{}) {
|
|
// 鉴权失败等也值得在 debug 时打出
|
|
if !Enabled {
|
|
return
|
|
}
|
|
prefix := fmt.Sprintf("[open] req_id=%s ", GetReqID(c))
|
|
log.Printf(prefix+format, args...)
|
|
}
|
|
|
|
func Elapsed(c *gin.Context) time.Duration {
|
|
if v, ok := c.Get(CtxStart); ok {
|
|
if t, ok := v.(time.Time); ok {
|
|
return time.Since(t)
|
|
}
|
|
}
|
|
return 0
|
|
}
|