Spring 使用AOP和Log4J实现日志记录

Spring 使用AOP和Log4J实现日志记录,spring,configuration,log4j,aspectj,Spring,Configuration,Log4j,Aspectj,我尝试将使用方面的自动日志添加到使用JavaEE和Spring(core+mvc+security…)开发的web应用程序中。依赖项由maven管理,应用服务器是Glassfish服务器。aspect部分似乎可以工作,但是日志文件没有获得我通过aspect发送的日志 我在my web.xml中设置了日志记录配置,添加了以下内容: <listener id="myLogger"> <listener-class>org.springframework.web.uti

我尝试将使用方面的自动日志添加到使用JavaEE和Spring(core+mvc+security…)开发的web应用程序中。依赖项由maven管理,应用服务器是Glassfish服务器。aspect部分似乎可以工作,但是日志文件没有获得我通过aspect发送的日志

我在my web.xml中设置了日志记录配置,添加了以下内容:

<listener id="myLogger">
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
我还配置了一个方面(使用AspectJ),以便在调用以“get”开头的函数时记录所有信息。以下是这方面的课程:

import org.apache.log4j.Logger;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class PropertyChangeTracker {

private Logger logger = Logger.getLogger(PropertyChangeTracker.getClass());

@Before("execution(String get*(..))")
public void trackCalls(){
    logger.info("controller called !!");
}
}
aspect-config.xml中的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="propertyChangeTracker" class="services.aspects.PropertyChangeTracker">   </bean>

<aop:aspectj-autoproxy>
    <aop:include name="propertyChangeTracker"/>
</aop:aspectj-autoproxy>
</beans>

我在调试模式下对此进行了测试,调用了我的“trackCalls”方法,没有任何问题,但日志文件中没有记录任何信息。我感觉在我的应用程序中有两个不同的记录器,PropertyChangeTracker类使用的记录器不是我想要的。。。你知道我哪里搞错了吗


提前感谢您的帮助

我不确定,但您的log4j配置看起来可疑。尝试在log4j.properties中仅添加log4j.rootLogger=INFO文件。

我不确定,但您的log4j配置看起来可疑。尝试只在log4j.properties中添加log4j.rootLogger=INFO文件。您保存了我的一天!我不敢相信这是一个如此愚蠢的错误。。。我花了太多时间阅读spring和aop配置,以至于我没有检查编写配置文件的人是否正确地完成了配置!非常感谢,现在可以用了@科拉里:塔基的评论回答了你的问题。所以你应该把他的答案设为接受答案。看见
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="propertyChangeTracker" class="services.aspects.PropertyChangeTracker">   </bean>

<aop:aspectj-autoproxy>
    <aop:include name="propertyChangeTracker"/>
</aop:aspectj-autoproxy>
</beans>