Commit a1c700f4 by fsn

完成异常落库

parent 980d11c8
...@@ -3,11 +3,15 @@ package com.stylefeng.guns.core.aop; ...@@ -3,11 +3,15 @@ package com.stylefeng.guns.core.aop;
import com.stylefeng.guns.common.constant.tips.ErrorTip; import com.stylefeng.guns.common.constant.tips.ErrorTip;
import com.stylefeng.guns.common.exception.BizExceptionEnum; import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException; import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.core.log.ILog;
import com.stylefeng.guns.core.support.HttpKit; import com.stylefeng.guns.core.support.HttpKit;
import com.stylefeng.guns.core.util.SpringContextHolder;
import com.stylefeng.guns.core.util.ToolUtil;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.CredentialsException; import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.DisabledAccountException; import org.apache.shiro.authc.DisabledAccountException;
import org.springframework.context.annotation.DependsOn;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
...@@ -24,10 +28,14 @@ import javax.naming.NoPermissionException; ...@@ -24,10 +28,14 @@ import javax.naming.NoPermissionException;
* @date 2016年11月12日 下午3:19:56 * @date 2016年11月12日 下午3:19:56
*/ */
@ControllerAdvice @ControllerAdvice
@DependsOn("springContextHolder")
public class GlobalExceptionHandler { public class GlobalExceptionHandler {
private Logger log = Logger.getLogger(this.getClass()); private Logger log = Logger.getLogger(this.getClass());
private ILog logFactory = SpringContextHolder.getBean(ILog.class);
/** /**
* 拦截业务异常 * 拦截业务异常
* *
...@@ -37,6 +45,7 @@ public class GlobalExceptionHandler { ...@@ -37,6 +45,7 @@ public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody @ResponseBody
public ErrorTip notFount(BussinessException e) { public ErrorTip notFount(BussinessException e) {
logFactory.doLog("业务异常", e.toString(), false);
HttpKit.getRequest().setAttribute("tip", e.getMessage()); HttpKit.getRequest().setAttribute("tip", e.getMessage());
return new ErrorTip(e.getCode(), e.getMessage()); return new ErrorTip(e.getCode(), e.getMessage());
} }
...@@ -50,6 +59,8 @@ public class GlobalExceptionHandler { ...@@ -50,6 +59,8 @@ public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody @ResponseBody
public ErrorTip notFount(RuntimeException e) { public ErrorTip notFount(RuntimeException e) {
String msg = ToolUtil.getExceptionMsg(e);
logFactory.doLog("Runtime异常", msg, false);
log.error("服务器异常:", e); log.error("服务器异常:", e);
HttpKit.getRequest().setAttribute("tip", "服务器未知运行时异常"); HttpKit.getRequest().setAttribute("tip", "服务器未知运行时异常");
return new ErrorTip(BizExceptionEnum.SERVER_ERROR); return new ErrorTip(BizExceptionEnum.SERVER_ERROR);
......
...@@ -30,6 +30,10 @@ public class LogFactory implements ILog { ...@@ -30,6 +30,10 @@ public class LogFactory implements ILog {
log.setSucceed((succeed) ? "1" : "0"); log.setSucceed((succeed) ? "1" : "0");
log.setUserid(String.valueOf(user.getId())); log.setUserid(String.valueOf(user.getId()));
log.setLogname(logName); log.setLogname(logName);
try {
operationLogMapper.insert(log); operationLogMapper.insert(log);
}catch (Exception e){
//e.printStackTrace();
}
} }
} }
/** /**
* Copyright (c) 2015-2016, Chill Zhuang 庄骞 (smallchill@163.com). * Copyright (c) 2015-2016, Chill Zhuang 庄骞 (smallchill@163.com).
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...@@ -15,52 +15,46 @@ ...@@ -15,52 +15,46 @@
*/ */
package com.stylefeng.guns.core.util; package com.stylefeng.guns.core.util;
import com.stylefeng.guns.core.support.StrKit;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.Arrays; import java.util.*;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set;
import com.stylefeng.guns.core.support.StrKit;
/** /**
* 高频方法集合类 * 高频方法集合类
*/ */
public class ToolUtil { public class ToolUtil {
/** /**
* 获取随机车牌号码 * 获取异常的具体信息
* @date 2017年2月24日 下午10:12:52 *
* @author fengshuonan
* @Date 2017/3/30 9:21
* @version 2.0
*/ */
public static String randomPlateNumber(){ public static String getExceptionMsg(Exception e) {
// String province = "冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼渝京津沪"; StringWriter sw = new StringWriter();
String province = "京"; try{
String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; e.printStackTrace(new PrintWriter(sw));
String nums = "0123456789"; }finally {
Random random = new Random(); try {
StringBuffer sb = new StringBuffer(); sw.close();
sb.append(province.charAt(random.nextInt(province.length()))); } catch (IOException e1) {
sb.append(letters.charAt(random.nextInt(letters.length()))); e1.printStackTrace();
for(int i=0;i<5;i++){ }
sb.append(nums.charAt(random.nextInt(nums.length())));
} }
return sb.toString(); return sw.getBuffer().toString().replaceAll("\\$","T");
} }
/** /**
* @Description 主键id * @Description 主键id
* @author fengshuonan * @author fengshuonan
*/ */
public static String getUid(){ public static String getUid() {
return getRandomNum(); return getRandomNum();
} }
...@@ -68,7 +62,7 @@ public class ToolUtil { ...@@ -68,7 +62,7 @@ public class ToolUtil {
* @Description 随机数字 * @Description 随机数字
* @author fengshuonan * @author fengshuonan
*/ */
public static String getRandomNum(){ public static String getRandomNum() {
return Calendar.getInstance().getTimeInMillis() + generateCellPhoneValNum(); return Calendar.getInstance().getTimeInMillis() + generateCellPhoneValNum();
} }
...@@ -77,8 +71,8 @@ public class ToolUtil { ...@@ -77,8 +71,8 @@ public class ToolUtil {
* @author fengshuonan * @author fengshuonan
*/ */
public static String generateCellPhoneValNum() { public static String generateCellPhoneValNum() {
String[] beforeShuffle = new String[] { "1", "2", "3", "4", "5", "6", String[] beforeShuffle = new String[]{"1", "2", "3", "4", "5", "6",
"7", "8", "9", "0" }; "7", "8", "9", "0"};
List<String> list = Arrays.asList(beforeShuffle); List<String> list = Arrays.asList(beforeShuffle);
Collections.shuffle(list); Collections.shuffle(list);
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
...@@ -95,10 +89,8 @@ public class ToolUtil { ...@@ -95,10 +89,8 @@ public class ToolUtil {
* 相同的条件有两个,满足其一即可:<br> * 相同的条件有两个,满足其一即可:<br>
* 1. obj1 == null && obj2 == null; 2. obj1.equals(obj2) * 1. obj1 == null && obj2 == null; 2. obj1.equals(obj2)
* *
* @param obj1 * @param obj1 对象1
* 对象1 * @param obj2 对象2
* @param obj2
* 对象2
* @return 是否相等 * @return 是否相等
*/ */
public static boolean equals(Object obj1, Object obj2) { public static boolean equals(Object obj1, Object obj2) {
...@@ -108,8 +100,7 @@ public class ToolUtil { ...@@ -108,8 +100,7 @@ public class ToolUtil {
/** /**
* 计算对象长度,如果是字符串调用其length函数,集合类调用其size函数,数组调用其length属性,其他可遍历对象遍历计算长度 * 计算对象长度,如果是字符串调用其length函数,集合类调用其size函数,数组调用其length属性,其他可遍历对象遍历计算长度
* *
* @param obj * @param obj 被计算长度的对象
* 被计算长度的对象
* @return 长度 * @return 长度
*/ */
public static int length(Object obj) { public static int length(Object obj) {
...@@ -154,10 +145,8 @@ public class ToolUtil { ...@@ -154,10 +145,8 @@ public class ToolUtil {
/** /**
* 对象中是否包含元素 * 对象中是否包含元素
* *
* @param obj * @param obj 对象
* 对象 * @param element 元素
* @param element
* 元素
* @return 是否包含 * @return 是否包含
*/ */
public static boolean contains(Object obj, Object element) { public static boolean contains(Object obj, Object element) {
...@@ -212,8 +201,7 @@ public class ToolUtil { ...@@ -212,8 +201,7 @@ public class ToolUtil {
/** /**
* 对象是否不为空(新增) * 对象是否不为空(新增)
* *
* @param obj * @param obj String,List,Map,Object[],int[],long[]
* String,List,Map,Object[],int[],long[]
* @return * @return
*/ */
public static boolean isNotEmpty(Object o) { public static boolean isNotEmpty(Object o) {
...@@ -223,8 +211,7 @@ public class ToolUtil { ...@@ -223,8 +211,7 @@ public class ToolUtil {
/** /**
* 对象是否为空 * 对象是否为空
* *
* @param obj * @param obj String,List,Map,Object[],int[],long[]
* String,List,Map,Object[],int[],long[]
* @return * @return
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
...@@ -267,8 +254,7 @@ public class ToolUtil { ...@@ -267,8 +254,7 @@ public class ToolUtil {
/** /**
* 对象组中是否存在 Empty Object * 对象组中是否存在 Empty Object
* *
* @param os * @param os 对象组
* 对象组
* @return * @return
*/ */
public static boolean isOneEmpty(Object... os) { public static boolean isOneEmpty(Object... os) {
...@@ -326,10 +312,8 @@ public class ToolUtil { ...@@ -326,10 +312,8 @@ public class ToolUtil {
/** /**
* 格式化文本 * 格式化文本
* *
* @param template * @param template 文本模板,被替换的部分用 {} 表示
* 文本模板,被替换的部分用 {} 表示 * @param values 参数值
* @param values
* 参数值
* @return 格式化后的文本 * @return 格式化后的文本
*/ */
public static String format(String template, Object... values) { public static String format(String template, Object... values) {
...@@ -339,10 +323,8 @@ public class ToolUtil { ...@@ -339,10 +323,8 @@ public class ToolUtil {
/** /**
* 格式化文本 * 格式化文本
* *
* @param template * @param template 文本模板,被替换的部分用 {key} 表示
* 文本模板,被替换的部分用 {key} 表示 * @param map 参数值对
* @param map
* 参数值对
* @return 格式化后的文本 * @return 格式化后的文本
*/ */
public static String format(String template, Map<?, ?> map) { public static String format(String template, Map<?, ?> map) {
...@@ -488,7 +470,7 @@ public class ToolUtil { ...@@ -488,7 +470,7 @@ public class ToolUtil {
* @return 切掉后的字符串,若后缀不是 suffix, 返回原字符串 * @return 切掉后的字符串,若后缀不是 suffix, 返回原字符串
*/ */
public static String removeSuffix(String str, String suffix) { public static String removeSuffix(String str, String suffix) {
if(isEmpty(str) || isEmpty(suffix)){ if (isEmpty(str) || isEmpty(suffix)) {
return str; return str;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment