Log4j 日志的使用

时间:2022-10-24 23:18:48


首先导入相关的包文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
<log4j:configuration>
<!--输出到控制台-->
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</layout>
</appender>

<!--输出到文件(info)-->
<!--将生成“admin.log.2014-06-11”这样的日志文件-->
<appender name="fileAppenderInfo" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/logs/admin.log" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>


<!--输出到文件(warn)-->
<appender name="fileAppenderWarn" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/logs/warn.log" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</layout>

<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>

<!--输出到文件(error)-->
<appender name="fileAppenderError" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/logs/error.log" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>

<!--屏蔽所有org.springframework.*输出的Debug(及以下)信息-->
<logger name="org.springframework">
<level value="INFO"></level>
</logger>

<logger name="admin_trace" additivity="false">
<level value="INFO" />
<appender-ref ref="fileAppenderInfo" />
</logger>

<root>
<level value="ALL"/>
<appender-ref ref="consoleAppender" />
<appender-ref ref="fileAppenderWarn" />
<appender-ref ref="fileAppenderError" />
</root>
</log4j:configuration>


在相应的java文件中引入

private final Logger TraceLogger = Logger.getLogger("trace");

TraceLogger.info("------");