Coralie
Coralie

Reputation: 107

Implement logging with AOP and Log4J

I try to add automatic logging using aspects to my web app which is developed with Java EE and Spring (core + mvc + security...). Dependencies are managed by maven and the application server is a Glassfish server. The aspect part seems to work but the logging file doesn't get the logs I send through the aspect.

I set the logging configuration in my web.xml adding this:

<listener id="myLogger">
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

The property file is the following (and it works because the log file is well created in my system folders) :

# 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

I also configured an aspect (using AspectJ) in order to log all information in case of a call to a function starting with "get". Here is the class for this aspect :

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 !!");
}
}

The configuration in 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>

I tested this in the debug mode and my "trackCalls" method is called without any trouble but no information gets logged into the log file. I have the feeling there are two different loggers in my app and the one used by the PropertyChangeTracker class is not the one I want... Do you see where I set something wrong?

Thanks in advance for your help!

Upvotes: 0

Views: 6388

Answers (1)

Taky
Taky

Reputation: 5344

I am not sure, but your log4j configuration looks suspected. Try to add only log4j.rootLogger=INFO, file in log4j.properties.

Upvotes: 1

Related Questions