mirror of https://github.com/elunez/eladmin
代码优化,异常日志保存堆栈信息
parent
1ec10e3d38
commit
49f3e590f9
18
pom.xml
18
pom.xml
|
@ -60,6 +60,12 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--模板引擎-->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||
</dependency>
|
||||
<!--Spring boot end-->
|
||||
|
||||
<!--jedis-->
|
||||
|
@ -175,6 +181,18 @@
|
|||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/commons-configuration/commons-configuration -->
|
||||
<dependency>
|
||||
<groupId>commons-configuration</groupId>
|
||||
<artifactId>commons-configuration</artifactId>
|
||||
<version>1.9</version>
|
||||
</dependency>
|
||||
|
||||
<!-- quartz -->
|
||||
<dependency>
|
||||
<groupId>org.quartz-scheduler</groupId>
|
||||
<artifactId>quartz</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
|
|
@ -11,7 +11,6 @@ import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBr
|
|||
* @date 2018/11/15 9:20:19
|
||||
*/
|
||||
@SpringBootApplication
|
||||
//开启定时任务
|
||||
@EnableScheduling
|
||||
@EnableTransactionManagement
|
||||
@EnableWebSocketMessageBroker
|
||||
|
|
|
@ -2,6 +2,7 @@ package me.zhengjie.common.aop.log;
|
|||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.common.exception.BadRequestException;
|
||||
import me.zhengjie.common.utils.ThrowableUtil;
|
||||
import me.zhengjie.monitor.domain.Logging;
|
||||
import me.zhengjie.monitor.service.LoggingService;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
|
@ -66,7 +67,7 @@ public class LogAspect {
|
|||
@AfterThrowing(pointcut = "logPointcut()", throwing = "e")
|
||||
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
|
||||
Logging logging = new Logging("ERROR",System.currentTimeMillis() - currentTime);
|
||||
logging.setExceptionDetail(e.getMessage());
|
||||
logging.setExceptionDetail(ThrowableUtil.getStackTrace(e));
|
||||
loggingService.save((ProceedingJoinPoint)joinPoint, logging);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,14 +4,13 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import me.zhengjie.common.exception.BadRequestException;
|
||||
import me.zhengjie.common.exception.EntityExistException;
|
||||
import me.zhengjie.common.exception.EntityNotFoundException;
|
||||
import me.zhengjie.common.utils.ThrowableUtil;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
import static org.springframework.http.HttpStatus.FORBIDDEN;
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
|
@ -32,7 +31,7 @@ public class GlobalExceptionHandler {
|
|||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity handleException(Exception e){
|
||||
// 打印堆栈信息
|
||||
log.error(getStackTrace(e));
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
ApiError apiError = new ApiError(BAD_REQUEST.value(),e.getMessage());
|
||||
return buildResponseEntity(apiError);
|
||||
}
|
||||
|
@ -45,7 +44,7 @@ public class GlobalExceptionHandler {
|
|||
@ExceptionHandler(AccessDeniedException.class)
|
||||
public ResponseEntity handleAccessDeniedException(AccessDeniedException e){
|
||||
// 打印堆栈信息
|
||||
log.error(getStackTrace(e));
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
ApiError apiError = new ApiError(FORBIDDEN.value(),e.getMessage());
|
||||
return buildResponseEntity(apiError);
|
||||
}
|
||||
|
@ -58,7 +57,7 @@ public class GlobalExceptionHandler {
|
|||
@ExceptionHandler(value = BadRequestException.class)
|
||||
public ResponseEntity<ApiError> badRequestException(BadRequestException e) {
|
||||
// 打印堆栈信息
|
||||
log.error(getStackTrace(e));
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
ApiError apiError = new ApiError(e.getStatus(),e.getMessage());
|
||||
return buildResponseEntity(apiError);
|
||||
}
|
||||
|
@ -71,7 +70,7 @@ public class GlobalExceptionHandler {
|
|||
@ExceptionHandler(value = EntityExistException.class)
|
||||
public ResponseEntity<ApiError> entityExistException(EntityExistException e) {
|
||||
// 打印堆栈信息
|
||||
log.error(getStackTrace(e));
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
ApiError apiError = new ApiError(BAD_REQUEST.value(),e.getMessage());
|
||||
return buildResponseEntity(apiError);
|
||||
}
|
||||
|
@ -84,7 +83,7 @@ public class GlobalExceptionHandler {
|
|||
@ExceptionHandler(value = EntityNotFoundException.class)
|
||||
public ResponseEntity<ApiError> entityNotFoundException(EntityNotFoundException e) {
|
||||
// 打印堆栈信息
|
||||
log.error(getStackTrace(e));
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
ApiError apiError = new ApiError(NOT_FOUND.value(),e.getMessage());
|
||||
return buildResponseEntity(apiError);
|
||||
}
|
||||
|
@ -97,7 +96,7 @@ public class GlobalExceptionHandler {
|
|||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<ApiError> handleMethodArgumentNotValidException(MethodArgumentNotValidException e){
|
||||
// 打印堆栈信息
|
||||
log.error(getStackTrace(e));
|
||||
log.error(ThrowableUtil.getStackTrace(e));
|
||||
String[] str = e.getBindingResult().getAllErrors().get(0).getCodes()[1].split("\\.");
|
||||
StringBuffer msg = new StringBuffer(str[1]+":");
|
||||
msg.append(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
|
||||
|
@ -113,21 +112,4 @@ public class GlobalExceptionHandler {
|
|||
private ResponseEntity<ApiError> buildResponseEntity(ApiError apiError) {
|
||||
return new ResponseEntity(apiError, HttpStatus.valueOf(apiError.getStatus()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取堆栈信息
|
||||
* @param throwable
|
||||
* @return
|
||||
*/
|
||||
private String getStackTrace(Throwable throwable)
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
try {
|
||||
throwable.printStackTrace(pw);
|
||||
return "\n"+sw.toString();
|
||||
} finally {
|
||||
pw.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package me.zhengjie.common.utils;
|
||||
|
||||
import freemarker.template.Configuration;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
/**
|
||||
* @author jie
|
||||
* @date 2019-01-03
|
||||
*/
|
||||
public class FtlUtil {
|
||||
|
||||
private static Configuration configuration;
|
||||
private static FileOutputStream fileOut = null;
|
||||
|
||||
static {
|
||||
configuration=new Configuration(Configuration.VERSION_2_3_28);
|
||||
configuration.setDefaultEncoding("UTF-8");
|
||||
configuration.setClassForTemplateLoading(FtlUtil.class, "/template/generator");
|
||||
configuration.setClassForTemplateLoading(FtlUtil.class, "/template/email");
|
||||
}
|
||||
|
||||
public static Configuration getConfig(){
|
||||
return configuration;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,214 @@
|
|||
package me.zhengjie.common.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
|
||||
/**
|
||||
* 字符串工具类, 继承org.apache.commons.lang3.StringUtils类
|
||||
*/
|
||||
public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
||||
|
||||
private static final char SEPARATOR = '_';
|
||||
private static final String CHARSET_NAME = "UTF-8";
|
||||
|
||||
/**
|
||||
* 转换为字节数组
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
public static byte[] getBytes(String str) {
|
||||
if (str != null) {
|
||||
try {
|
||||
return str.getBytes(CHARSET_NAME);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为字节数组
|
||||
*
|
||||
* @param bytes
|
||||
* @return
|
||||
*/
|
||||
public static String toString(byte[] bytes) {
|
||||
try {
|
||||
return new String(bytes, CHARSET_NAME);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
return EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否包含字符串
|
||||
*
|
||||
* @param str 验证字符串
|
||||
* @param strs 字符串组
|
||||
* @return 包含返回true
|
||||
*/
|
||||
public static boolean inString(String str, String... strs) {
|
||||
if (str != null) {
|
||||
for (String s : strs) {
|
||||
if (str.equals(trim(s))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Double类型
|
||||
*/
|
||||
public static Double toDouble(Object val) {
|
||||
if (val == null) {
|
||||
return 0D;
|
||||
}
|
||||
try {
|
||||
return Double.valueOf(trim(val.toString()));
|
||||
} catch (Exception e) {
|
||||
return 0D;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Float类型
|
||||
*/
|
||||
public static Float toFloat(Object val) {
|
||||
return toDouble(val).floatValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Long类型
|
||||
*/
|
||||
public static Long toLong(Object val) {
|
||||
return toDouble(val).longValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换为Integer类型
|
||||
*/
|
||||
public static Integer toInteger(Object val) {
|
||||
return toLong(val).intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰命名法工具
|
||||
*
|
||||
* @return toCamelCase(" hello_world ") == "helloWorld"
|
||||
* toCapitalizeCamelCase("hello_world") == "HelloWorld"
|
||||
* toUnderScoreCase("helloWorld") = "hello_world"
|
||||
*/
|
||||
public static String toCamelCase(String s) {
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
s = s.toLowerCase();
|
||||
|
||||
StringBuilder sb = new StringBuilder(s.length());
|
||||
boolean upperCase = false;
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
|
||||
if (c == SEPARATOR) {
|
||||
upperCase = true;
|
||||
} else if (upperCase) {
|
||||
sb.append(Character.toUpperCase(c));
|
||||
upperCase = false;
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰命名法工具
|
||||
*
|
||||
* @return toCamelCase(" hello_world ") == "helloWorld"
|
||||
* toCapitalizeCamelCase("hello_world") == "HelloWorld"
|
||||
* toUnderScoreCase("helloWorld") = "hello_world"
|
||||
*/
|
||||
public static String toCapitalizeCamelCase(String s) {
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
s = toCamelCase(s);
|
||||
return s.substring(0, 1).toUpperCase() + s.substring(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰命名法工具
|
||||
*
|
||||
* @return toCamelCase(" hello_world ") == "helloWorld"
|
||||
* toCapitalizeCamelCase("hello_world") == "HelloWorld"
|
||||
* toUnderScoreCase("helloWorld") = "hello_world"
|
||||
*/
|
||||
public static String toUnderScoreCase(String s) {
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean upperCase = false;
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
|
||||
boolean nextUpperCase = true;
|
||||
|
||||
if (i < (s.length() - 1)) {
|
||||
nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
|
||||
}
|
||||
|
||||
if ((i > 0) && Character.isUpperCase(c)) {
|
||||
if (!upperCase || !nextUpperCase) {
|
||||
sb.append(SEPARATOR);
|
||||
}
|
||||
upperCase = true;
|
||||
} else {
|
||||
upperCase = false;
|
||||
}
|
||||
|
||||
sb.append(Character.toLowerCase(c));
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取工程路径
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String getProjectPath() {
|
||||
String projectPath = "";
|
||||
try {
|
||||
File file = new DefaultResourceLoader().getResource("").getFile();
|
||||
if (file != null) {
|
||||
while (true) {
|
||||
File f = new File(file.getPath() + File.separator + "src" + File.separator + "main");
|
||||
if (f == null || f.exists()) {
|
||||
break;
|
||||
}
|
||||
if (file.getParentFile() != null) {
|
||||
file = file.getParentFile();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
projectPath = file.toString();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return projectPath;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package me.zhengjie.common.utils;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
|
||||
/**
|
||||
* 异常工具
|
||||
* @author jie
|
||||
* @date 2019-01-06
|
||||
*/
|
||||
public class ThrowableUtil {
|
||||
|
||||
/**
|
||||
* 获取堆栈信息
|
||||
* @param throwable
|
||||
* @return
|
||||
*/
|
||||
public static String getStackTrace(Throwable throwable){
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
try {
|
||||
throwable.printStackTrace(pw);
|
||||
return "\n"+sw.toString();
|
||||
} finally {
|
||||
pw.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -38,7 +38,7 @@ public class Logging {
|
|||
/**
|
||||
* 参数
|
||||
*/
|
||||
@Column(length = 1500)
|
||||
@Column(columnDefinition = "text")
|
||||
private String params;
|
||||
|
||||
/**
|
||||
|
@ -59,7 +59,7 @@ public class Logging {
|
|||
/**
|
||||
* 异常详细
|
||||
*/
|
||||
@Column(length = 1500)
|
||||
@Column(columnDefinition = "text")
|
||||
private String exceptionDetail;
|
||||
|
||||
/**
|
||||
|
|
|
@ -6,8 +6,6 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
|||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author jie
|
||||
* @date 2018-11-24
|
||||
|
@ -21,6 +19,6 @@ public interface LoggingRepository extends JpaRepository<Logging,Long>, JpaSpeci
|
|||
* @param date2
|
||||
* @return
|
||||
*/
|
||||
@Query(value = "select count(*) FROM (select * FROM log where createTime between ?1 and ?2 GROUP BY requestIp) as s",nativeQuery = true)
|
||||
@Query(value = "select count(*) FROM (select requestIp FROM log where createTime between ?1 and ?2 GROUP BY requestIp) as s",nativeQuery = true)
|
||||
Long findIp(String date1, String date2);
|
||||
}
|
||||
|
|
|
@ -83,9 +83,7 @@ public class LoggingServiceImpl implements LoggingService {
|
|||
username = user.getUsername();
|
||||
|
||||
}
|
||||
if (params.length() > 1000){
|
||||
params = params.substring(0,999);
|
||||
}
|
||||
logging.setMethod(methodName);
|
||||
logging.setUsername(username);
|
||||
logging.setParams(params + " }");
|
||||
loggingRepository.save(logging);
|
||||
|
|
|
@ -101,9 +101,13 @@ public class MenuServiceImpl implements MenuService {
|
|||
List<Map<String,Object>> list = new LinkedList<>();
|
||||
menus.forEach(menu -> {
|
||||
if (menu!=null){
|
||||
List<Menu> menuList = menuRepository.findByPid(menu.getId());
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("id",menu.getId());
|
||||
map.put("label",menu.getName());
|
||||
if(menuList!=null && menuList.size()!=0){
|
||||
map.put("children",getMenuTree(menuList));
|
||||
}
|
||||
list.add(map);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue