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;
|
||||
|
||||
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<String, LogEvent> eventMap = new ConcurrentHashMap<>();
|
||||
|
||||
protected MapAppender(String name, Filter filter, Layout<? extends Serializable> 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<? 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);
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -51,10 +51,7 @@
|
|||
<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>
|
||||
<MapAppender name="MapAppender"/>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Logger name="CONSOLE_PATTERN_APPENDER_MARKER" level="TRACE"
|
||||
|
|
Loading…
Reference in New Issue