0
votes

I am using log4j (1.2.15) for logging in my application, where I have two file appenders for two different packages (darius.log4j.sql and darius.log4j.net) and a console appender. Its configuration is this:

# CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# FILE A
log4j.appender.A=org.apache.log4j.FileAppender
log4j.appender.A.File=A.log
log4j.appender.A.layout=org.apache.log4j.PatternLayout
log4j.appender.A.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
# FILE B
log4j.appender.B=org.apache.log4j.FileAppender
log4j.appender.B.File=B.log
log4j.appender.B.layout=org.apache.log4j.PatternLayout
log4j.appender.B.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
#
log4j.rootLogger=ERROR, CONSOLE
#
log4j.logger.darius.sql=DEBUG, A
log4j.logger.darius.net=INFO, B

What I need now is to add a new file appender programmatically if a specific data combination appears. I konw how to define this new file appender (ex C) but I don't know how to set the level and the new appender to a third package:

log4j.logger.darius.ws=INFO, C

where the C is the third file appender.

Any hit?

1

1 Answers

1
votes

Simple! After you create your third appender (C) get a logger for your namespace (darius.ws), set the INFO level for this logger. At the end just append this C appender to the logger.

FileAppender fileAppender = new FileAppender();
fileAppender.setName("C");
fileAppender.setFile("C.log");
fileAppender.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
fileAppender.setThreshold(Level.INFO);
fileAppender.setAppend(true);
...
Logger someLogger = Logger.getLogger("darius.ws");
someLogger.setLevel(Level.INFO);
someLogger.addAppender(fileAppender);

Best regards