Skip to content

Commit d0c619a

Browse files
author
shreyashthakare
committed
BAEL-1960: Custom appender for log4j
1 parent b44883b commit d0c619a

4 files changed

Lines changed: 297 additions & 189 deletions

File tree

‎logging-modules/log4j2/pom.xml‎

Lines changed: 114 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,126 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
<artifactId>log4j2</artifactId>
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+
<artifactId>log4j2</artifactId>
67

7-
<parent>
8-
<groupId>com.baeldung</groupId>
9-
<artifactId>parent-modules</artifactId>
10-
<version>1.0.0-SNAPSHOT</version>
11-
<relativePath>../../</relativePath>
12-
</parent>
8+
<parent>
9+
<groupId>com.baeldung</groupId>
10+
<artifactId>parent-modules</artifactId>
11+
<version>1.0.0-SNAPSHOT</version>
12+
<relativePath>../../</relativePath>
13+
</parent>
1314

14-
<dependencies>
15-
<!-- This is the needed core component. -->
16-
<dependency>
17-
<groupId>org.apache.logging.log4j</groupId>
18-
<artifactId>log4j-core</artifactId>
19-
<version>${log4j-core.version}</version>
20-
</dependency>
15+
<dependencies>
16+
<!-- This is the needed core component. -->
17+
<dependency>
18+
<groupId>org.apache.logging.log4j</groupId>
19+
<artifactId>log4j-core</artifactId>
20+
<version>${log4j-core.version}</version>
21+
</dependency>
2122

22-
<!-- This is used by JSONLayout. -->
23-
<dependency>
24-
<groupId>com.fasterxml.jackson.core</groupId>
25-
<artifactId>jackson-databind</artifactId>
26-
<version>${jackson.version}</version>
27-
</dependency>
23+
<!-- This is the needed API component. -->
24+
<dependency>
25+
<groupId>org.apache.logging.log4j</groupId>
26+
<artifactId>log4j-api</artifactId>
27+
<version>${log4j-core.version}</version>
28+
</dependency>
2829

29-
<!-- This is used by XMLLayout. -->
30-
<dependency>
31-
<groupId>com.fasterxml.jackson.dataformat</groupId>
32-
<artifactId>jackson-dataformat-xml</artifactId>
33-
<version>${jackson.version}</version>
34-
</dependency>
3530

36-
<!-- This is used by JDBC Appender. -->
37-
<dependency>
38-
<groupId>com.h2database</groupId>
39-
<artifactId>h2</artifactId>
40-
<version>${h2.version}</version>
41-
</dependency>
42-
<dependency>
43-
<groupId>org.apache.commons</groupId>
44-
<artifactId>commons-dbcp2</artifactId>
45-
<version>${commons-dbcp2.version}</version>
46-
</dependency>
31+
<!-- This is used by JSONLayout. -->
32+
<dependency>
33+
<groupId>com.fasterxml.jackson.core</groupId>
34+
<artifactId>jackson-databind</artifactId>
35+
<version>${jackson.version}</version>
36+
</dependency>
4737

48-
<!-- This is used for testing only. -->
49-
<dependency>
50-
<groupId>org.apache.logging.log4j</groupId>
51-
<artifactId>log4j-core</artifactId>
52-
<version>${log4j-core.version}</version>
53-
<type>test-jar</type>
54-
<scope>test</scope>
55-
</dependency>
56-
</dependencies>
38+
<!-- This is used by XMLLayout. -->
39+
<dependency>
40+
<groupId>com.fasterxml.jackson.dataformat</groupId>
41+
<artifactId>jackson-dataformat-xml</artifactId>
42+
<version>${jackson.version}</version>
43+
</dependency>
5744

58-
<build>
59-
<plugins>
60-
<plugin>
61-
<groupId>org.apache.maven.plugins</groupId>
62-
<artifactId>maven-compiler-plugin</artifactId>
63-
<version>${maven-compiler-plugin.version}</version>
64-
<configuration>
65-
<proc>none</proc>
66-
</configuration>
67-
</plugin>
68-
</plugins>
69-
</build>
45+
<!-- This is used by JDBC Appender. -->
46+
<dependency>
47+
<groupId>com.h2database</groupId>
48+
<artifactId>h2</artifactId>
49+
<version>${h2.version}</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.apache.commons</groupId>
53+
<artifactId>commons-dbcp2</artifactId>
54+
<version>${commons-dbcp2.version}</version>
55+
</dependency>
7056

71-
<profiles>
72-
<profile>
73-
<id>integration</id>
74-
<build>
75-
<plugins>
76-
<plugin>
77-
<groupId>org.apache.maven.plugins</groupId>
78-
<artifactId>maven-surefire-plugin</artifactId>
79-
<executions>
80-
<execution>
81-
<phase>integration-test</phase>
82-
<goals>
83-
<goal>test</goal>
84-
</goals>
85-
<configuration>
86-
<excludes>
87-
<exclude>**/*ManualTest.java</exclude>
88-
<exclude>**/*LiveTest.java</exclude>
89-
</excludes>
90-
<includes>
91-
<include>**/*IntegrationTest.java</include>
92-
<include>**/*IntTest.java</include>
93-
</includes>
94-
</configuration>
95-
</execution>
96-
</executions>
97-
<configuration>
98-
<systemPropertyVariables>
99-
<test.mime>json</test.mime>
100-
<logging.folder.path>${java.io.tmpdir}/${maven.build.timestamp}/logfile.json</logging.folder.path>
101-
</systemPropertyVariables>
102-
</configuration>
103-
</plugin>
104-
</plugins>
105-
</build>
106-
</profile>
107-
</profiles>
57+
<!-- This is used for testing only. -->
58+
<dependency>
59+
<groupId>org.apache.logging.log4j</groupId>
60+
<artifactId>log4j-core</artifactId>
61+
<version>${log4j-core.version}</version>
62+
<type>test-jar</type>
63+
<scope>test</scope>
64+
</dependency>
65+
</dependencies>
10866

109-
<properties>
110-
<jackson.version>2.9.5</jackson.version>
111-
<h2.version>1.4.193</h2.version>
112-
<commons-dbcp2.version>2.1.1</commons-dbcp2.version>
113-
<log4j-core.version>2.11.0</log4j-core.version>
114-
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
115-
</properties>
67+
<build>
68+
<plugins>
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-compiler-plugin</artifactId>
72+
<version>${maven-compiler-plugin.version}</version>
73+
<configuration>
74+
<proc>none</proc>
75+
</configuration>
76+
</plugin>
77+
</plugins>
78+
</build>
79+
80+
<profiles>
81+
<profile>
82+
<id>integration</id>
83+
<build>
84+
<plugins>
85+
<plugin>
86+
<groupId>org.apache.maven.plugins</groupId>
87+
<artifactId>maven-surefire-plugin</artifactId>
88+
<executions>
89+
<execution>
90+
<phase>integration-test</phase>
91+
<goals>
92+
<goal>test</goal>
93+
</goals>
94+
<configuration>
95+
<excludes>
96+
<exclude>**/*ManualTest.java</exclude>
97+
<exclude>**/*LiveTest.java</exclude>
98+
</excludes>
99+
<includes>
100+
<include>**/*IntegrationTest.java</include>
101+
<include>**/*IntTest.java</include>
102+
</includes>
103+
</configuration>
104+
</execution>
105+
</executions>
106+
<configuration>
107+
<systemPropertyVariables>
108+
<test.mime>json</test.mime>
109+
<logging.folder.path>${java.io.tmpdir}/${maven.build.timestamp}/logfile.json</logging.folder.path>
110+
</systemPropertyVariables>
111+
</configuration>
112+
</plugin>
113+
</plugins>
114+
</build>
115+
</profile>
116+
</profiles>
117+
118+
<properties>
119+
<jackson.version>2.9.5</jackson.version>
120+
<h2.version>1.4.193</h2.version>
121+
<commons-dbcp2.version>2.1.1</commons-dbcp2.version>
122+
<log4j-core.version>2.11.0</log4j-core.version>
123+
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
124+
</properties>
116125

117126
</project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.logging.log4j2.appender;
5+
6+
import java.io.Serializable;
7+
import java.time.Instant;
8+
import java.util.concurrent.ConcurrentHashMap;
9+
import java.util.concurrent.ConcurrentMap;
10+
11+
import org.apache.logging.log4j.core.Appender;
12+
import org.apache.logging.log4j.core.Core;
13+
import org.apache.logging.log4j.core.Filter;
14+
import org.apache.logging.log4j.core.Layout;
15+
import org.apache.logging.log4j.core.LogEvent;
16+
import org.apache.logging.log4j.core.appender.AbstractAppender;
17+
import org.apache.logging.log4j.core.config.plugins.Plugin;
18+
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
19+
import org.apache.logging.log4j.core.config.plugins.PluginElement;
20+
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
21+
import org.apache.logging.log4j.core.layout.PatternLayout;
22+
23+
@Plugin(name = "MapAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE, printObject = true)
24+
public class MapAppender extends AbstractAppender {
25+
26+
private ConcurrentMap<String, LogEvent> eventMap = new ConcurrentHashMap<>();
27+
28+
protected MapAppender(String name, Filter filter, Layout<? extends Serializable> layout) {
29+
super(name, filter, layout);
30+
}
31+
32+
@PluginFactory
33+
public static MapAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") final Filter filter) {
34+
if (name == null) {
35+
LOGGER.error("No name provided for MapAppender");
36+
return null;
37+
}
38+
if (layout == null) {
39+
layout = PatternLayout.createDefaultLayout();
40+
}
41+
return new MapAppender(name, filter, layout);
42+
}
43+
44+
@Override
45+
public void append(LogEvent event) {
46+
eventMap.put(Instant.now()
47+
.toString(), event);
48+
}
49+
50+
public ConcurrentMap<String, LogEvent> getEventMap() {
51+
return eventMap;
52+
}
53+
54+
public void setEventMap(ConcurrentMap<String, LogEvent> eventMap) {
55+
this.eventMap = eventMap;
56+
}
57+
58+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.logging.log4j2.appender;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.apache.logging.log4j.LogManager;
6+
import org.apache.logging.log4j.Logger;
7+
import org.apache.logging.log4j.core.LoggerContext;
8+
import org.apache.logging.log4j.core.config.Configuration;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.junit.runner.RunWith;
12+
import org.junit.runners.JUnit4;
13+
14+
@RunWith(JUnit4.class)
15+
public class MapAppenderIntegrationTest {
16+
17+
private Logger logger;
18+
19+
@Before
20+
public void setup() {
21+
logger = LogManager.getLogger(MapAppenderIntegrationTest.class);
22+
}
23+
24+
@Test
25+
public void whenLoggerEmitsLoggingEvent_thenAppenderReceivesEvent() throws Exception {
26+
logger.info("Test from {}", this.getClass()
27+
.getSimpleName());
28+
LoggerContext context = LoggerContext.getContext(false);
29+
Configuration config = context.getConfiguration();
30+
MapAppender appender = config.getAppender("MapAppender");
31+
assertEquals(appender.getEventMap()
32+
.size(), 1);
33+
}
34+
35+
}

0 commit comments

Comments
 (0)