log4j.properties 配置(实例讲解)

发布时间:2020-10-06 15:42:31 作者:jingxian
来源:脚本之家 阅读:337

1、需要的jar如下:

!-- log4j -->
<dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.17</version>
</dependency>
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>1.7.4</version>
</dependency>
<dependency> 
 <groupId>org.slf4j</groupId> 
 <artifactId>slf4j-log4j12</artifactId> 
 <version>1.7.4</version> 
 <scope>runtime</scope> 
</dependency> 

2、将log4j.properties放入项目src路径下即可,虚拟机会自动加载日志配置文件,文件内容如下:

### set log levels ###
log4j.rootLogger = DEBUG , C , D , E 

### console ###
log4j.appender.C = org.apache.log4j.ConsoleAppender
log4j.appender.C.Target = System.out
log4j.appender.C.layout = org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern = [mybatis_study][%p] [%-d{yyyy-MM-dd HH\:mm\:ss}] %C.%M(%L) | %m%n

### log file ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ../logs/mybatis_study.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = INFO 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = [mybatis_study][%p] [%-d{yyyy-MM-dd HH\:mm\:ss}] %C.%M(%L) | %m%n

### exception ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = ../logs/mybatis_study_error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = [mybatis_study][%p] [%-d{yyyy-MM-dd HH\:mm\:ss}] %C.%M(%L) | %m%n

###mybatis show sql###
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug

log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug

3、也可以配置log4j.xml,如下:

<?xml version="1.0" encoding="UTF-8"?> 
 <!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd"> 
 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> 

  <!-- [控制台STDOUT] --> 
  <appender name="console" class="org.apache.log4j.ConsoleAppender"> 
   <param name="encoding" value="GBK" /> 
   <param name="target" value="System.out" /> 
   <layout class="org.apache.log4j.PatternLayout"> 
   <param name="ConversionPattern" value="%-5p %c{2} - %m%n" /> 
   </layout> 
  </appender> 

  <!-- [公共Appender] --> 
  <appender name="DEFAULT-APPENDER" class="org.apache.log4j.DailyRollingFileAppender"> 
   <param name="File" value="${webapp.root}/logs/rms-default.log" /> 
   <param name="Append" value="true" /> 
   <param name="encoding" value="GBK" /> 
   <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" /> 
   <layout class="org.apache.log4j.PatternLayout"> 
  <param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" /> 
   </layout> 
  </appender> 

  <!-- [错误日志APPENDER] --> 
  <appender name="ERROR-APPENDER" class="org.apache.log4j.DailyRollingFileAppender"> 
   <param name="File" value="${webapp.root}/logs/rms-error.log" /> 
   <param name="Append" value="true" /> 
   <param name="encoding" value="GBK" /> 
   <param name="threshold" value="error" /> 
   <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" /> 
   <layout class="org.apache.log4j.PatternLayout"> 
   <param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" /> 
   </layout> 
  </appender> 

  <!-- Root Logger --> 
  <root> 
   <level value="${rootLevel}"></level> 
   <appender-ref ref="DEFAULT-APPENDER" /> 
   <appender-ref ref="ERROR-APPENDER" /> 
   <appender-ref ref="console" /> 
  </root> 
 </log4j:configuration>

以上这篇log4j.properties 配置(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。

推荐阅读:
  1. log4j
  2. log4j最清晰的日志配置文件,支持文件和控制台

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

log4j properties 配置

上一篇:Elasticsearch搜索调优权威指南 (2/3)

下一篇:selenium+python自动化测试之多窗口切换

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》