Announcement Announcement Module
Collapse
No announcement yet.
Spring AOP Logger Aspect printing Aspect class name instead of actual class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring AOP Logger Aspect printing Aspect class name instead of actual class

    Issue: The log statement within the aspect is printing the Aspect class instead of the Business class. I believe I am missing something in the code; but am unable to identify the issue. Could somebody help me with this?

    Log entry with my current code:
    2014-04-23 15:15:23,499 DEBUG: com.company.aspect.LoggingAspect logBefore [Started]

    Expected log entry:
    2014-04-23 15:15:23,499 DEBUG: com.company.BusinessClassImpl doSomething [Started]

    Details:
    Code:
    package com.company.aspect.LoggingAspect;
    @Aspect
    public class LoggingAspect {
        @Before("execution(* com.company..*.*(..))")
        public void logBefore(JoinPoint joinPoint) {
            LocationAwareLogger logger = (LocationAwareLogger) LoggerFactory
                    .getLogger(joinPoint.getTarget().getClass());
            logger.log(null, joinPoint.getTarget().getClass(),
                    LocationAwareLogger.DEBUG_INT, "Started", null, null);
        }
    }
    log4j.xml:
    Code:
    <appender name="MainLogger" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="file" value="logs/logfile.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ISO8601} %p: %C %M [%m]%n" />
        </layout>
    </appender>
    spring-config.xml:
    Code:
    <aop:aspectj-autoproxy />
    <context:annotation-config />
    <context:component-scan base-package="com.company" />
    <bean id="logAspect" class="com.company.aspect.LoggingAspect" />
    <bean id="log4jInitialization"
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
        <property name="targetMethod" value="initLogging" />
        <property name="arguments">
            <value>classpath:config/log4j.xml</value>
        </property>
    </bean>
    Business class:
    Code:
    package com.company.business;
    public class BusinessClassImpl implements BusinessClass {
        public void doSomething() {
        }
    }
    I also tried with an interceptor and observed the same. The details are as below.
    spring-config.xml
    Code:
    <bean id="BusinessClassBean" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces">
            <value>com.company.BusinessClass</value>
        </property>
        <property name="target">
            <ref bean="BusinessClassImpl" />
        </property>
        <property name="interceptorNames">
            <list>
                <value>loggingInterceptor</value>
            </list>
        </property>
    </bean>
    
    <bean id="BusinessClassImpl" class="com.company.BusinessClassImpl" />
    
    <bean id=" loggingInterceptor"
        class="com.company.interceptor.LoggingInterceptor" />
    pom.xml
    Code:
    <dependencies>
        <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <!-- Spring AOP + AspectJ -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.11</version>
        </dependency>
    
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.11</version>
        </dependency>
    
        <!-- SLF4j with Log4j dependencies -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
    
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
    
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>
    Last edited by cherumb; Apr 23rd, 2014, 08:36 PM.

  • #2
    We are currently in the process of moving to stackoverflow.com for our forums. If you're still having this issue please consider asking it again athttp://stackoverflow.com using the #spring tag. If you do post it there, please reply here with the link. Thanks!

    Comment

    Working...
    X