add:添加定时器日志
fix:修复日志打印时没有traceId问题 opt:优化日志输出所有Get日志都不输出
This commit is contained in:
@@ -10,7 +10,6 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.http.ResponseEntity;import org.nl.common.base.ResponseData;
|
import org.springframework.http.ResponseEntity;import org.nl.common.base.ResponseData;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ public class GlobalExceptionHandler {
|
|||||||
*/
|
*/
|
||||||
@ExceptionHandler(Throwable.class)
|
@ExceptionHandler(Throwable.class)
|
||||||
public ResponseEntity handleException(Throwable e) {
|
public ResponseEntity handleException(Throwable e) {
|
||||||
log.error(ThrowableUtil.getStackTrace(e));
|
|
||||||
return ResponseData.error(e.getMessage());
|
return ResponseData.error(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,8 +57,6 @@ public class GlobalExceptionHandler {
|
|||||||
*/
|
*/
|
||||||
@ExceptionHandler(value = BadRequestException.class)
|
@ExceptionHandler(value = BadRequestException.class)
|
||||||
public ResponseEntity badRequestException(BadRequestException e) {
|
public ResponseEntity badRequestException(BadRequestException e) {
|
||||||
// 打印堆栈信息
|
|
||||||
log.error(ThrowableUtil.getStackTrace(e));
|
|
||||||
return ResponseData.error(e.getMessage());
|
return ResponseData.error(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,7 +60,6 @@ public class LogAspect {
|
|||||||
*/
|
*/
|
||||||
@Around("@annotation(logAnno)")
|
@Around("@annotation(logAnno)")
|
||||||
public Object logAround(ProceedingJoinPoint joinPoint,org.nl.common.logging.annotation.Log logAnno) throws Throwable {
|
public Object logAround(ProceedingJoinPoint joinPoint,org.nl.common.logging.annotation.Log logAnno) throws Throwable {
|
||||||
|
|
||||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||||
Method method = signature.getMethod();
|
Method method = signature.getMethod();
|
||||||
String params = getParameter(method, joinPoint.getArgs());
|
String params = getParameter(method, joinPoint.getArgs());
|
||||||
@@ -70,19 +69,15 @@ public class LogAspect {
|
|||||||
MDC.put("requestMethod",url);
|
MDC.put("requestMethod",url);
|
||||||
MDC.put("requestIp", requestIp);
|
MDC.put("requestIp", requestIp);
|
||||||
MDC.put("requestTime", DateUtil.now());
|
MDC.put("requestTime", DateUtil.now());
|
||||||
|
//请求方式GET,POST
|
||||||
|
MDC.put("urlMethod", request.getMethod());
|
||||||
LuceneAppender.traceIdTL.set(BaseCode.intToChars(IdUtil.getLongId()));
|
LuceneAppender.traceIdTL.set(BaseCode.intToChars(IdUtil.getLongId()));
|
||||||
Object result = null;
|
Object result = null;
|
||||||
long comming = System.currentTimeMillis();
|
long comming = System.currentTimeMillis();
|
||||||
try {
|
try {
|
||||||
log.info("[--request--][请求接口:{}][请求参数:{}]",url,params);
|
log.info("[--request--][请求接口:{}][请求参数:{}]",url,params);
|
||||||
result = joinPoint.proceed();
|
result = joinPoint.proceed();
|
||||||
// //是否把日志存到日志表
|
|
||||||
// if (logInfo.isAddLogTable()) {
|
|
||||||
// Log log = new Log("INFO", System.currentTimeMillis() - comming);
|
|
||||||
// logService.save(getUsername(), StringUtils.getBrowser(request), requestIp, joinPoint, log);
|
|
||||||
// }
|
|
||||||
}catch (Exception ex){
|
}catch (Exception ex){
|
||||||
|
|
||||||
StringBuffer errorStack = new StringBuffer();
|
StringBuffer errorStack = new StringBuffer();
|
||||||
errorStack.append("<br/>【异常堆栈:");
|
errorStack.append("<br/>【异常堆栈:");
|
||||||
String errorMsg = ex.getMessage();
|
String errorMsg = ex.getMessage();
|
||||||
|
|||||||
@@ -20,13 +20,16 @@ public class AsyncLuceneAppender extends AspectLogbackAsyncAppender {
|
|||||||
@Override
|
@Override
|
||||||
protected void append(ILoggingEvent event) {
|
protected void append(ILoggingEvent event) {
|
||||||
String traceId = LuceneAppender.traceIdTL.get();
|
String traceId = LuceneAppender.traceIdTL.get();
|
||||||
if (StringUtils.isNotEmpty(traceId)){
|
final String urlMethod = MDC.get("urlMethod");
|
||||||
MDC.put("traceId",traceId);
|
if (StringUtils.isEmpty(urlMethod) || !"GET".equalsIgnoreCase(urlMethod)){
|
||||||
Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();
|
if (StringUtils.isNotEmpty(traceId)){
|
||||||
if (mdcPropertyMap.getClass().getName().contains("SynchronizedMap")){
|
MDC.put("traceId",traceId);
|
||||||
mdcPropertyMap.put("traceId",traceId);
|
Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();
|
||||||
|
if (mdcPropertyMap.getClass().getName().contains("SynchronizedMap")){
|
||||||
|
mdcPropertyMap.put("traceId",traceId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
super.append(event);
|
||||||
super.append(event);
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package org.nl.wms.system_manage.service.quartz.utils;
|
package org.nl.wms.system_manage.service.quartz.utils;
|
||||||
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import org.nl.common.utils.RedisUtils;
|
import org.nl.common.utils.RedisUtils;
|
||||||
import org.nl.config.SpringContextHolder;
|
import org.nl.config.SpringContextHolder;
|
||||||
|
import org.nl.wms.system_manage.service.logserver.lucene.BaseCode;
|
||||||
|
import org.nl.wms.system_manage.service.logserver.lucene.LuceneAppender;
|
||||||
import org.nl.wms.system_manage.service.quartz.ISysQuartzJobService;
|
import org.nl.wms.system_manage.service.quartz.ISysQuartzJobService;
|
||||||
import org.nl.wms.system_manage.service.quartz.dao.SysQuartzJob;
|
import org.nl.wms.system_manage.service.quartz.dao.SysQuartzJob;
|
||||||
import org.nl.wms.system_manage.service.quartz.dao.SysQuartzLog;
|
import org.nl.wms.system_manage.service.quartz.dao.SysQuartzLog;
|
||||||
@@ -13,6 +16,7 @@ import org.nl.wms.system_manage.service.quartz.impl.SysQuartzJobServiceImpl;
|
|||||||
import org.quartz.DisallowConcurrentExecution;
|
import org.quartz.DisallowConcurrentExecution;
|
||||||
import org.quartz.JobExecutionContext;
|
import org.quartz.JobExecutionContext;
|
||||||
import org.quartz.JobExecutionException;
|
import org.quartz.JobExecutionException;
|
||||||
|
import org.slf4j.MDC;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.scheduling.annotation.Async;
|
import org.springframework.scheduling.annotation.Async;
|
||||||
import org.springframework.scheduling.quartz.QuartzJobBean;
|
import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||||
@@ -48,56 +52,42 @@ public class ExecutionJob extends QuartzJobBean {
|
|||||||
ISysQuartzJobService quartzJobService = SpringContextHolder.getBean(SysQuartzJobServiceImpl.class);
|
ISysQuartzJobService quartzJobService = SpringContextHolder.getBean(SysQuartzJobServiceImpl.class);
|
||||||
SysQuartzLogMapper quartzLogMapper = SpringContextHolder.getBean(SysQuartzLogMapper.class);
|
SysQuartzLogMapper quartzLogMapper = SpringContextHolder.getBean(SysQuartzLogMapper.class);
|
||||||
RedisUtils redisUtils = SpringContextHolder.getBean(RedisUtils.class);
|
RedisUtils redisUtils = SpringContextHolder.getBean(RedisUtils.class);
|
||||||
|
|
||||||
String uuid = quartzJob.getUuid();
|
|
||||||
|
|
||||||
SysQuartzLog logDto = new SysQuartzLog();
|
|
||||||
logDto.setLog_id(IdUtil.getSnowflake(1,1).nextIdStr());
|
|
||||||
logDto.setJob_name(quartzJob.getJob_name());
|
|
||||||
logDto.setBean_name(quartzJob.getBean_name());
|
|
||||||
logDto.setMethod_name(quartzJob.getMethod_name());
|
|
||||||
logDto.setParams(quartzJob.getParams());
|
|
||||||
long startTime = System.currentTimeMillis();
|
|
||||||
logDto.setCron_expression(quartzJob.getCron_expression());
|
|
||||||
try {
|
try {
|
||||||
|
LuceneAppender.traceIdTL.set(BaseCode.intToChars(org.nl.common.utils.IdUtil.getLongId()));
|
||||||
// 执行任务
|
// 执行任务
|
||||||
|
log.info("[--Quartz--][定时器:{}][开始执行....]",quartzJob.getBean_name());
|
||||||
QuartzRunnable task = new QuartzRunnable(quartzJob.getBean_name(), quartzJob.getMethod_name(),
|
QuartzRunnable task = new QuartzRunnable(quartzJob.getBean_name(), quartzJob.getMethod_name(),
|
||||||
quartzJob.getParams());
|
quartzJob.getParams());
|
||||||
Future<?> future = EXECUTOR.submit(task);
|
Future<?> future = EXECUTOR.submit(task);
|
||||||
future.get();
|
future.get();
|
||||||
long times = System.currentTimeMillis() - startTime;
|
|
||||||
logDto.setTime(times);
|
|
||||||
if (StrUtil.isNotEmpty(uuid)) {
|
|
||||||
redisUtils.set(uuid, true);
|
|
||||||
}
|
|
||||||
// 任务状态
|
|
||||||
logDto.setIs_success(true);
|
|
||||||
// 判断是否存在子任务
|
// 判断是否存在子任务
|
||||||
if (StrUtil.isNotEmpty(quartzJob.getSub_task())) {
|
if (StrUtil.isNotEmpty(quartzJob.getSub_task())) {
|
||||||
String[] tasks = quartzJob.getSub_task().split("[,,]");
|
String[] tasks = quartzJob.getSub_task().split("[,,]");
|
||||||
// 执行子任务
|
|
||||||
quartzJobService.executionSubJob(tasks);
|
quartzJobService.executionSubJob(tasks);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
log.info("[--Quartz--][定时器:{}][执行结束....]",quartzJob.getBean_name());
|
||||||
if (StrUtil.isNotEmpty(uuid)) {
|
} catch (Exception ex) {
|
||||||
redisUtils.set(uuid, false);
|
|
||||||
}
|
|
||||||
long times = System.currentTimeMillis() - startTime;
|
|
||||||
logDto.setTime(times);
|
|
||||||
// 任务状态 0:成功 1:失败
|
|
||||||
logDto.setIs_success(false);
|
|
||||||
logDto.setException_detail(e.getMessage());
|
|
||||||
// 任务如果失败了则暂停
|
|
||||||
if (quartzJob.getPause_after_failure() != null && quartzJob.getPause_after_failure()) {
|
if (quartzJob.getPause_after_failure() != null && quartzJob.getPause_after_failure()) {
|
||||||
quartzJob.setIs_pause(false);
|
quartzJob.setIs_pause(false);
|
||||||
//更新状态
|
quartzJob.setUpdate_time(DateUtil.now());
|
||||||
quartzJobService.updateIsPause(quartzJob);
|
quartzJobService.updateIsPause(quartzJob);
|
||||||
}
|
}
|
||||||
|
StringBuffer errorStack = new StringBuffer();
|
||||||
|
errorStack.append("<br/>【异常堆栈:");
|
||||||
|
String errorMsg = ex.getMessage();
|
||||||
|
int x = 0;
|
||||||
|
StackTraceElement[] stackTrace = ex.getStackTrace();
|
||||||
|
if (stackTrace!=null && stackTrace.length>0){
|
||||||
|
for (StackTraceElement stack : stackTrace) {
|
||||||
|
x++;errorStack.append(stack.toString().replaceAll("<",">")).append("<br/>");
|
||||||
|
if (x>10){ break; }
|
||||||
|
}
|
||||||
|
}
|
||||||
//异常时候打印日志
|
//异常时候打印日志
|
||||||
log.error(logDto.toString());
|
log.error("[-QuartzError-][定时器:{}]【异常信息:{}】", quartzJob.getBean_name(),errorStack.toString());
|
||||||
quartzLogMapper.insert(logDto);
|
|
||||||
} finally {
|
} finally {
|
||||||
|
LuceneAppender.traceIdTL.remove();
|
||||||
|
MDC.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user