If a log4j properties have multiple file appenders, then in Java how can I make sure that logs are written in a specific file.
log4j.rootLogger=INFO,out
log4j.appender.SUCCESS_FILE=org.apache.log4j.FileAppender
log4j.appender.SUCCESS_FILE.File=${dd.log.dir}/success.log
log4j.appender.VALID_FILE=org.apache.log4j.FileAppender
log4j.appender.VALID_FILE.File=${dd.log.dir}/valid_error.log
log4j.appender.TEMP_FILE=org.apache.log4j.FileAppender
log4j.appender.TEMP_FILE.File=${dd.log.dir}/Temp_error.tmp_log
In Java class, what can I do to write some messages to, lets say, SUCCESS_FILE and some messages to TEMP_FILE
Logger log = Logger.getLogger(Test.class);
log.debug("This message should go to SUCCESS_FILE");
log.debug("This message should go to TEMP_FILE");
FileAppender appends log events to a file. Support for java. io. Writer and console appending has been deprecated and then removed. See the replacement solutions: WriterAppender and ConsoleAppender .
FileAppender. Override the activateOptions() function for setting the new log file name every time the logger is instantiated. For simplicity, we will append the current timestamp to each log file name. So, each time when you run the application, you will get a fresh log file.
To write your logging information into multiple files, you would have to use org. apache. log4j. RollingFileAppender class which extends the FileAppender class and inherits all its properties.
This might help:-
log4j.appender.successLog=org.apache.log4j.FileAppender
log4j.appender.successLog.File=${dd.log.dir}/success.log
log4j.appender.tempLog=org.apache.log4j.FileAppender
log4j.appender.tempLog.File=${dd.log.dir}/Temp_error.tmp_log
log4j.category.successLogger=INFO, successLog
log4j.additivity.successLogger=false
log4j.category.tempLogger=INFO, tempLog
log4j.additivity.tempLogger=false
Access them like:-
static final Logger successLog = Logger.getLogger("successLogger");
static final Logger tempLog = Logger.getLogger("tempLogger");
Using XML configuration like @Nio answered is:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="successLog" class="org.apache.log4j.FileAppender">
<param name="file" value="${dd.log.dir}/success.log" />
<param name="append" value="false" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d %c %M - %m%n" />
</layout>
</appender>
<appender name="tempLog" class="org.apache.log4j.FileAppender">
<param name="file" value="${dd.log.dir}/Temp_error.tmp_log" />
<param name="append" value="false" />
<param name="append" value="false" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%p] %d %c %M - %m%n" />
</layout>
</appender>
<logger name="successLogger" additivity="false">
<level value="INFO"/>
<appender-ref ref="successLog"/>
</logger>
<logger name="tempLogger" additivity="false">
<level value="INFO"/>
<appender-ref ref="tempLog"/>
</logger>
</log4j:configuration>
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