Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppenderSkeleton (Log4j2)

Tags:

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?

like image 975
hell_storm2004 Avatar asked Jan 13 '20 09:01

hell_storm2004


2 Answers

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.

like image 152
rgoers Avatar answered Sep 24 '22 15:09

rgoers


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).

like image 45
oskarryn Avatar answered Sep 26 '22 15:09

oskarryn