import org.apache.logging.log4j.core.Filter; impor...
# codereview
d
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.appender.AppenderLoggingException; 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; import java.io.Serializable; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; /** * <h2>AnsiConsoleAppender</h2> * * <h3>notes</h3> * <li>class name need not match the @Plugin name</li> * <h3>more<h3/> * <li><a href="https://stackoverflow.com/a/24220688/270143">How to Create a Custom Appender in log4j2?</a></li> */ @Plugin(name="AnsiConsoleAppender", category="Core", elementType="appender", printObject=true) public final class AnsiConsoleAppender extends AbstractAppender { private final ReadWriteLock rwLock = new ReentrantReadWriteLock(); private final Lock readLock = rwLock.readLock(); protected AnsiConsoleAppender(String name, Filter filter, Layout<? extends Serializable> layout, final boolean ignoreExceptions) { super(name, filter, layout, ignoreExceptions); } // The append method is where the appender does the work. // Given a log event, you are free to do with it what you want. // This example demonstrates: // 1. Concurrency: this method may be called by multiple threads concurrently // 2. How to use layouts // 3. Error handling //@Override public void append(LogEvent event) { try { final byte[] bytes = getLayout().toByteArray(event); System.out.write("YO: ".getBytes()); System.out.write(bytes); } catch (Exception ex) { if (!ignoreExceptions()) throw new AppenderLoggingException(ex); } } // Your custom appender needs to declare a factory method // annotated with
@PluginFactory
. Log4j will parse the configuration // and call this factory method to construct an appender instance with // the configured attributes. @PluginFactory public static AnsiConsoleAppender createAppender( @PluginAttribute("name") String name, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") final Filter filter, @PluginAttribute("otherAttribute") String otherAttribute) { if (name == null) { LOGGER.error("No name provided for AnsiConsoleAppenderImpl"); return null; } if (layout == null) { layout = PatternLayout.createDefaultLayout(); } return new AnsiConsoleAppender(name, filter, layout, true); } }
r
Hey man, just as an aside, I wanted to draw your attention to the + to the left of the text entry in Slack. If you click that and click
Code or text snippet
, you can paste your code there and it will receive syntax highlighting and be collapsible. It helps a lot in workspaces with a large number of people to be able to collapse large code posts to improve readability.