From 380dce8e6e629c7546222cd07ede44099c58608e Mon Sep 17 00:00:00 2001 From: root Date: Wed, 25 Jul 2018 00:21:47 +0530 Subject: [PATCH] Changes as per suggestion to BAEL-1960 --- .../logging/log4j2/appender/MapAppender.java | 26 ++++++++----------- .../log4j2/src/test/resources/log4j2.xml | 5 +--- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/appender/MapAppender.java b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/appender/MapAppender.java index 160ba58395..e9c025f480 100644 --- a/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/appender/MapAppender.java +++ b/logging-modules/log4j2/src/main/java/com/baeldung/logging/log4j2/appender/MapAppender.java @@ -3,46 +3,42 @@ */ 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.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.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) +@Plugin(name = "MapAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE, printObject = false) public class MapAppender extends AbstractAppender { private ConcurrentMap eventMap = new ConcurrentHashMap<>(); - protected MapAppender(String name, Filter filter, Layout layout) { - super(name, filter, layout); + protected MapAppender(String name, Filter filter) { + super(name, filter, null); } @PluginFactory - public static MapAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Layout") Layout 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); + 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); } diff --git a/logging-modules/log4j2/src/test/resources/log4j2.xml b/logging-modules/log4j2/src/test/resources/log4j2.xml index d0fd0d088f..eefa00e1ba 100644 --- a/logging-modules/log4j2/src/test/resources/log4j2.xml +++ b/logging-modules/log4j2/src/test/resources/log4j2.xml @@ -51,10 +51,7 @@ - - - +