feat: 新增异常日志收集功能,mediabot 可提交运行时异常,管理端可查看
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package com.par.api.controller;
|
||||
|
||||
import com.par.core.dto.ApiResponse;
|
||||
import com.par.core.dto.ErrorLogDTO;
|
||||
import com.par.core.entity.ErrorLog;
|
||||
import com.par.core.service.ErrorLogService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 异常日志控制器
|
||||
* 提交需 HMAC 鉴权(mediabot 调用),查看需管理员权限
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class ErrorLogController {
|
||||
|
||||
private final ErrorLogService errorLogService;
|
||||
|
||||
// ==================== mediabot 提交接口(PSK 鉴权) ====================
|
||||
|
||||
/**
|
||||
* mediabot 提交异常日志
|
||||
*/
|
||||
@PostMapping("/api/v1/sites/{siteId}/error-log")
|
||||
public ApiResponse<ErrorLogDTO> submitErrorLog(
|
||||
@PathVariable("siteId") String siteId,
|
||||
@RequestBody Map<String, Object> payload,
|
||||
HttpServletRequest request) {
|
||||
|
||||
String errorType = (String) payload.get("errorType");
|
||||
String message = (String) payload.get("message");
|
||||
String stackTrace = (String) payload.get("stackTrace");
|
||||
String configVersion = (String) payload.get("configVersion");
|
||||
String url = (String) payload.get("url");
|
||||
|
||||
if (errorType == null || errorType.isBlank()) {
|
||||
return ApiResponse.error(400, "errorType is required");
|
||||
}
|
||||
|
||||
String clientId = (String) request.getAttribute("clientId");
|
||||
|
||||
ErrorLog result = errorLogService.submit(siteId, clientId, errorType,
|
||||
message, stackTrace, configVersion, url);
|
||||
|
||||
return ApiResponse.success(errorLogService.toDTO(result));
|
||||
}
|
||||
|
||||
// ==================== 管理员查看接口(JWT 鉴权) ====================
|
||||
|
||||
/**
|
||||
* 获取所有异常日志
|
||||
*/
|
||||
@GetMapping("/api/v1/admin/error-logs")
|
||||
public ApiResponse<List<ErrorLogDTO>> listAll() {
|
||||
return ApiResponse.success(errorLogService.listAll());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定站点的异常日志
|
||||
*/
|
||||
@GetMapping("/api/v1/admin/error-logs/by-site/{siteId}")
|
||||
public ApiResponse<List<ErrorLogDTO>> listBySiteId(@PathVariable("siteId") String siteId) {
|
||||
return ApiResponse.success(errorLogService.listBySiteId(siteId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取单条异常日志详情
|
||||
*/
|
||||
@GetMapping("/api/v1/admin/error-logs/{id}")
|
||||
public ApiResponse<ErrorLogDTO> getById(@PathVariable("id") Long id) {
|
||||
ErrorLogDTO dto = errorLogService.getById(id);
|
||||
if (dto == null) {
|
||||
return ApiResponse.error(404, "异常日志不存在");
|
||||
}
|
||||
return ApiResponse.success(dto);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user