柚子快報(bào)邀請(qǐng)碼778899分享:log4j(日志的配置)
柚子快報(bào)邀請(qǐng)碼778899分享:log4j(日志的配置)
日志一般配置在resources的config下面的,并且Util當(dāng)中的initLogRecord中的initLog()方法就是加載這個(gè)log4j.properties的.
首先先看log4j.properties的配置文件
log4j.rootLogger=debug, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
然后看Util包當(dāng)中的initLogRecord代碼
package Util;
import org.apache.log4j.PropertyConfigurator;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class initLogRecord {
public static void initLog() {
FileInputStream fileInputStream = null;
try {
Properties properties = new Properties();
fileInputStream = new FileInputStream("src/main/resources/config/log4j.properties");
properties.load(fileInputStream);
PropertyConfigurator.configure(properties);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
???? 下面找個(gè)例子測(cè)試一下這個(gè)日志??? 當(dāng)中initLogRecord.initLog();就是查看日志的
/**
* 查找所有數(shù)據(jù) 和日志
* @throws IOException
*/
@Test
public void findAll() throws IOException {
initLogRecord.initLog();
//1.讀取mybatis的核心配置文件
InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
//2.通過配置信息獲取一個(gè)SqlSessionFactory
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
//3.通過工廠獲取一個(gè)sqlSession對(duì)象
SqlSession sqlSession = build.openSession();
//4.通過空間名+id找到要執(zhí)行的sql語句并執(zhí)行sql語句
List
for (students students : list) {
System.out.println(students);
}
}
結(jié)果展示
最后切記,一定要在maven的配置當(dāng)中導(dǎo)入log4j坐標(biāo)?
柚子快報(bào)邀請(qǐng)碼778899分享:log4j(日志的配置)
相關(guān)鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。