常熟吴彦祖 1 month ago
parent
commit
b86e31787b
  1. 33
      src/main/java/com/gaotao/common/utils/ErrorLogService.java
  2. 17
      src/main/java/com/gaotao/common/utils/ErrorLogUtils.java
  3. 20
      src/main/java/com/gaotao/modules/api/service/impl/WcsApiServiceImpl.java

33
src/main/java/com/gaotao/common/utils/ErrorLogService.java

@ -0,0 +1,33 @@
package com.gaotao.common.utils;
import com.gaotao.modules.api.dao.SysErrorLogMapper;
import com.gaotao.modules.api.entity.SysErrorLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* 错误日志保存服务
* 使用独立事务保存日志避免被外层事务回滚
*
* @author rqrq
* @date 2026/01/26
*/
@Service
public class ErrorLogService {
@Autowired
private SysErrorLogMapper sysErrorLogMapper;
/**
* 在独立事务中保存错误日志
* REQUIRES_NEW开启新事务不受外层事务影响
*
* @param log 错误日志实体
*/
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveInNewTransaction(SysErrorLog log) {
sysErrorLogMapper.insert(log);
}
}

17
src/main/java/com/gaotao/common/utils/ErrorLogUtils.java

@ -1,6 +1,5 @@
package com.gaotao.common.utils;
import com.gaotao.modules.api.dao.SysErrorLogMapper;
import com.gaotao.modules.api.entity.SysErrorLog;
import com.gaotao.modules.sys.entity.SysUserEntity;
import jakarta.annotation.PostConstruct;
@ -10,7 +9,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
@ -19,6 +17,8 @@ import java.util.Date;
* 错误日志工具类
* 提供简单易用的静态方法记录错误日志
*
* 特点使用独立事务保存日志不会被外层事务回滚
*
* 使用示例
* 1. 业务错误ErrorLogUtils.log("55", "立库自动化", "直接组盘", "W00250", "栈板不存在");
* 2. 接口错误ErrorLogUtils.logInterface("55", "立库自动化"(可为空), "推送WCS"(可为空), "WCS", "PushPalletDetailNew", "W00250", requestJson, "WCS返回失败");
@ -34,14 +34,14 @@ public class ErrorLogUtils {
private static final Logger logger = LoggerFactory.getLogger(ErrorLogUtils.class);
private static SysErrorLogMapper errorLogMapper;
private static ErrorLogService errorLogService;
@Autowired
private SysErrorLogMapper mapper;
private ErrorLogService service;
@PostConstruct
public void init() {
ErrorLogUtils.errorLogMapper = this.mapper;
ErrorLogUtils.errorLogService = this.service;
}
// ==================== 业务错误记录 ====================
@ -155,13 +155,13 @@ public class ErrorLogUtils {
// ==================== 私有方法 ====================
/**
* 保存错误日志
* 保存错误日志使用独立事务不受外层事务回滚影响
*/
private static void saveLog(String site, String moduleName, String functionName, String businessKey,
String isInterface, String interfaceType, String interfaceName,
String requestData, String errorMessage, String errorDetail) {
try {
if (errorLogMapper == null) {
if (errorLogService == null) {
logger.warn("ErrorLogUtils未初始化,无法记录错误日志");
return;
}
@ -182,7 +182,8 @@ public class ErrorLogUtils {
log.setUsername(getUsername());
log.setCreatedTime(new Date());
errorLogMapper.insert(log);
// 使用独立事务保存避免被外层事务回滚 - rqrq
errorLogService.saveInNewTransaction(log);
} catch (Exception ex) {
// 记录日志失败不影响主流程仅打印警告
logger.warn("记录错误日志失败: {}", ex.getMessage());

20
src/main/java/com/gaotao/modules/api/service/impl/WcsApiServiceImpl.java

@ -131,7 +131,7 @@ public class WcsApiServiceImpl implements WcsApiService {
"assemble-pallet",
inData.getPalletBarcode(),
requestJson,
e.getMessage());
e.getMessage(),e.getMessage());
throw new RuntimeException(e.getMessage());
}
@ -199,7 +199,7 @@ public class WcsApiServiceImpl implements WcsApiService {
"agv-feedback",
inData.getPalletId(),
requestJson,
e.getMessage());
e.getMessage(),e.getMessage());
throw new RuntimeException(e.getMessage());
}
@ -260,7 +260,7 @@ public class WcsApiServiceImpl implements WcsApiService {
"accept-material-requisition",
inData.getNotifyNo(),
requestJson,
e.getMessage());
e.getMessage(),e.getMessage());
throw new RuntimeException(e.getMessage());
}
@ -318,7 +318,7 @@ public class WcsApiServiceImpl implements WcsApiService {
"wcs-task/cancel",
inData.getPalletBarcode(),
requestJson,
e.getMessage());
e.getMessage(),e.getMessage());
throw new RuntimeException(e.getMessage());
}
@ -376,7 +376,7 @@ public class WcsApiServiceImpl implements WcsApiService {
"Call-pallet",
inData.getTaskNo(),
requestJson,
e.getMessage());
e.getMessage(),e.getMessage());
throw new RuntimeException(e.getMessage());
}
@ -434,7 +434,7 @@ public class WcsApiServiceImpl implements WcsApiService {
"update-empty-pallet-type",
inData.getPalletBarcode(),
requestJson,
e.getMessage());
e.getMessage(),e.getMessage());
throw new RuntimeException(e.getMessage());
}
@ -492,7 +492,7 @@ public class WcsApiServiceImpl implements WcsApiService {
"check-inventory-exists",
inData.getBasePalletType()+"-"+inData.getPalletType(),
requestJson,
e.getMessage());
e.getMessage(),e.getMessage());
throw new RuntimeException(e.getMessage());
}
@ -561,7 +561,7 @@ public class WcsApiServiceImpl implements WcsApiService {
"direct-material-call",
inData.getPalletId(),
requestJson,
e.getMessage());
e.getMessage(),e.getMessage());
throw new RuntimeException(e.getMessage());
}
@ -625,7 +625,7 @@ public class WcsApiServiceImpl implements WcsApiService {
"palletizingstation/process",
inData.getPalletCode(),
requestJson,
e.getMessage());
e.getMessage(),e.getMessage());
// 更新接口日志错误信息
if (logId != null) {
interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null);
@ -682,7 +682,7 @@ public class WcsApiServiceImpl implements WcsApiService {
"agv/getAvailableInboundStation",
inData.getTaskNo()+";"+inData.getPalletNo(),
requestJson,
e.getMessage());
e.getMessage(),e.getMessage());
// 更新接口日志错误信息
if (logId != null) {
interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null);

Loading…
Cancel
Save