Search code examples
javalog4jappender

Can I instansiate log4j appender independently


I'd like to implement architecture that the user defines log4j appender through log4j properties as usual but this appender is just a buffer and it delegate the log messages to one of several appenders that it holds.
I don't want to define those appenders in the log4j peroprties file, I want them to be hidden so that no user could modify there configuration. Is there mechanism were I can construct new log4j appenders, save them as a data members of the another appender that extends org.apache.log4j.AppenderSkeleton and is published and bound to my logger and then call those appenders from the skeleton?
something like:

public class MemAppender extends AppenderSkeleton

private Appender delegateAppender = new RollingFileAppender();
...//set all the proprerties of the rolling file appender ...

public void append(LoggingEvent event){
  //do my business logic and then:
  delegateAppender.append(event); 
}

What is the way to implement such mechanism?


Solution

  • Apparently, there is no special problem in constructing log4j appender, such as 'RollingFileAppender' and call it from another appender. I just had to set all the required attributes such as MaxFileSize, File, maxBackupIndex, etc.