Changes as per suggestion to BAEL-1960
This commit is contained in:
parent
d0c619a0ad
commit
380dce8e6e
|
@ -3,46 +3,42 @@
|
||||||
*/
|
*/
|
||||||
package com.baeldung.logging.log4j2.appender;
|
package com.baeldung.logging.log4j2.appender;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.Level;
|
||||||
import org.apache.logging.log4j.core.Appender;
|
import org.apache.logging.log4j.core.Appender;
|
||||||
import org.apache.logging.log4j.core.Core;
|
import org.apache.logging.log4j.core.Core;
|
||||||
import org.apache.logging.log4j.core.Filter;
|
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.LogEvent;
|
||||||
import org.apache.logging.log4j.core.appender.AbstractAppender;
|
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.Plugin;
|
||||||
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
|
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.PluginElement;
|
||||||
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
|
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 {
|
public class MapAppender extends AbstractAppender {
|
||||||
|
|
||||||
private ConcurrentMap<String, LogEvent> eventMap = new ConcurrentHashMap<>();
|
private ConcurrentMap<String, LogEvent> eventMap = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
protected MapAppender(String name, Filter filter, Layout<? extends Serializable> layout) {
|
protected MapAppender(String name, Filter filter) {
|
||||||
super(name, filter, layout);
|
super(name, filter, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PluginFactory
|
@PluginFactory
|
||||||
public static MapAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") final Filter filter) {
|
public static MapAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Filter") final Filter filter) {
|
||||||
if (name == null) {
|
return new MapAppender(name, filter);
|
||||||
LOGGER.error("No name provided for MapAppender");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (layout == null) {
|
|
||||||
layout = PatternLayout.createDefaultLayout();
|
|
||||||
}
|
|
||||||
return new MapAppender(name, filter, layout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void append(LogEvent event) {
|
public void append(LogEvent event) {
|
||||||
|
if (event.getLevel()
|
||||||
|
.isLessSpecificThan(Level.WARN)) {
|
||||||
|
error("Unable to log less than WARN level.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
eventMap.put(Instant.now()
|
eventMap.put(Instant.now()
|
||||||
.toString(), event);
|
.toString(), event);
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,10 +51,7 @@
|
||||||
<SizeBasedTriggeringPolicy size="17 kB" />
|
<SizeBasedTriggeringPolicy size="17 kB" />
|
||||||
</Policies>
|
</Policies>
|
||||||
</RollingFile>
|
</RollingFile>
|
||||||
<MapAppender name="MapAppender">
|
<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>
|
</Appenders>
|
||||||
<Loggers>
|
<Loggers>
|
||||||
<Logger name="CONSOLE_PATTERN_APPENDER_MARKER" level="TRACE"
|
<Logger name="CONSOLE_PATTERN_APPENDER_MARKER" level="TRACE"
|
||||||
|
|
Loading…
Reference in New Issue