如果log4j属性有多个文件追加器,那么在Java中如何确保将日志写入特定文件中。
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
在Java类中,我可以做些什么来编写一些消息,比方说,SUCCESS_FILE和一些消息到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");
答案 0 :(得分:7)
这可能会有所帮助: -
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
访问它们: -
static final Logger successLog = Logger.getLogger("successLogger");
static final Logger tempLog = Logger.getLogger("tempLogger");
答案 1 :(得分:0)
使用@Nio回答的XML配置是:
<?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>
答案 2 :(得分:0)
这是我用来基于2个附加程序写入2个日志文件的方法。这也将写入控制台。
log4j.properties文件和Java代码可用How to group by and order by in cosmos db?
# Root logger option
log4j.rootLogger=ALL,STDOUT,debugLog
log4j.logger.reportsLogger=INFO,reportsLog
log4j.additivity.reportsLogger=false
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %C:%L - %m%n
# Direct log messages to a log file
log4j.appender.debugLog=org.apache.log4j.RollingFileAppender
log4j.appender.debugLog.File="/Users/test/debuglog.log"
log4j.appender.debugLog.MaxFileSize=10MB
log4j.appender.debugLog.MaxBackupIndex=10
log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout
log4j.appender.debugLog.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %C:%L - %m%n
# Direct log messages to a log file
log4j.appender.reportsLog=org.apache.log4j.RollingFileAppender
log4j.appender.reportsLog.File="/Users/test/reportslog.log"
log4j.appender.reportsLog.MaxFileSize=10MB
log4j.appender.reportsLog.MaxBackupIndex=10
log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout
log4j.appender.reportsLog.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %C:%L - %m%n