sdk shipping 日志修改
This commit is contained in:
parent
e395e64b17
commit
2e0ebbe228
@ -7,7 +7,6 @@ import (
|
|||||||
"backend/sdk/login/model/tuyou"
|
"backend/sdk/login/model/tuyou"
|
||||||
"backend/util"
|
"backend/util"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@ -49,7 +48,7 @@ func init() {
|
|||||||
currFile, err := os.OpenFile("./log/login.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
currFile, err := os.OpenFile("./log/login.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||||
if err != nil {
|
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)
|
logWriter = io.MultiWriter(rl, os.Stdout)
|
||||||
errWriter = io.MultiWriter(rl, os.Stderr)
|
errWriter = io.MultiWriter(rl, os.Stderr)
|
||||||
} else {
|
} else {
|
||||||
@ -68,29 +67,12 @@ func init() {
|
|||||||
go util.MonitorServerList()
|
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 {
|
func requestLogger() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
startedAt := time.Now()
|
startedAt := time.Now()
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
logStructured("info", "http request", map[string]any{
|
util.LogStructured("info", "http request", map[string]any{
|
||||||
"clientIp": c.ClientIP(),
|
"clientIp": c.ClientIP(),
|
||||||
"latencyMs": time.Since(startedAt).Milliseconds(),
|
"latencyMs": time.Since(startedAt).Milliseconds(),
|
||||||
"method": c.Request.Method,
|
"method": c.Request.Method,
|
||||||
@ -104,7 +86,7 @@ func requestLogger() gin.HandlerFunc {
|
|||||||
|
|
||||||
func recoveryLogger() gin.HandlerFunc {
|
func recoveryLogger() gin.HandlerFunc {
|
||||||
return gin.CustomRecovery(func(c *gin.Context, recovered any) {
|
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(),
|
"clientIp": c.ClientIP(),
|
||||||
"method": c.Request.Method,
|
"method": c.Request.Method,
|
||||||
"path": c.Request.URL.Path,
|
"path": c.Request.URL.Path,
|
||||||
@ -141,7 +123,7 @@ func main() {
|
|||||||
|
|
||||||
serverErr := make(chan error, 1)
|
serverErr := make(chan error, 1)
|
||||||
go func() {
|
go func() {
|
||||||
logStructured("info", "login sdk started", map[string]any{
|
util.LogStructured("info", "login sdk started", map[string]any{
|
||||||
"port": logincommon.AppConf.Port,
|
"port": logincommon.AppConf.Port,
|
||||||
"version": logincommon.AppConf.Version,
|
"version": logincommon.AppConf.Version,
|
||||||
})
|
})
|
||||||
@ -154,18 +136,18 @@ func main() {
|
|||||||
select {
|
select {
|
||||||
case err, ok := <-serverErr:
|
case err, ok := <-serverErr:
|
||||||
if ok && err != nil {
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
case <-shutdownCtx.Done():
|
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)
|
gracefulCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if err := server.Shutdown(gracefulCtx); err != nil {
|
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)
|
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)
|
log.Print("query order error:", err, p.OrderId)
|
||||||
return err
|
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 {
|
// if fmt.Sprintf("%.2f", price) != prodprice {
|
||||||
// return fmt.Errorf("订单金额不匹配,数据库金额:%.2f,回调金额:%s; 订单id:%s, chargeid:%d", price, prodprice, p.OrderId, chargeId)
|
// return fmt.Errorf("订单金额不匹配,数据库金额:%.2f,回调金额:%s; 订单id:%s, chargeid:%d", price, prodprice, p.OrderId, chargeId)
|
||||||
// }
|
// }
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user