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

@@ -229,7 +229,6 @@ func (s *chatServiceImpl) SendMessage(ctx context.Context, senderID string, conv
}
// 发送消息给接收者
log.Printf("[DEBUG SendMessage] 私聊消息 segments 类型: %T, 值: %+v", message.Segments, message.Segments)
wsMsg := websocket.CreateWSMessage(websocket.MessageTypeMessage, websocket.ChatMessage{
ID: message.ID,
ConversationID: message.ConversationID,
@@ -250,15 +249,11 @@ func (s *chatServiceImpl) SendMessage(ctx context.Context, senderID string, conv
// 如果接收者在线,发送实时消息
if s.wsManager != nil {
isOnline := s.wsManager.IsUserOnline(p.UserID)
log.Printf("[DEBUG SendMessage] 接收者 UserID=%s, 在线状态=%v", p.UserID, isOnline)
if isOnline {
log.Printf("[DEBUG SendMessage] 发送WebSocket消息给 UserID=%s, 消息类型=%s", p.UserID, wsMsg.Type)
s.wsManager.SendToUser(p.UserID, wsMsg)
}
}
}
} else {
log.Printf("[DEBUG SendMessage] 获取参与者失败: %v", err)
}
_ = participant // 避免未使用变量警告

View File

@@ -137,7 +137,7 @@ func (s *CommentService) afterCommentPublished(userID, postID, commentID string,
if parentUserID != userID {
notifyErr := s.systemMessageService.SendReplyNotification(context.Background(), parentUserID, userID, postID, *parentID, commentID)
if notifyErr != nil {
fmt.Printf("[DEBUG] Error sending reply notification: %v\n", notifyErr)
log.Printf("[ERROR] Error sending reply notification: %v", notifyErr)
}
}
} else {
@@ -145,7 +145,7 @@ func (s *CommentService) afterCommentPublished(userID, postID, commentID string,
if postOwnerID != userID {
notifyErr := s.systemMessageService.SendCommentNotification(context.Background(), postOwnerID, userID, postID, commentID)
if notifyErr != nil {
fmt.Printf("[DEBUG] Error sending comment notification: %v\n", notifyErr)
log.Printf("[ERROR] Error sending comment notification: %v", notifyErr)
}
}
}
@@ -252,9 +252,7 @@ func (s *CommentService) Like(ctx context.Context, commentID, userID string) err
)
}
if notifyErr != nil {
fmt.Printf("[DEBUG] Error sending like notification: %v\n", notifyErr)
} else {
fmt.Printf("[DEBUG] Like notification sent successfully\n")
log.Printf("[ERROR] Error sending like notification: %v", notifyErr)
}
}()
}

View File

@@ -276,9 +276,7 @@ func (s *PostService) Like(ctx context.Context, postID, userID string) error {
go func() {
notifyErr := s.systemMessageService.SendLikeNotification(context.Background(), post.UserID, userID, postID)
if notifyErr != nil {
fmt.Printf("[DEBUG] Error sending like notification: %v\n", notifyErr)
} else {
fmt.Printf("[DEBUG] Like notification sent successfully\n")
log.Printf("[ERROR] Error sending like notification: %v", notifyErr)
}
}()
}
@@ -343,9 +341,7 @@ func (s *PostService) Favorite(ctx context.Context, postID, userID string) error
go func() {
notifyErr := s.systemMessageService.SendFavoriteNotification(context.Background(), post.UserID, userID, postID)
if notifyErr != nil {
fmt.Printf("[DEBUG] Error sending favorite notification: %v\n", notifyErr)
} else {
fmt.Printf("[DEBUG] Favorite notification sent successfully\n")
log.Printf("[ERROR] Error sending favorite notification: %v", notifyErr)
}
}()
}

View File

