BAEL-1960: Custom appender for log4j

This commit is contained in:
shreyashthakare 2018-07-17 00:32:13 +05:30
parent b44883b364
commit d0c619a0ad
4 changed files with 297 additions and 189 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>log4j2</artifactId>
@ -19,6 +20,14 @@
<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>
<groupId>com.fasterxml.jackson.core</groupId>

View File

@ -0,0 +1,58 @@
/**
*
*/
package com.baeldung.logging.log4j2.appender;
import java.io.Serializable;
import java.time.Instant;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
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.Layout;
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;
import org.apache.logging.log4j.core.layout.PatternLayout;
@Plugin(name = "MapAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE, printObject = true)
public class MapAppender extends AbstractAppender {
private ConcurrentMap<String, LogEvent> eventMap = new ConcurrentHashMap<>();
protected MapAppender(String name, Filter filter, Layout<? extends Serializable> layout) {
super(name, filter, layout);
}
@PluginFactory
public static MapAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") final Filter filter) {
if (name == null) {
LOGGER.error("No name provided for MapAppender");
return null;
}
if (layout == null) {
layout = PatternLayout.createDefaultLayout();
}
return new MapAppender(name, filter, layout);
}
@Override
public void append(LogEvent event) {
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">
packages="com.baeldung" status="WARN">
<Appenders>
<xi:include
href="log4j2-includes/console-appender_pattern-layout_colored.xml" />
@ -21,7 +21,8 @@
<KeyValuePair key="myCustomField" value="myCustomValue" />
</JsonLayout>
</Console>
<File name="JSONLogfileAppender" fileName="${sys:logging.folder.path}">
<File name="JSONLogfileAppender"
fileName="${sys:logging.folder.path}">
<JSONLayout compact="true" eventEol="true" />
<BurstFilter level="INFO" rate="2" maxBurst="10" />
</File>
@ -42,14 +43,18 @@
<Column name="message" pattern="%message" />
<Column name="throwable" pattern="%ex{full}" />
</JDBC>
<RollingFile name="XMLRollingfileAppender" fileName="target/logfile.xml"
<RollingFile name="XMLRollingfileAppender"
fileName="target/logfile.xml"
filePattern="target/logfile-%d{yyyy-MM-dd}-%i.log.gz">
<XMLLayout />
<Policies>
<SizeBasedTriggeringPolicy
size="17 kB" />
<SizeBasedTriggeringPolicy size="17 kB" />
</Policies>
</RollingFile>
<MapAppender name="MapAppender">
<PatternLayout
pattern=" Date: %d | level:%level | class name:%logger | method name:%M() | line number:%L | Location: %l | message:%m%n" />
</MapAppender>
</Appenders>
<Loggers>
<Logger name="CONSOLE_PATTERN_APPENDER_MARKER" level="TRACE"
@ -82,6 +87,7 @@
</Logger>
<Root level="DEBUG">
<AppenderRef ref="ConsoleAppender" />
<AppenderRef ref="MapAppender" />
</Root>
</Loggers>
</Configuration>