AspectJ Load Time weaving working in unit tests but NOT at runtime in spring boot (maven) application

624 views Asked by At

We have JAVA micro service which constitutes lot of logging. The logging as of now is inconsistent. As a part of improvement, I plan to have an aspect so as we log/append certain data points against each info/error/warn being logged. The data points will be picked form current thread context. I’m sharing the code. Problem: The load time weaving works with unit tests, but when the application is run, nothing happens. That means the weaving is not taking place. The application use tomcat hosting.

Pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>a.b.services</groupId>
    <artifactId>a-b-processor</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>a-b-processor</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jose4j.version>0.6.2</jose4j.version>
        <spring.jms.version>4.3.11.RELEASE</spring.jms.version>
        <org.json.version>20180130</org.json.version>
        <aspectjrt.version>1.9.1</aspectjrt.version>
        <aspectjweaver.version>1.9.1</aspectjweaver.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.jms.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectjrt.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectjweaver.version}</version>
        </dependency>
    </dependencies>
    <build>
        <defaultGoal>clean install</defaultGoal>
        <finalName>${project.artifactId}-${project.version}</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.0</version>
                    <configuration>
                        <argLine>
                            -javaagent:"${settings.localRepository}"\org\aspectj\aspectjweaver\${aspectj.version}\aspectjweaver-${aspectj.version}.jar
                        </argLine>
                        <useSystemClassLoader>true</useSystemClassLoader>
                        <forkCount>3</forkCount>
                        <reuseForks>true</reuseForks>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco-plugin.version}</version>
                <configuration>
                    <excludes>
                        <exclude>**/aspect/marker/*.class</exclude>
                        <exclude>**/controller/*.class</exclude>
                        <exclude>**/enums/*.class</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.5.3</version>
                <configuration>
                    <tagNameFormat>@{project.version}</tagNameFormat>
                    <preparationGoals>clean verify replacer:replace docker:build -DdockerRepo=greenbuild-repo
                        -DpushImageTag scm:checkin -Dmessage="[maven-release-plugin] prepare release" promote:artifacts
                        deploy:deploy
                    </preparationGoals>
                    <completionGoals>clean verify replacer:replace scm:checkin -Dmessage="[maven-release-plugin] prepare
                        for next development iteration"
                    </completionGoals>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jxr-plugin</artifactId>
                <version>${jxr-plugin.version}</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
                <version>${findbugs-plugin.version}</version>
                <configuration>
                    <effort>Max</effort>
                    <threshold>Low</threshold>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>${pmd-plugin.version}</version>
                <configuration>
                    <targetJdk>${java.version}</targetJdk>
                    <skipEmptyReport>false</skipEmptyReport>
                </configuration>
            </plugin>
        </plugins>
    </reporting>
    <scm>
        <developerConnection>scm:git:http://github.com/prr.git</developerConnection>
        <tag>1.0.1</tag>
    </scm>
</project>

aspectj aop.xml

    <aspectj>
    <aspects>
        <aspect name="com.e.s.aspect.uniformLoggerAspect"/>
        <weaver options="-debug -showWeaveInfo">
            <include call="* org.slf4j.Logger.*(..)"/>
        </weaver>
    </aspects>
</aspectj>

uniformLoggerAspect

    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.jdbc.core.namedparam.NamedParameterUtils;

@Aspect
public class uniformLoggerAspect {

    @Around("uniformLoggerPointCut()")
    public Object formatMessageLog(final Object msg, final Throwable exception
            , final ProceedingJoinPoint invocation
            , final JoinPoint.EnclosingStaticPart callerContext) throws Throwable {

        return formatLogAndProceed(msg, exception, invocation, callerContext);
    }

    private Object formatLogAndProceed(final Object msg, final Throwable exception
            , final ProceedingJoinPoint invocation
            , final JoinPoint.EnclosingStaticPart callerContext
    ) throws Throwable {

        final Object[] arguments = (Object[]) invocation.getArgs();

        Context context = Context.getCurrentContext();

        if (context != null) {
            arguments[0] = String.format("Cor Id:[%s], PId[%s], IId:[%s], PkId:[%s] LId:[{%s}] [%s]", context.getCorId(), "123", "xyz", context.getPkId(), context.getLId(), arguments[0]);

        return invocation.proceed(arguments);
    }

    @Pointcut("call(* org.slf4j.Logger.*(..))")
    public void uniformLoggerPointCut() {
    }
}
0

There are 0 answers