|
|
@ -12,12 +12,14 @@ |
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
* limitations under the License. |
|
|
|
*//*
|
|
|
|
*/ |
|
|
|
|
|
|
|
package org.nl.common.logging.aspect; |
|
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.aspectj.lang.JoinPoint; |
|
|
|
import org.aspectj.lang.ProceedingJoinPoint; |
|
|
@ -31,6 +33,10 @@ import org.nl.common.utils.SecurityUtils; |
|
|
|
import org.nl.common.utils.StringUtils; |
|
|
|
import org.nl.common.utils.ThrowableUtil; |
|
|
|
import org.nl.common.logging.domain.Log; |
|
|
|
import org.nl.config.IdUtil; |
|
|
|
import org.nl.system.service.logging.ISysLogService; |
|
|
|
import org.nl.system.service.logging.dao.SysLog; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.web.bind.annotation.RequestBody; |
|
|
|
import org.springframework.web.bind.annotation.RequestParam; |
|
|
@ -41,79 +47,67 @@ import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.lang.reflect.Method; |
|
|
|
import java.lang.reflect.Parameter; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
*/ |
|
|
|
/** |
|
|
|
* @author Zheng Jie |
|
|
|
* @date 2018-11-24 |
|
|
|
*//*
|
|
|
|
|
|
|
|
@Component |
|
|
|
@Aspect |
|
|
|
@Slf4j |
|
|
|
public class LogAspect { |
|
|
|
|
|
|
|
private final LogService logService; |
|
|
|
@Autowired |
|
|
|
private ISysLogService logService; |
|
|
|
|
|
|
|
ThreadLocal<Long> currentTime = new ThreadLocal<>(); |
|
|
|
|
|
|
|
public LogAspect(LogService logService) { |
|
|
|
public LogAspect(ISysLogService logService) { |
|
|
|
this.logService = logService; |
|
|
|
} |
|
|
|
|
|
|
|
*/ |
|
|
|
/** |
|
|
|
* 配置切入点 |
|
|
|
*//*
|
|
|
|
|
|
|
|
|
|
|
|
@Pointcut("@annotation(org.nl.common.logging.annotation.Log)") |
|
|
|
public void logPointcut() { |
|
|
|
// 该方法无方法体,主要为了让同类中其他方法使用此切入点
|
|
|
|
} |
|
|
|
|
|
|
|
*/ |
|
|
|
/** |
|
|
|
* 配置环绕通知,使用在方法logPointcut()上注册的切入点 |
|
|
|
* |
|
|
|
* @param joinPoint join point for advice |
|
|
|
*//*
|
|
|
|
|
|
|
|
@Around("logPointcut()") |
|
|
|
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { |
|
|
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
|
|
|
HttpServletRequest request = attributes.getRequest(); |
|
|
|
HttpServletResponse response = attributes.getResponse(); |
|
|
|
// HttpServletRequest request = RequestHolder.getHttpServletRequest();
|
|
|
|
|
|
|
|
|
|
|
|
String trackId = UUID.randomUUID().toString(); |
|
|
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); |
|
|
|
Method method = signature.getMethod(); |
|
|
|
// 方法路径
|
|
|
|
String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()"; |
|
|
|
String params=getParameter(method, joinPoint.getArgs()); |
|
|
|
String params = getParameter(method, joinPoint.getArgs()); |
|
|
|
|
|
|
|
log.info("请求uri:{}", request.getRequestURI()); |
|
|
|
log.info("请求方法:{}",methodName); |
|
|
|
log.info("请求方法参数:{}",params); |
|
|
|
org.nl.common.logging.annotation.Log logInfo = method.getAnnotation(org.nl.common.logging.annotation.Log.class); |
|
|
|
|
|
|
|
//是否输出到日志文件
|
|
|
|
if (logInfo.isPrintToLogFile()) { |
|
|
|
log.info("track_id:{},请求方法:{},请求方法参数:{}", trackId, methodName, params); |
|
|
|
} |
|
|
|
HttpServletRequest request = RequestHolder.getHttpServletRequest(); |
|
|
|
String requestIp = StringUtils.getIp(request); |
|
|
|
Object result; |
|
|
|
currentTime.set(System.currentTimeMillis()); |
|
|
|
result = joinPoint.proceed(); |
|
|
|
Log log = new Log("INFO",System.currentTimeMillis() - currentTime.get()); |
|
|
|
currentTime.remove(); |
|
|
|
|
|
|
|
logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request),joinPoint, log); |
|
|
|
long startTime = System.currentTimeMillis(); |
|
|
|
try { |
|
|
|
result = joinPoint.proceed(); |
|
|
|
//是否把日志存到日志表
|
|
|
|
if (logInfo.isAddLogTable()) { |
|
|
|
SysLog log = new SysLog("INFO", System.currentTimeMillis() - startTime); |
|
|
|
logService.save(getUsername(), StringUtils.getBrowser(request), requestIp, joinPoint, log); |
|
|
|
} |
|
|
|
} catch (Exception ex) { |
|
|
|
log.error("track_id:{},error:{}", trackId, ex.getMessage()); |
|
|
|
SysLog log = new SysLog("ERROR", System.currentTimeMillis() - startTime); |
|
|
|
log.setException_detail(ThrowableUtil.getStackTrace(ex).getBytes()); |
|
|
|
logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint) joinPoint, log); |
|
|
|
throw ex; |
|
|
|
} |
|
|
|
return result; |
|
|
|
} |
|
|
|
|
|
|
|
*/ |
|
|
|
/** |
|
|
|
* 根据方法和传入的参数获取请求参数 |
|
|
|
*//*
|
|
|
|
|
|
|
|
|
|
|
|
private String getParameter(Method method, Object[] args) { |
|
|
|
List<Object> argList = new ArrayList<>(); |
|
|
@ -142,29 +136,13 @@ public class LogAspect { |
|
|
|
return argList.size() == 1 ? JSONUtil.toJsonStr(argList.get(0)) : JSONUtil.toJsonStr(argList); |
|
|
|
} |
|
|
|
|
|
|
|
*/ |
|
|
|
/** |
|
|
|
* 配置异常通知 |
|
|
|
* |
|
|
|
* @param joinPoint join point for advice |
|
|
|
* @param e exception |
|
|
|
*//*
|
|
|
|
|
|
|
|
@AfterThrowing(pointcut = "logPointcut()", throwing = "e") |
|
|
|
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { |
|
|
|
Log log = new Log("ERROR",System.currentTimeMillis() - currentTime.get()); |
|
|
|
currentTime.remove(); |
|
|
|
log.setExceptionDetail(ThrowableUtil.getStackTrace(e).getBytes()); |
|
|
|
HttpServletRequest request = RequestHolder.getHttpServletRequest(); |
|
|
|
logService.save(getUsername(), StringUtils.getBrowser(request), StringUtils.getIp(request), (ProceedingJoinPoint)joinPoint, log); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getUsername() { |
|
|
|
try { |
|
|
|
return SecurityUtils.getCurrentUsername(); |
|
|
|
return SecurityUtils.getCurrentNickName(); |
|
|
|
}catch (Exception e){ |
|
|
|
return ""; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
*/ |
|
|
|