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 java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -38,7 +38,6 @@ public class GlobalExceptionHandler {
|
||||
*/
|
||||
@ExceptionHandler(Throwable.class)
|
||||
public ResponseEntity handleException(Throwable e) {
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
return ResponseData.error(e.getMessage());
|
||||
}
|
||||
|
||||
@@ -58,8 +57,6 @@ public class GlobalExceptionHandler {
|
||||
*/
|
||||
@ExceptionHandler(value = BadRequestException.class)
|
||||
public ResponseEntity badRequestException(BadRequestException e) {
|
||||
// 打印堆栈信息
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
return ResponseData.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,6 @@ public class LogAspect {
|
||||
*/
|
||||
@Around("@annotation(logAnno)")
|
||||
public Object logAround(ProceedingJoinPoint joinPoint,org.nl.common.logging.annotation.Log logAnno) throws Throwable {
|
||||
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
String params = getParameter(method, joinPoint.getArgs());
|
||||
@@ -70,19 +69,15 @@ public class LogAspect {
|
||||
MDC.put("requestMethod",url);
|
||||
MDC.put("requestIp", requestIp);
|
||||
MDC.put("requestTime", DateUtil.now());
|
||||
//请求方式GET,POST
|
||||
MDC.put("urlMethod", request.getMethod());
|
||||
LuceneAppender.traceIdTL.set(BaseCode.intToChars(IdUtil.getLongId()));
|
||||
Object result = null;
|
||||
long comming = System.currentTimeMillis();
|
||||
try {
|
||||
log.info("[--request--][请求接口:{}][请求参数:{}]",url,params);
|
||||
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){
|
||||
|
||||
StringBuffer errorStack = new StringBuffer();
|
||||
errorStack.append("<br/>【异常堆栈:");
|
||||
String errorMsg = ex.getMessage();
|
||||
|
||||
@@ -20,6 +20,8 @@ public class AsyncLuceneAppender extends AspectLogbackAsyncAppender {
|
||||
@Override
|
||||
protected void append(ILoggingEvent event) {
|
||||
String traceId = LuceneAppender.traceIdTL.get();
|
||||
final String urlMethod = MDC.get("urlMethod");
|
||||
if (StringUtils.isEmpty(urlMethod) || !"GET".equalsIgnoreCase(urlMethod)){
|
||||
if (StringUtils.isNotEmpty(traceId)){
|
||||
MDC.put("traceId",traceId);
|
||||
Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();
|
||||
@@ -28,5 +30,6 @@ public class AsyncLuceneAppender extends AspectLogbackAsyncAppender {
|
||||
}
|
||||
}
|
||||
super.append(event);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
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.StrUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.nl.common.utils.RedisUtils;
|
||||
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.dao.SysQuartzJob;
|
||||
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.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.quartz.QuartzJobBean;
|
||||
@@ -48,56 +52,42 @@ public class ExecutionJob extends QuartzJobBean {
|
||||
ISysQuartzJobService quartzJobService = SpringContextHolder.getBean(SysQuartzJobServiceImpl.class);
|
||||
SysQuartzLogMapper quartzLogMapper = SpringContextHolder.getBean(SysQuartzLogMapper.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 {
|
||||
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(),
|
||||
quartzJob.getParams());
|
||||
Future<?> future = EXECUTOR.submit(task);
|
||||
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())) {
|
||||
String[] tasks = quartzJob.getSub_task().split("[,,]");
|
||||
// 执行子任务
|
||||
quartzJobService.executionSubJob(tasks);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (StrUtil.isNotEmpty(uuid)) {
|
||||
redisUtils.set(uuid, false);
|
||||
}
|
||||
long times = System.currentTimeMillis() - startTime;
|
||||
logDto.setTime(times);
|
||||
// 任务状态 0:成功 1:失败
|
||||
logDto.setIs_success(false);
|
||||
logDto.setException_detail(e.getMessage());
|
||||
// 任务如果失败了则暂停
|
||||
log.info("[--Quartz--][定时器:{}][执行结束....]",quartzJob.getBean_name());
|
||||
} catch (Exception ex) {
|
||||
if (quartzJob.getPause_after_failure() != null && quartzJob.getPause_after_failure()) {
|
||||
quartzJob.setIs_pause(false);
|
||||
//更新状态
|
||||
quartzJob.setUpdate_time(DateUtil.now());
|
||||
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());
|
||||
quartzLogMapper.insert(logDto);
|
||||
log.error("[-QuartzError-][定时器:{}]【异常信息:{}】", quartzJob.getBean_name(),errorStack.toString());
|
||||
} finally {
|
||||
|
||||
LuceneAppender.traceIdTL.remove();
|
||||
MDC.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user