我想根据我指定的仅记录器名称将消息记录到文件中,因为记录器名称作为日志消息中的参数发送。默认情况下,无论记录器名称如何,我的所有日志文件都会被附加,因为我在root下添加了所有appender的ref。
我的log4j.xml如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true">
<!-- ========== appenders start here ========= -->
<appender name="operator_log" class="org.apache.log4j.DailyRollingFileAppender">
<param name="ImmediateFlush" value="true" />
<param name="Append" value="true" />
<param name="File" value="C:/SSG_Admin.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE}|%5p|%m%n" />
</layout>
</appender>
<appender name="security_log" class="org.apache.log4j.DailyRollingFileAppender">
<param name="ImmediateFlush" value="true" />
<param name="Append" value="true" />
<param name="File" value="C:/SSG_Security.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE}|%5p|%m%n" />
</layout>
</appender>
<appender name="application_log" class="org.apache.log4j.DailyRollingFileAppender">
<param name="ImmediateFlush" value="true" />
<param name="Append" value="true" />
<param name="File" value="C:/SSG_Core.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE}|%5p|%m%n" />
</layout>
</appender>
<!-- ========= loggers start here ========= -->
<logger name="DB_CONNECTION_ISSUE" additivity="false">
<level value="debug" />
<appender-ref ref="operator_log" />
<appender-ref ref="application_log" />
</logger>
<logger name="NETWORK_ISSUE" additivity="false">
<level value="debug" />
<appender-ref ref="operator_log" />
<appender-ref ref="application_log" />
</logger>
<logger name="COMMUNICATION_ISSUE" additivity="false">
<level value="debug" />
<appender-ref ref="operator_log" />
<appender-ref ref="application_log" />
</logger>
<logger name="ACCESS_ISSUES" additivity="false">
<level value="debug" />
<appender-ref ref="security_log" />
<appender-ref ref="application_log" />
</logger>
<logger name="CERTIFICATE_ISSUE" additivity="false">
<level value="debug" />
<appender-ref ref="security_log" />
<appender-ref ref="application_log" />
</logger>
<logger name="VALIDATION_OR_DATA_ISSUE" additivity="false">
<level value="debug" />
<appender-ref ref="application_log" />
</logger>
<logger name="OTHER_ISSUE" additivity="false">
<level value="debug" />
<appender-ref ref="application_log" />
</logger>
<root>
<priority value="debug" />
<appender-ref ref="operator_log" />
<appender-ref ref="security_log" />
<appender-ref ref="application_log" />
</root>
记录器下的指定appender只能为我附加相应的文件。但是,无论记录器名称如何,所有文件都会被追加。 我对记录器很新。有没有解决方案......
答案 0 :(得分:0)
请您查看&#34;类别&#34;?以下是一个例子:
<appender name="myFile" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="INFO" />
<param name="File" value="/abc/logs/myLog.log"/>
<param name="Append" value="true" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="100" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%r %d %-5p [%c{1}] %m %n" />
</layout>
</appender>
<category name="com.myFile.crs" additivity="false">
<priority value="info" />
<appender-ref ref="myFile" />
</category>