1.本人的maven项目(pom.xml中加入以下依赖,log4j的依赖)
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>
<artifactId>log4jdbc-log4j2-jdbc4.1</artifactId>
<version>1.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
2.项目log4j.properties的目录结构
3.结果测试代码打印不了加载不了log4j.properties文件打印结果如下:
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
4.原因具体看图设置:
如果有人不知道如何设置resources属性,打开File - Project Structure 左侧选中Project Settings - Modules 右侧选中 你的项目名成,右侧选择第一个Sources选项卡,然后选择需要resources类型的文件夹,选中Resources即可。
5.后来项目log4j.properties的目录结构变成如下面:
6.测试结果正常结果:
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 282828951.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@10dba097]
DEBUG [main] - ==> Preparing: SELECT * FROM t_user WHERE tid = ?;
DEBUG [main] - ==> Parameters: 1001(Integer)
DEBUG [main] - <== Total: 1
User{tid=1001, uname='haoren', birthday=Mon Feb 04 00:00:00 CST 2019, sex=1, address='湖南邵阳'}
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@10dba097]
DEBUG [main] - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@10dba097]
DEBUG [main] - Returned connection 282828951 to pool.
Always keep the faith!!!