Skip to content

Commit 757a629

Browse files
committed
Added example for log4j2 api
1 parent c4d1ac9 commit 757a629

File tree

7 files changed

+96
-3
lines changed

7 files changed

+96
-3
lines changed

README.MD

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ A repository containing different java tutorials
1212
## Testing 🎯
1313
- [Unit Testing Static Methods with Mockito](mock-statics-with-mockito)
1414
- [Unit Testing Java Util Logging logs with LogCaptor](log-captor-examples/log-captor-with-java-util-logging)
15-
- [Unit Testing Log4J logs with LogCaptor](log-captor-examples/log-captor-with-log4j-core)
15+
- [Unit Testing Log4J API logs with LogCaptor](log-captor-examples/log-captor-with-log4j-api)
16+
- [Unit Testing Log4J Core logs with LogCaptor](log-captor-examples/log-captor-with-log4j-core)
1617
- [Unit Testing SLF4J Logback logs with LogCaptor](log-captor-examples/log-captor-with-slf4j-logback-classic)
1718
- [Unit Testing SLF4J Log4j logs with LogCaptor](log-captor-examples/log-captor-with-slf4j-log4j)
1819
- [Unit Testing Flogger logs with LogCaptor](log-captor-examples/log-captor-with-flogger)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<artifactId>log-captor-examples-parent</artifactId>
9+
<groupId>io.github.hakky54</groupId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>log-captor-with-log4j-api</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
<packaging>jar</packaging>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.apache.logging.log4j</groupId>
20+
<artifactId>log4j-api</artifactId>
21+
<version>${version.log4j}</version>
22+
</dependency>
23+
</dependencies>
24+
25+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2022 Thunderberry.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package nl.altindag.log4j;
17+
18+
import org.apache.logging.log4j.LogManager;
19+
import org.apache.logging.log4j.Logger;
20+
21+
public class FooService {
22+
23+
private static final Logger LOGGER = LogManager.getLogger(FooService.class);
24+
25+
public void hello() {
26+
LOGGER.info("Hello there friend!");
27+
}
28+
29+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2022 Thunderberry.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package nl.altindag.log4j;
17+
18+
import nl.altindag.log.LogCaptor;
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
class FooServiceShould {
24+
25+
@Test
26+
void sayHello() {
27+
LogCaptor logCaptor = LogCaptor.forClass(FooService.class);
28+
29+
FooService fooService = new FooService();
30+
fooService.hello();
31+
32+
assertThat(logCaptor.getLogs())
33+
.hasSize(1)
34+
.contains("Hello there friend!");
35+
}
36+
37+
}

log-captor-examples/log-captor-with-log4j-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<dependency>
1919
<groupId>org.apache.logging.log4j</groupId>
2020
<artifactId>log4j-core</artifactId>
21-
<version>${version.log4j-core}</version>
21+
<version>${version.log4j}</version>
2222
</dependency>
2323
</dependencies>
2424

log-captor-examples/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<modules>
77
<module>log-captor-with-java-util-logging</module>
88
<module>log-captor-with-log4j-core</module>
9+
<module>log-captor-with-log4j-api</module>
910
<module>log-captor-with-slf4j-logback-classic</module>
1011
<module>log-captor-with-slf4j-log4j</module>
1112
<module>log-captor-with-spring-boot-starter-log4j2</module>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<version.aspectweaver>1.9.22.1</version.aspectweaver>
6969
<version.jackson>2.17.2</version.jackson>
7070
<version.slf4j>2.0.13</version.slf4j>
71-
<version.log4j-core>2.23.1</version.log4j-core>
71+
<version.log4j>2.23.1</version.log4j>
7272
<version.log4j-slf4j>2.19.0</version.log4j-slf4j>
7373
<version.log4j-log4j-slf4j18>2.18.0</version.log4j-log4j-slf4j18>
7474
<version.logback-classic>1.3.5</version.logback-classic>

0 commit comments

Comments
 (0)