记录下log4j的两种配置方式

时间:2022-10-12 04:33:39

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="CONSOLE_APPENDER" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" />
</layout>

<!--过滤器设置输出的级别 -->
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="debug" />
<param name="levelMax" value="warn" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>

<!-- 系统运行日志 -->
<appender name="SYSTEM_APPENDER" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${user.home}/logs/bingu/sys_run.log" /><!--
设置日志输出文件名 -->
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<!-- 设置是否在重新启动服务时,在原有日志的基础添加新日志 -->
<param name="Append" value="true" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p (%c:%L)- %m%n" />
</layout>
</appender>
<logger name="SYSTEM" additivity="false">
<priority value="debug" />
<appender-ref ref="SYSTEM_APPENDER" />
</logger>

<!-- error -->
<appender name="ERROR_APPENDER" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${user.home}/logs/bingu/error.log" />
<param name="Append" value="true" />
<param name="Threshold" value="ERROR" />
<param name="MaxFileSize" value="1024KB" />
<param name="MaxBackupIndex" value="1" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %l - %m%n" />
</layout>
</appender>
<logger name="ERROR" additivity="false">
<priority value="info" />
<appender-ref ref="ERROR_APPENDER" />
</logger>

<!-- 方法运行时间统计 -->
<appender name="ALL_METHODS_RUNTIME_APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${user.home}/logs/bingu/method_runtime.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{MMdd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" />
</layout>
</appender>
<logger name="ALL_METHODS_RUNTIME" additivity="false">
<priority value="info" />
<appender-ref ref="ALL_METHODS_RUNTIME_APPENDER" />
</logger>

<!-- web -->
<appender name="WEB_APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="encoding" value="UTF-8" />
<param name="File" value="${user.home}/logs/bingu/web.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %t %5p [%c:%L] - %m%n" />
</layout>
</appender>
<category name="com.bingu.web" additivity="false">
<level value="info" />
<appender-ref ref="WEB_APPENDER" />
</category>

<!-- service -->
<appender name="SERVICE_APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="encoding" value="UTF-8" />
<param name="File" value="${user.home}/logs/bingu/service.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %t %5p [%c:%L] - %m%n" />
</layout>
</appender>
<category name="com.bingu.bll" additivity="false">
<level value="info" />
<appender-ref ref="SERVICE_APPENDER" />
</category>

<!-- dao -->
<appender name="DAO_APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="encoding" value="UTF-8" />
<param name="File" value="${user.home}/logs/bingu/dao.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %t %5p [%c:%L] - %m%n" />
</layout>
</appender>
<category name="com.bingu.dal" additivity="false">
<level value="info" />
<appender-ref ref="DAO_APPENDER" />
</category>

<!-- util -->
<appender name="UTIL_APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="encoding" value="UTF-8" />
<param name="File" value="${user.home}/logs/bingu/util.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %t %5p [%c:%L] - %m%n" />
</layout>
</appender>
<category name="com.bingu.util" additivity="false">
<level value="info" />
<appender-ref ref="UTIL_APPENDER" />
</category>

<root>
<priority value="debug" />
<appender-ref ref="CONSOLE_APPENDER" />
<appender-ref ref="SYSTEM" />
<appender-ref ref="ERROR" />
</root>

</log4j:configuration>

.properties文件配置

log4j.rootLogger=info,file

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern='-'yyyy-MM-dd
log4j.appender.file.File=logs/log.txt
log4j.appender.file.Append=true
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%-5p] %d %37c %3x - %m%n

log4j.logger.org.springframework.web.servlet.handler.SimpleMappingExceptionResolver=WARN