Hi,

I have a set up where I need to add and remove appenders programmatically.
However it doesn't work if I don't DOMConfigure the logger with an XML
config file initially - and that file must have at least one appender. I
used console appender because it is supposed to be throwaway - but when I
try to remove that appender it doesn't work - the log still logs to the
console.

Here is my XML:

*<log4j:configuration>

  <appender name="my-appnd" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>

<root>
<priority value="error"></priority>
  <appender-ref ref="my-appnd"/>
</root>
</log4j:configuration>*

Here is my Java code:

*public MyLogger(HttpSession session) throws IOException {

  DOMConfigurator.configure(session.getServletContext().getRealPath("[path
to conf xml]"));

  if(log.getAppender("my-appnd") != null) {
  log.removeAppender("my-appnd");
}

  String fileName = "[path to log file]";
PatternLayout layout = new PatternLayout("%d{MM/dd/yyyy HH:mm:ss} - %m%n");

  RollingFileAppender myNewAppender = new
RollingFileAppender(layout,fileName);
  **myNewAppender**.setName("my-appnd");
**myNewAppender**.setMaxFileSize("100KB");

log.addAppender(**myNewAppender**);
}*

Reply via email to