我尝试使用方面添加自动日志记录到我的Web应用程序,该应用程序是使用Java EE和Spring(core + mvc + security ...)开发的。依赖关系由maven管理,应用程序服务器是Glassfish服务器。方面部分似乎工作,但日志文件不会获取我通过方面发送的日志。
我在web.xml中设置日志配置,添加:
<listener id="myLogger">
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
属性文件如下(它的工作原理是因为我的系统文件夹中创建了日志文件):
# Root logger option
log4j.rootLogger=INFO, file
log4j.rootLogger=WARN, file
log4j.rootLogger=ERROR, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\Users\\chaese\\Documents\\Dev\\LogsMEANS\\logging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
我还配置了一个方面(使用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类使用的记录器不是我想要的那个......你看到我设置错误的位置了吗?
提前感谢您的帮助!
答案 0 :(得分:1)
我不确定,但您的log4j配置看起来有疑问。尝试在log4j.properties中仅添加log4j.rootLogger = INFO文件。