I am trying to migrate a class that extends org.apache.log4j.AppenderSkeleton
from an old version of log4j to log4j 2. I was reading the JavaDoc for the class, and I read that,
Appenders constructed using this are ignored in Log4j 2.
on the website for the class.
Does this mean, if I am using Log4j 2, I should extend this class? If yes, what should I use as an alternative? Would ConsoleAppender do the trick?
Appenders in Log4j 2 implement the Appender interface. Most Appenders will extend either AbstractAppender, AbstractOutputStreamAppender, or AbstractWriterAppender. Log4j 2 uses Plugins, which means your appender will have to be annotated with @Plugin and defined as an Appender. Appenders also require a Builder annotated with @PluginBuilderFactory to create the Appender instance from its configuration. You can look at any of Log4j's Appenders, such as FileAppender, for an example.
You will also notice that most of the Appenders use a Manager to perform most of the work. This is because Appenders are always recreated during a reconfiguration, which could lead to problems. The Managers are only recreated if attributes specific to that Manager are changed, otherwise the new Appender instance will reuse the previous Manager.
The code supporting the accepted answer here is available in the answer by David Lopez Carrasco to another question. This code should be supplemented with clearing the logs List in-between tests and test suites (otherwise the appender would hold messages from previous loggers).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With