Skip to content

Commit 7070f25

Browse files
shreyashtpivovarit
authored andcommitted
BAEL-1960: Custom appender for log4j (eugenp#4731)
* BAEL-1960: Custom appender for log4j * Changes as per suggestion to BAEL-1960 * Changes as [er review for BAEL-1960 * Changes for formatting as per suggestion. * BAEL-1960. Copied pom.xml from master and pasted my changes against it. * Chnages for spaces instead of tabs. * Changes for spaces instead of tabs.
1 parent a1ba371 commit 7070f25

4 files changed

Lines changed: 101 additions & 3 deletions

File tree

‎logging-modules/log4j2/pom.xml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
<artifactId>log4j-core</artifactId>
1919
<version>${log4j-core.version}</version>
2020
</dependency>
21+
22+
<!-- This is the needed API component. -->
23+
<dependency>
24+
<groupId>org.apache.logging.log4j</groupId>
25+
<artifactId>log4j-api</artifactId>
26+
<version>${log4j-core.version}</version>
27+
</dependency>
2128

2229
<!-- This is used by JSONLayout. -->
2330
<dependency>
@@ -114,4 +121,4 @@
114121
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
115122
</properties>
116123

117-
</project>
124+
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.logging.log4j2.appender;
5+
6+
import java.time.Instant;
7+
import java.util.concurrent.ConcurrentHashMap;
8+
import java.util.concurrent.ConcurrentMap;
9+
10+
import org.apache.logging.log4j.Level;
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.LogEvent;
15+
import org.apache.logging.log4j.core.appender.AbstractAppender;
16+
import org.apache.logging.log4j.core.config.plugins.Plugin;
17+
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
18+
import org.apache.logging.log4j.core.config.plugins.PluginElement;
19+
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
20+
21+
@Plugin(name = "MapAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE)
22+
public class MapAppender extends AbstractAppender {
23+
24+
private ConcurrentMap<String, LogEvent> eventMap = new ConcurrentHashMap<>();
25+
26+
protected MapAppender(String name, Filter filter) {
27+
super(name, filter, null);
28+
}
29+
30+
@PluginFactory
31+
public static MapAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Filter") final Filter filter) {
32+
return new MapAppender(name, filter);
33+
}
34+
35+
@Override
36+
public void append(LogEvent event) {
37+
if (event.getLevel()
38+
.isLessSpecificThan(Level.WARN)) {
39+
error("Unable to log less than WARN level.");
40+
return;
41+
}
42+
eventMap.put(Instant.now()
43+
.toString(), event);
44+
}
45+
46+
public ConcurrentMap<String, LogEvent> getEventMap() {
47+
return eventMap;
48+
}
49+
50+
public void setEventMap(ConcurrentMap<String, LogEvent> eventMap) {
51+
this.eventMap = eventMap;
52+
}
53+
54+
}
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+
}

‎logging-modules/log4j2/src/test/resources/log4j2.xml‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude"
3-
status="WARN">
2+
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude"
3+
packages="com.baeldung" status="WARN">
44
<Appenders>
55
<xi:include
66
href="log4j2-includes/console-appender_pattern-layout_colored.xml" />
@@ -50,6 +50,7 @@
5050
size="17 kB" />
5151
</Policies>
5252
</RollingFile>
53+
<MapAppender name="MapAppender"/>
5354
</Appenders>
5455
<Loggers>
5556
<Logger name="CONSOLE_PATTERN_APPENDER_MARKER" level="TRACE"
@@ -82,6 +83,7 @@
8283
</Logger>
8384
<Root level="DEBUG">
8485
<AppenderRef ref="ConsoleAppender" />
86+
<AppenderRef ref="MapAppender" />
8587
</Root>
8688
</Loggers>
8789
</Configuration>

0 commit comments

Comments
 (0)