BAEL-1960: Custom appender for log4j (#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.
This commit is contained in:
Shreyash 2018-07-27 02:27:19 +05:30 committed by Grzegorz Piwowarek
parent a1ba371c73
commit 7070f25400
4 changed files with 101 additions and 3 deletions

View File

@ -18,6 +18,13 @@
<artifactId>log4j-core</artifactId>
<version>${log4j-core.version}</version>
</dependency>
<!-- This is the needed API component. -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j-core.version}</version>
</dependency>
<!-- This is used by JSONLayout. -->
<dependency>
@ -114,4 +121,4 @@
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
</properties>
</project>
</project>

View File

@ -0,0 +1,54 @@
/**
*
*/
package com.baeldung.logging.log4j2.appender;
import java.time.Instant;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
@Plugin(name = "MapAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE)
public class MapAppender extends AbstractAppender {
private ConcurrentMap<String, LogEvent> eventMap = new ConcurrentHashMap<>();
protected MapAppender(String name, Filter filter) {
super(name, filter, null);
}
@PluginFactory
public static MapAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Filter") final Filter filter) {
return new MapAppender(name, filter);
}
@Override
public void append(LogEvent event) {
if (event.getLevel()
.isLessSpecificThan(Level.WARN)) {
error("Unable to log less than WARN level.");
return;
}
eventMap.put(Instant.now()
.toString(), event);
}
public ConcurrentMap<String, LogEvent> getEventMap() {
return eventMap;
}
public void setEventMap(ConcurrentMap<String, LogEvent> eventMap) {
this.eventMap = eventMap;
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.logging.log4j2.appender;
import static org.junit.Assert.assertEquals;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class MapAppenderIntegrationTest {
private Logger logger;
@Before
public void setup() {
logger = LogManager.getLogger(MapAppenderIntegrationTest.class);
}
@Test
public void whenLoggerEmitsLoggingEvent_thenAppenderReceivesEvent() throws Exception {
logger.info("Test from {}", this.getClass()
.getSimpleName());
LoggerContext context = LoggerContext.getContext(false);
Configuration config = context.getConfiguration();
MapAppender appender = config.getAppender("MapAppender");
assertEquals(appender.getEventMap()
.size(), 1);
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude"
status="WARN">
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude"
packages="com.baeldung" status="WARN">
<Appenders>
<xi:include
href="log4j2-includes/console-appender_pattern-layout_colored.xml" />
@ -50,6 +50,7 @@
size="17 kB" />
</Policies>
</RollingFile>
<MapAppender name="MapAppender"/>
</Appenders>
<Loggers>
<Logger name="CONSOLE_PATTERN_APPENDER_MARKER" level="TRACE"
@ -82,6 +83,7 @@
</Logger>
<Root level="DEBUG">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="MapAppender" />
</Root>
</Loggers>
</Configuration>