使用环境:
1.IED工具 idea
2.系统环境win10
错误原因:
1.根本原因是日志问题
解决方式:
1.pom文件添加jar包
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.2</version>
</dependency>
2.引入log包
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
3.main方法中初始化
3.1加载配置文件
3.2初始化配置文件
PropertyConfigurator.configure("F:/IdeaProjects/test/src/main/log4j.properties");//加载.properties文件
Logger log=Logger.getLogger("org.zblog.test");
log.info("--------------测试");
说明:
import org.apache.log4j.xml.DOMConfigurator; 此包使用来加载xml格式配置文件
log4j.properties:
配置文件内容
### 设置日志级别及日志存储器 ###
#log4j.rootLogger=DEBUG, Console
### 设置日志级别及日志存储器 ###
log4j.rootLogger=info,consolePrint,errorFile,logFile
#log4j.rootLogger=DEBUG,consolePrint,errorFile,logFile,Console
### 输出到控制台 ###
log4j.appender.consolePrint.Encoding = UTF-8
log4j.appender.consolePrint = org.apache.log4j.ConsoleAppender
log4j.appender.consolePrint.Target = System.out
log4j.appender.consolePrint.layout = org.apache.log4j.PatternLayout
log4j.appender.consolePrint.layout.ConversionPattern=%d %p [%c] - %m%n
### 输出到日志文件 ###
log4j.appender.logFile.Encoding = UTF-8
log4j.appender.logFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.logFile.File = F:/IdeaProjects/test/target/log/test.log
log4j.appender.logFile.Append = true
log4j.appender.logFile.Threshold = info
log4j.appender.logFile.layout = org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 保存异常信息到单独文件 ###
log4j.appender.errorFile.Encoding = UTF-8
log4j.appender.errorFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.errorFile.File = D:/RUN_Data/log/dajiangtai_error.log
log4j.appender.errorFile.Append = true
log4j.appender.errorFile.Threshold = ERROR
log4j.appender.errorFile.layout = org.apache.log4j.PatternLayout
log4j.appender.errorFile.layout.ConversionPattern =%-d{yyyy-MM-dd HH\:mm\:ss} [ %t\:%r ] - [ %p ] %m%n
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
#log4j.logger.com.dajiangtai.dao=DEBUG,TRACE
log4j.logger.com.dajiangtai.dao.IFollowDao=DEBUG