sdk shipping 日志修改
This commit is contained in:
parent
e395e64b17
commit
2e0ebbe228
@ -7,7 +7,6 @@ import (
|
||||
"backend/sdk/login/model/tuyou"
|
||||
"backend/util"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
@ -49,7 +48,7 @@ func init() {
|
||||
currFile, err := os.OpenFile("./log/login.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
// 如果打开失败,仍然继续使用轮转器+控制台
|
||||
logStructured("warn", "failed to open current log file", map[string]any{"error": err.Error()})
|
||||
util.LogStructured("warn", "failed to open current log file", map[string]any{"error": err.Error()})
|
||||
logWriter = io.MultiWriter(rl, os.Stdout)
|
||||
errWriter = io.MultiWriter(rl, os.Stderr)
|
||||
} else {
|
||||
@ -68,29 +67,12 @@ func init() {
|
||||
go util.MonitorServerList()
|
||||
}
|
||||
|
||||
func logStructured(level string, message string, fields map[string]any) {
|
||||
payload := map[string]any{
|
||||
"level": level,
|
||||
"msg": message,
|
||||
"time": time.Now().Format(time.RFC3339),
|
||||
}
|
||||
for key, value := range fields {
|
||||
payload[key] = value
|
||||
}
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
log.Printf("level=%s msg=%q marshal_error=%v", level, message, err)
|
||||
return
|
||||
}
|
||||
log.Print(string(data))
|
||||
}
|
||||
|
||||
func requestLogger() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
startedAt := time.Now()
|
||||
c.Next()
|
||||
|
||||
logStructured("info", "http request", map[string]any{
|
||||
util.LogStructured("info", "http request", map[string]any{
|
||||
"clientIp": c.ClientIP(),
|
||||
"latencyMs": time.Since(startedAt).Milliseconds(),
|
||||
"method": c.Request.Method,
|
||||
@ -104,7 +86,7 @@ func requestLogger() gin.HandlerFunc {
|
||||
|
||||
func recoveryLogger() gin.HandlerFunc {
|
||||
return gin.CustomRecovery(func(c *gin.Context, recovered any) {
|
||||
logStructured("error", "panic recovered", map[string]any{
|
||||
util.LogStructured("error", "panic recovered", map[string]any{
|
||||
"clientIp": c.ClientIP(),
|
||||
"method": c.Request.Method,
|
||||
"path": c.Request.URL.Path,
|
||||
@ -141,7 +123,7 @@ func main() {
|
||||
|
||||
serverErr := make(chan error, 1)
|
||||
go func() {
|
||||
logStructured("info", "login sdk started", map[string]any{
|
||||
util.LogStructured("info", "login sdk started", map[string]any{
|
||||
"port": logincommon.AppConf.Port,
|
||||
"version": logincommon.AppConf.Version,
|
||||
})
|
||||
@ -154,18 +136,18 @@ func main() {
|
||||
select {
|
||||
case err, ok := <-serverErr:
|
||||
if ok && err != nil {
|
||||
logStructured("error", "login sdk start failed", map[string]any{"error": err.Error()})
|
||||
util.LogStructured("error", "login sdk start failed", map[string]any{"error": err.Error()})
|
||||
os.Exit(1)
|
||||
}
|
||||
case <-shutdownCtx.Done():
|
||||
logStructured("info", "login sdk shutting down", map[string]any{"reason": shutdownCtx.Err().Error()})
|
||||
util.LogStructured("info", "login sdk shutting down", map[string]any{"reason": shutdownCtx.Err().Error()})
|
||||
}
|
||||
|
||||
gracefulCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := server.Shutdown(gracefulCtx); err != nil {
|
||||
logStructured("error", "login sdk shutdown failed", map[string]any{"error": err.Error()})
|
||||
util.LogStructured("error", "login sdk shutdown failed", map[string]any{"error": err.Error()})
|
||||
os.Exit(1)
|
||||
}
|
||||
logStructured("info", "login sdk stopped", nil)
|
||||
util.LogStructured("info", "login sdk stopped", nil)
|
||||
}
|
||||
|
||||
@ -46,7 +46,13 @@ func (p *Param) ChangeOrderStatus(Platform string, prodprice string) error {
|
||||
log.Print("query order error:", err, p.OrderId)
|
||||
return err
|
||||
}
|
||||
log.Printf("订单金额:数据库金额:%.2f,回调金额:%s; 订单id:%s, chargeid:%d", price, prodprice, p.OrderId, chargeId)
|
||||
util.LogStructured("info", "order_info", map[string]any{
|
||||
"orderId": p.OrderId,
|
||||
"price": price,
|
||||
"prodprice": prodprice,
|
||||
"chargeId": chargeId,
|
||||
"paystatus": paystatus,
|
||||
})
|
||||
// if fmt.Sprintf("%.2f", price) != prodprice {
|
||||
// return fmt.Errorf("订单金额不匹配,数据库金额:%.2f,回调金额:%s; 订单id:%s, chargeid:%d", price, prodprice, p.OrderId, chargeId)
|
||||
// }
|
||||
|
||||
Loading…
Reference in New Issue
Block a user