Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should extend AppenderSkeleton class, that (quoting javadoc) "provides the code for common functionality, such as support for threshold filtering and support for general filters."</p> <p>If you read the code of AppenderSkeleton, you'll see that it handles almost all, leaving to you just:</p> <ol> <li>protected void append(LoggingEvent event) </li> <li>public void close() </li> <li>public boolean requiresLayout()</li> </ol> <p>The core method is append. Remember that you don't need to implement the filtering logic in it because it is already implemented in doAppend that in turn calls append. Here I made a (quite useless) class that stores the log entries in an ArrayList, just as a demo. </p> <pre><code>public /*static*/ class MyAppender extends AppenderSkeleton { ArrayList&lt;LoggingEvent&gt; eventsList = new ArrayList(); @Override protected void append(LoggingEvent event) { eventsList.add(event); } public void close() { } public boolean requiresLayout() { return false; } } </code></pre> <p>Ok, let's test it:</p> <pre><code>public static void main (String [] args) { Logger l = Logger.getLogger("test"); MyAppender app = new MyAppender(); l.addAppender(app); l.warn("first"); l.warn("second"); l.warn("third"); l.trace("fourth shouldn't be printed"); for (LoggingEvent le: app.eventsList) { System.out.println("***" + le.getMessage()); } } </code></pre> <p>You should have "first", "second", "third" printed; the fourth message shouldn't be printed since the log level of root logger is debug while the event level is trace. This proves that AbstractSkeleton implements "level management" correctly for us. So that's definitely seems the way to go... now the question: why do you need a custom appender while there are many built in that log to almost any destination? (btw a good place to start with log4j: <a href="http://logging.apache.org/log4j/1.2/manual.html" rel="noreferrer">http://logging.apache.org/log4j/1.2/manual.html</a>)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload