log4j日志+面向切面监控异常
log4j.xml
src/main/resources
--------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<appender name="activexAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/log/yangGuan/admin/yangGuan-admin.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<!-- <param name="DatePattern" value="'.'yyyy-MM-dd-HH-mm" /> -->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="[%d{MMdd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" />
</layout>
</appender>
<!-- Application Loggers -->
<logger name="com.fly.test">
<level value="info" />
</logger>
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="info" />
</logger>
<logger name="org.springframework.beans">
<level value="info" />
</logger>
<logger name="org.springframework.context">
<level value="info" />
</logger>
<logger name="org.springframework.web">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="info" />
<appender-ref ref="console" />
<appender-ref ref="activexAppender"/>
</root>
</log4j:configuration>
=========================================================================================
spring.xml
--------------------------------------------------------------------------------------------------------------------------
<aop:aspectj-autoproxy proxy-target-class="true" />
<context:component-scan base-package="com.fly.test" />
<task:annotation-driven/>
=========================================================================================
切面类
--------------------------------------------------------------------------------------------------------------------------
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspect {
private final static Logger LOG = LogManager.getLogger(MyAspect.class);
@Pointcut("execution(* com.fly.test.test1..*.*(..))")
private void pointcut() {
}
@Before("com.fly.test.test1.controller.MyAspect.pointcut()")
private void before(JoinPoint joinPoint) {
//System.out.println(joinPoint.getTarget() + "----------------------Before---------------------");
}
/**
* 异常通知,用于拦截记录异常日志
*
* @param joinPoint
* @param e
*/
@AfterThrowing(pointcut = "com.fly.test.test1.controller.MyAspect.pointcut()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
LOG.error("----------------------异常开始---------------------");
LOG.error("异常方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
LOG.error("异常信息:", e);
LOG.error("----------------------异常结束---------------------");
}
}
=========================================================================================
参考文献:
--------------------------------------------------------------------------------------------------------------------------
https://blog.csdn.net/zhoulenihao/article/details/24995397
https://blog.csdn.net/loongshawn/article/details/72303040?utm_source=tuicool&utm_medium=referral
https://blog.csdn.net/registerin1030/article/details/51192197
https://www.cnblogs.com/rembau/p/5201001.html
http://elim.iteye.com/blog/2394629
http://elim.iteye.com/blog/2394762
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步