首页 > 代码库 > 记录下log4j的两种配置方式
记录下log4j的两种配置方式
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
记录下log4j的两种配置方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。