Clean backend debug logging and standardize error reporting.

This removes verbose trace output in handlers/services and keeps only actionable error-level logs.
This commit is contained in:
2026-03-09 22:18:53 +08:00
parent 4d8f2ec997
commit 4c0177149a
13 changed files with 8 additions and 163 deletions

View File

@@ -2,7 +2,6 @@ package handler
import (
"context"
"fmt"
"strconv"
"github.com/gin-gonic/gin"
@@ -145,14 +144,11 @@ func (h *MessageHandler) GetConversationByID(c *gin.Context) {
}
conversationIDStr := c.Param("id")
fmt.Printf("[DEBUG] GetConversationByID: conversationIDStr = %s\n", conversationIDStr)
conversationID, err := service.ParseConversationID(conversationIDStr)
if err != nil {
fmt.Printf("[DEBUG] GetConversationByID: failed to parse conversation ID: %v\n", err)
response.BadRequest(c, "invalid conversation id")
return
}
fmt.Printf("[DEBUG] GetConversationByID: conversationID = %s\n", conversationID)
conv, err := h.chatService.GetConversationByID(c.Request.Context(), conversationID, userID)
if err != nil {
@@ -281,14 +277,11 @@ func (h *MessageHandler) SendMessage(c *gin.Context) {
}
conversationIDStr := c.Param("id")
fmt.Printf("[DEBUG] SendMessage: conversationIDStr = %s\n", conversationIDStr)
conversationID, err := service.ParseConversationID(conversationIDStr)
if err != nil {
fmt.Printf("[DEBUG] SendMessage: failed to parse conversation ID: %v\n", err)
response.BadRequest(c, "invalid conversation id")
return
}
fmt.Printf("[DEBUG] SendMessage: conversationID = %s, userID = %s\n", conversationID, userID)
var req dto.SendMessageRequest
if err := c.ShouldBindJSON(&req); err != nil {
@@ -507,11 +500,8 @@ func (h *MessageHandler) MarkAsRead(c *gin.Context) {
// GET /api/conversations/unread/count
func (h *MessageHandler) GetUnreadCount(c *gin.Context) {
userID := c.GetString("user_id")
// 添加调试日志
fmt.Printf("[DEBUG] GetUnreadCount: user_id from context = %q\n", userID)
if userID == "" {
fmt.Printf("[DEBUG] GetUnreadCount: user_id is empty, returning 401\n")
response.Unauthorized(c, "")
return
}

View File

@@ -80,16 +80,10 @@ func (h *PostHandler) GetByID(c *gin.Context) {
// 注意:不再自动增加浏览量,浏览量通过 RecordView 端点单独记录
// 获取当前用户ID用于判断点赞和收藏状态
fmt.Printf("[DEBUG] GetByID - postID: %s, currentUserID: %s\n", id, currentUserID)
var isLiked, isFavorited bool
if currentUserID != "" {
isLiked = h.postService.IsLiked(c.Request.Context(), id, currentUserID)
isFavorited = h.postService.IsFavorited(c.Request.Context(), id, currentUserID)
fmt.Printf("[DEBUG] GetByID - isLiked: %v, isFavorited: %v\n", isLiked, isFavorited)
} else {
fmt.Printf("[DEBUG] GetByID - user not logged in, isLiked: false, isFavorited: false\n")
}
// 如果有当前用户,检查与帖子作者的相互关注状态
@@ -142,7 +136,7 @@ func (h *PostHandler) RecordView(c *gin.Context) {
// 增加浏览量
if err := h.postService.IncrementViews(c.Request.Context(), id, userID); err != nil {
fmt.Printf("[DEBUG] Failed to increment views for post %s: %v\n", id, err)
fmt.Printf("[ERROR] Failed to increment views for post %s: %v\n", id, err)
response.InternalServerError(c, "failed to record view")
return
}
@@ -192,8 +186,6 @@ func (h *PostHandler) List(c *gin.Context) {
return
}
fmt.Printf("[DEBUG] List - tab: %s, currentUserID: %s, posts count: %d\n", tab, currentUserID, len(posts))
isLikedMap := make(map[string]bool)
isFavoritedMap := make(map[string]bool)
if currentUserID != "" {
@@ -202,10 +194,7 @@ func (h *PostHandler) List(c *gin.Context) {
isFavorited := h.postService.IsFavorited(c.Request.Context(), post.ID, currentUserID)
isLikedMap[post.ID] = isLiked
isFavoritedMap[post.ID] = isFavorited
fmt.Printf("[DEBUG] List - postID: %s, isLiked: %v, isFavorited: %v\n", post.ID, isLiked, isFavorited)
}
} else {
fmt.Printf("[DEBUG] List - user not logged in\n")
}
// 转换为响应结构
@@ -308,7 +297,6 @@ func (h *PostHandler) Like(c *gin.Context) {
}
id := c.Param("id")
fmt.Printf("[DEBUG] Like - postID: %s, userID: %s\n", id, userID)
err := h.postService.Like(c.Request.Context(), id, userID)
if err != nil {
@@ -325,7 +313,6 @@ func (h *PostHandler) Like(c *gin.Context) {
isLiked := h.postService.IsLiked(c.Request.Context(), id, userID)
isFavorited := h.postService.IsFavorited(c.Request.Context(), id, userID)
fmt.Printf("[DEBUG] Like - postID: %s, isLiked: %v, isFavorited: %v\n", id, isLiked, isFavorited)
response.Success(c, dto.ConvertPostToResponse(post, isLiked, isFavorited))
}
@@ -339,7 +326,6 @@ func (h *PostHandler) Unlike(c *gin.Context) {
}
id := c.Param("id")
fmt.Printf("[DEBUG] Unlike - postID: %s, userID: %s\n", id, userID)
err := h.postService.Unlike(c.Request.Context(), id, userID)
if err != nil {
@@ -356,7 +342,6 @@ func (h *PostHandler) Unlike(c *gin.Context) {
isLiked := h.postService.IsLiked(c.Request.Context(), id, userID)
isFavorited := h.postService.IsFavorited(c.Request.Context(), id, userID)
fmt.Printf("[DEBUG] Unlike - postID: %s, isLiked: %v, isFavorited: %v\n", id, isLiked, isFavorited)
response.Success(c, dto.ConvertPostToResponse(post, isLiked, isFavorited))
}
@@ -370,7 +355,6 @@ func (h *PostHandler) Favorite(c *gin.Context) {
}
id := c.Param("id")
fmt.Printf("[DEBUG] Favorite - postID: %s, userID: %s\n", id, userID)
err := h.postService.Favorite(c.Request.Context(), id, userID)
if err != nil {
@@ -387,7 +371,6 @@ func (h *PostHandler) Favorite(c *gin.Context) {
isLiked := h.postService.IsLiked(c.Request.Context(), id, userID)
isFavorited := h.postService.IsFavorited(c.Request.Context(), id, userID)
fmt.Printf("[DEBUG] Favorite - postID: %s, isLiked: %v, isFavorited: %v\n", id, isLiked, isFavorited)
response.Success(c, dto.ConvertPostToResponse(post, isLiked, isFavorited))
}
@@ -401,7 +384,6 @@ func (h *PostHandler) Unfavorite(c *gin.Context) {
}
id := c.Param("id")
fmt.Printf("[DEBUG] Unfavorite - postID: %s, userID: %s\n", id, userID)
err := h.postService.Unfavorite(c.Request.Context(), id, userID)
if err != nil {
@@ -418,7 +400,6 @@ func (h *PostHandler) Unfavorite(c *gin.Context) {
isLiked := h.postService.IsLiked(c.Request.Context(), id, userID)
isFavorited := h.postService.IsFavorited(c.Request.Context(), id, userID)
fmt.Printf("[DEBUG] Unfavorite - postID: %s, isLiked: %v, isFavorited: %v\n", id, isLiked, isFavorited)
response.Success(c, dto.ConvertPostToResponse(post, isLiked, isFavorited))
}

View File

@@ -1,7 +1,6 @@
package handler
import (
"fmt"
"strconv"
"github.com/gin-gonic/gin"
@@ -572,16 +571,12 @@ func (h *UserHandler) GetFollowersList(c *gin.Context) {
page := c.DefaultQuery("page", "1")
pageSize := c.DefaultQuery("page_size", "20")
fmt.Printf("[DEBUG] GetFollowersList: userID=%s, currentUserID=%s\n", userID, currentUserID)
users, err := h.userService.GetFollowersList(c.Request.Context(), userID, page, pageSize)
if err != nil {
response.InternalServerError(c, "failed to get followers list")
return
}
fmt.Printf("[DEBUG] GetFollowersList: found %d users\n", len(users))
// 如果已登录,获取双向关注状态和实时计算的帖子数量
var userResponses []*dto.UserResponse
if currentUserID != "" && len(users) > 0 {
@@ -589,7 +584,6 @@ func (h *UserHandler) GetFollowersList(c *gin.Context) {
for i, u := range users {
userIDs[i] = u.ID
}
fmt.Printf("[DEBUG] GetFollowersList: checking mutual follow status for userIDs=%v\n", userIDs)
statusMap, _ := h.userService.GetMutualFollowStatus(c.Request.Context(), currentUserID, userIDs)
postsCountMap, _ := h.userService.GetUserPostCountBatch(c.Request.Context(), userIDs)
userResponses = dto.ConvertUsersToResponseWithMutualFollowAndPostsCount(users, statusMap, postsCountMap)

View File

@@ -106,13 +106,6 @@ func (h *WebSocketHandler) HandleWebSocket(c *gin.Context) {
return
}
// 如果用户已在线,先注销旧连接
if h.wsManager.IsUserOnline(userID) {
log.Printf("[DEBUG] 用户 %s 已有在线连接,复用该连接", userID)
} else {
log.Printf("[DEBUG] 用户 %s 当前不在线,创建新连接", userID)
}
// 创建客户端
client := &ws.Client{
ID: userID,
@@ -129,7 +122,6 @@ func (h *WebSocketHandler) HandleWebSocket(c *gin.Context) {
go client.WritePump()
go h.handleMessages(client)
log.Printf("[DEBUG] WebSocket连接建立: userID=%s, 当前在线=%v", userID, h.wsManager.IsUserOnline(userID))
}
// handleMessages 处理客户端消息
@@ -233,8 +225,6 @@ func (h *WebSocketHandler) handleChatMessage(client *ws.Client, msg *ws.WSMessag
return
}
log.Printf("[DEBUG handleChatMessage] 完整data: %+v", data)
conversationIDStr, _ := data["conversationId"].(string)
if conversationIDStr == "" {
@@ -293,8 +283,6 @@ func (h *WebSocketHandler) handleChatMessage(client *ws.Client, msg *ws.WSMessag
})
if client.Send != nil {
msgBytes, _ := json.Marshal(metaAckMsg)
log.Printf("[DEBUG handleChatMessage] 私聊 ack 消息: %s", string(msgBytes))
log.Printf("[DEBUG handleChatMessage] message.Segments 类型: %T, 值: %+v", message.Segments, message.Segments)
client.Send <- msgBytes
}
}
@@ -507,9 +495,6 @@ func (h *WebSocketHandler) handleGroupMessage(client *ws.Client, msg *ws.WSMessa
// 发送确认消息给发送者作为meta事件
// 使用 meta 事件格式发送 ack
log.Printf("[DEBUG HandleGroupMessageSend] 准备发送 ack 消息, userID=%s, messageID=%s, seq=%d",
client.UserID, savedMessage.ID, savedMessage.Seq)
metaAckMsg := ws.CreateWSMessage("meta", map[string]interface{}{
"detail_type": ws.MetaDetailTypeAck,
"conversation_id": conversationID,
@@ -523,8 +508,6 @@ func (h *WebSocketHandler) handleGroupMessage(client *ws.Client, msg *ws.WSMessa
})
if client.Send != nil {
msgBytes, _ := json.Marshal(metaAckMsg)
log.Printf("[DEBUG HandleGroupMessageSend] 发送 ack 消息到 channel, userID=%s, msg=%s",
client.UserID, string(msgBytes))
client.Send <- msgBytes
} else {
log.Printf("[ERROR HandleGroupMessageSend] client.Send 为 nil, userID=%s", client.UserID)