@@ -165,15 +165,11 @@ func (s *systemMessageServiceImpl) SendReplyNotification(ctx context.Context, us
// SendFollowNotification 发送关注通知
func (s *systemMessageServiceImpl) SendFollowNotification(ctx context.Context, userID string, operatorID string) error {
fmt.Printf("[DEBUG] SendFollowNotification: userID=%s, operatorID=%s\n", userID, operatorID)
// 获取操作者信息
actorName, avatarURL, err := s.getActorInfo(ctx, operatorID)
if err != nil {
fmt.Printf("[DEBUG] SendFollowNotification: getActorInfo error: %v\n", err)
return err
}
fmt.Printf("[DEBUG] SendFollowNotification: actorName=%s, avatarURL=%s\n", actorName, avatarURL)
extraData := &model.SystemNotificationExtra{
ActorIDStr: operatorID,
@@ -193,16 +189,8 @@ func (s *systemMessageServiceImpl) SendFollowNotification(ctx context.Context, u
return fmt.Errorf("failed to create follow notification: %w", err)
}
fmt.Printf("[DEBUG] SendFollowNotification: notification created, ID=%d, Content=%s\n", notification.ID, notification.Content)
// 推送通知
pushErr := s.pushService.PushSystemNotification(ctx, userID, notification)
if pushErr != nil {
fmt.Printf("[DEBUG] SendFollowNotification: PushSystemNotification error: %v\n", pushErr)
} else {
fmt.Printf("[DEBUG] SendFollowNotification: PushSystemNotification success\n")
}
return pushErr
return s.pushService.PushSystemNotification(ctx, userID, notification)
}
// SendFavoriteNotification 发送收藏通知
@@ -387,12 +375,9 @@ func (s *systemMessageServiceImpl) SendBroadcastAnnouncement(ctx context.Context
// createNotification 创建系统通知(存储到独立表)
func (s *systemMessageServiceImpl) createNotification(ctx context.Context, userID string, notifyType model.SystemNotificationType, content string, extraData *model.SystemNotificationExtra) (*model.SystemNotification, error) {
fmt.Printf("[DEBUG] createNotification: userID=%s, notifyType=%s\n", userID, notifyType)
// 生成雪花算法ID
id, err := utils.GetSnowflake().GenerateID()
if err != nil {
fmt.Printf("[DEBUG] createNotification: failed to generate ID: %v\n", err)
return nil, fmt.Errorf("failed to generate notification ID: %w", err)
}
@@ -405,18 +390,14 @@ func (s *systemMessageServiceImpl) createNotification(ctx context.Context, userI
IsRead: false,
}
fmt.Printf("[DEBUG] createNotification: notification created with ID=%d, ReceiverID=%s\n", id, userID)
// 保存通知到数据库
if err := s.notifyRepo.Create(notification); err != nil {
fmt.Printf("[DEBUG] createNotification: failed to save notification: %v\n", err)
return nil, fmt.Errorf("failed to save notification: %w", err)
}
// 失效系统消息未读数缓存
cache.InvalidateUnreadSystem(s.cache, userID)
fmt.Printf("[DEBUG] createNotification: notification saved successfully, ID=%d\n", notification.ID)
return notification, nil
}
@@ -426,7 +407,6 @@ func (s *systemMessageServiceImpl) getActorInfo(ctx context.Context, operatorID
if s.userRepo != nil {
user, err := s.userRepo.GetByID(operatorID)
if err != nil {
fmt.Printf("[DEBUG] getActorInfo: failed to get user %s: %v\n", operatorID, err)
return "用户", utils.GenerateDefaultAvatarURL("用户"), nil // 返回默认值,不阻断流程
}
avatar := utils.GetAvatarOrDefault(user.Username, user.Nickname, user.Avatar)

View File

@@ -306,8 +306,6 @@ func (s *UserService) GetFollowing(ctx context.Context, userID string, page, pag
// FollowUser 关注用户
func (s *UserService) FollowUser(ctx context.Context, followerID, followeeID string) error {
fmt.Printf("[DEBUG] FollowUser called: followerID=%s, followeeID=%s\n", followerID, followeeID)
blocked, err := s.userRepo.IsBlockedEitherDirection(followerID, followeeID)
if err != nil {
return err
@@ -319,11 +317,9 @@ func (s *UserService) FollowUser(ctx context.Context, followerID, followeeID str
// 检查是否已经关注
isFollowing, err := s.userRepo.IsFollowing(followerID, followeeID)
if err != nil {
fmt.Printf("[DEBUG] Error checking existing follow: %v\n", err)
return err
}
if isFollowing {
fmt.Printf("[DEBUG] Already following, skip creation\n")
return nil // 已经关注,直接返回成功
}
@@ -335,23 +331,18 @@ func (s *UserService) FollowUser(ctx context.Context, followerID, followeeID str
err = s.userRepo.CreateFollow(follow)
if err != nil {
fmt.Printf("[DEBUG] CreateFollow error: %v\n", err)
return err
}
fmt.Printf("[DEBUG] Follow record created successfully\n")
// 刷新关注者的关注数(通过实际计数,更可靠)
err = s.userRepo.RefreshFollowingCount(followerID)
if err != nil {
fmt.Printf("[DEBUG] Error refreshing following count: %v\n", err)
// 不回滚,计数可以通过其他方式修复
}
// 刷新被关注者的粉丝数(通过实际计数,更可靠)
err = s.userRepo.RefreshFollowersCount(followeeID)
if err != nil {
fmt.Printf("[DEBUG] Error refreshing followers count: %v\n", err)
// 不回滚,计数可以通过其他方式修复
}
@@ -359,56 +350,38 @@ func (s *UserService) FollowUser(ctx context.Context, followerID, followeeID str
if s.systemMessageService != nil {
// 异步发送通知,不阻塞主流程
go func() {
notifyErr := s.systemMessageService.SendFollowNotification(context.Background(), followeeID, followerID)
if notifyErr != nil {
fmt.Printf("[DEBUG] Error sending follow notification: %v\n", notifyErr)
} else {
fmt.Printf("[DEBUG] Follow notification sent successfully to %s\n", followeeID)
}
_ = s.systemMessageService.SendFollowNotification(context.Background(), followeeID, followerID)
}()
}
fmt.Printf("[DEBUG] FollowUser completed: followerID=%s, followeeID=%s\n", followerID, followeeID)
return nil
}
// UnfollowUser 取消关注用户
func (s *UserService) UnfollowUser(ctx context.Context, followerID, followeeID string) error {
fmt.Printf("[DEBUG] UnfollowUser called: followerID=%s, followeeID=%s\n", followerID, followeeID)
// 检查是否已经关注
isFollowing, err := s.userRepo.IsFollowing(followerID, followeeID)
if err != nil {
fmt.Printf("[DEBUG] Error checking existing follow: %v\n", err)
return err
}
if !isFollowing {
fmt.Printf("[DEBUG] Not following, skip deletion\n")
return nil // 没有关注,直接返回成功
}
// 删除关注关系
err = s.userRepo.DeleteFollow(followerID, followeeID)
if err != nil {
fmt.Printf("[DEBUG] DeleteFollow error: %v\n", err)
return err
}
fmt.Printf("[DEBUG] Follow record deleted successfully\n")
// 刷新关注者的关注数(通过实际计数,更可靠)
err = s.userRepo.RefreshFollowingCount(followerID)
if err != nil {
fmt.Printf("[DEBUG] Error refreshing following count: %v\n", err)
}
// 刷新被关注者的粉丝数(通过实际计数,更可靠)
err = s.userRepo.RefreshFollowersCount(followeeID)
if err != nil {
fmt.Printf("[DEBUG] Error refreshing followers count: %v\n", err)
}
fmt.Printf("[DEBUG] UnfollowUser completed: followerID=%s, followeeID=%s\n", followerID, followeeID)
return nil
}