Skip to content

Commit

Permalink
Add version with compile-time weaving
Browse files Browse the repository at this point in the history
In order to activate compile-time weaving use `-Pcompile-time`.
  • Loading branch information
ppkarwasz committed Jun 14, 2024
1 parent 8a02e7c commit 4ddaca0
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 5 deletions.
90 changes: 90 additions & 0 deletions log4j-samples-aspectj/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<properties>
<!-- Used by Spring Boot starters -->
<log4j2.version>${log4j.version}</log4j2.version>

<!-- Dependency versions -->
<aspectj.version>1.9.22.1</aspectj.version>
</properties>

<dependencies>
Expand All @@ -55,11 +58,98 @@
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<scope>runtime</scope>
</dependency>

</dependencies>

<build>
<plugins>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>make-executable-jar</id>
<goals>
<goal>repackage</goal>
</goals>
<phase>package</phase>
<configuration>
<mainClass>org.apache.logging.log4j2.samples.aspectj.Main</mainClass>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>

<profiles>
<profile>
<id>compile-time</id>

<dependencies />
<build>
<plugins>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.15.0</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>apply-trace-logging</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<complianceLevel>11</complianceLevel>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>make-executable-jar</id>
<configuration>
<profiles>compile-time</profiles>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>
</profile>

</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class LoggingAspect {
private static final Logger LOGGER = LogManager.getLogger();
private static final FlowMessageFactory FLOW_MESSAGE_FACTORY = new DefaultFlowMessageFactory();

public LoggingAspect() {}

private static StackTraceElement getLocation(final JoinPoint joinPoint) {
final SourceLocation location = joinPoint.getSourceLocation();
final String methodName = joinPoint.getSignature().getName();
Expand All @@ -55,7 +57,7 @@ private static StackTraceElement getLocation(final JoinPoint joinPoint) {
return new StackTraceElement(location.getWithinType().getName(), methodName, fileName, lineNumber);
}

@Pointcut("within(org.apache.logging.log4j2.samples.aspectj.*)")
@Pointcut("execution(public * org.apache.logging..*(..))")
public void traceLogging() {}

@Before("traceLogging()")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@SpringBootApplication
@EnableAspectJAutoProxy
public class Main implements CommandLineRunner {

private final Logger logger = LogManager.getLogger();
Expand Down
29 changes: 28 additions & 1 deletion log4j-samples-aspectj/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to you under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
spring:
config:
activate:
on-profile: default
aop:
proxy-target-class: false
proxy-target-class: false
auto: true
---
spring:
config:
activate:
on-profile: compile-time
aop:
auto: false
2 changes: 1 addition & 1 deletion log4j-samples-aspectj/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
https://logging.apache.org/xml/ns/log4j-config-2.xsd">
<Appenders>
<Console name="CONSOLE">
<PatternLayout pattern="%d [%t] %p %C:%M - %m%n"/>
<PatternLayout pattern="%d [%t] %p %C#%M.%L - %m%n"/>
</Console>
</Appenders>
<MarkerFilter marker="FLOW"
Expand Down
37 changes: 37 additions & 0 deletions spotbugs-exclude.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to you under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<FindBugsFilter
xmlns="https://github.com/spotbugs/filter/3.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/3.1.0/spotbugs/etc/findbugsfilter.xsd">
<Match>
<Not>
<Bug category="SECURITY"/>
</Not>
<Rank value="9"/>
</Match>
<Match>
<Or>
<!-- Spotbugs `4.8.4.0` introducing `SING_SINGLETON_GETTER_NOT_SYNCHRONIZED` contains a regression[1], disabling it.
[1] https://github.com/spotbugs/spotbugs/issues/2932 -->
<Bug pattern="SING_SINGLETON_GETTER_NOT_SYNCHRONIZED"/>
<!-- `SING_SINGLETON_HAS_NONPRIVATE_CONSTRUCTOR` check mostly generates noise, disabling it. -->
<Bug pattern="SING_SINGLETON_HAS_NONPRIVATE_CONSTRUCTOR"/>
</Or>
</Match>
</FindBugsFilter>

0 comments on commit 4ddaca0

Please sign in to comment.