Mybatis、maven專案中整合log4j (17)

東北大亨發表於2021-10-19

Mybatis、maven專案總整合log4j 

java 中Mybatis、maven專案總整合log4j 

1、pom增加log4j包引用

 

 2、新增 log4j.properties檔案

# java中在maven專案增加log4j日誌支援
#log4j.rootLogger=CONSOLE,FILE
log4j.rootLogger=DEBUG,CONSOLE,FILE
#應用於控制檯
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=DEBUG
#log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.Encoding=GBK
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[lsjSso]%d-%c-%-4r[%t]%-5p%c%x-%m%n
#每天新建日誌
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=D:/logs/jackLog
log4j.appender.A1.Encoding=GBK
log4j.appender.A1.Threshold=DEBUG
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{ABSOLUTE}%5p%c{1}:%L:%m%n
#應用於檔案
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=D:/logs/jackLog/file.log
log4j.appender.FILE.Append=false
log4j.appender.FILE.Encoding=GBK
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[lsjSso]%d-%c-%-4r[%t]%-5p%c%x-%m%n

 3、pom增加日誌支援

 

 4、執行(16)例子應用,在控制檯列印日誌通知在本地磁碟建立資料夾同時寫入日誌

4.1 控制檯日誌

 

"C:\Program Files\Java\jdk1.8.0_25\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar=1051:C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\lib\idea_rt.jar;C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\plugins\junit\lib\junit-rt.jar;C:\Users\newsoft\AppData\Roaming\JetBrains\IntelliJ IDEA 2017.3.5\plugins\junit\lib\junit5-rt.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-launcher\1.5.2\junit-platform-launcher-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\apiguardian\apiguardian-api\1.1.0\apiguardian-api-1.1.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-engine\1.5.2\junit-platform-engine-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\platform\junit-platform-commons\1.5.2\junit-platform-commons-1.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\jupiter\junit-jupiter-engine\5.5.2\junit-jupiter-engine-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\jupiter\junit-jupiter-api\5.5.2\junit-jupiter-api-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\junit\vintage\junit-vintage-engine\5.5.2\junit-vintage-engine-5.5.2.jar;D:\download\lib\mavenTollTransfer\mic-repository\junit\junit\4.12\junit-4.12.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\rt.jar;D:\ideaworkspace\ProjectStudy\mybatis-02\target\classes;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest-core\2.1\hamcrest-core-2.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\hamcrest\hamcrest\2.1\hamcrest-2.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\mybatis\mybatis\3.4.5\mybatis-3.4.5.jar;D:\download\lib\mavenTollTransfer\mic-repository\mysql\mysql-connector-java\5.1.44\mysql-connector-java-5.1.44.jar;D:\download\lib\mavenTollTransfer\mic-repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\slf4j\slf4j-log4j12\1.7.12\slf4j-log4j12-1.7.12.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\download\lib\mavenTollTransfer\mic-repository\cglib\cglib\3.3.0\cglib-3.3.0.jar;D:\download\lib\mavenTollTransfer\mic-repository\org\ow2\asm\asm\7.1\asm-7.1.jar;D:\download\lib\mavenTollTransfer\mic-repository\net\logstash\logback\logstash-logback-encoder\5.3\logstash-logback-encoder-5.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-databind\2.10.3\jackson-databind-2.10.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-annotations\2.10.3\jackson-annotations-2.10.3.jar;D:\download\lib\mavenTollTransfer\mic-repository\com\fasterxml\jackson\core\jackson-core\2.10.3\jackson-core-2.10.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit5 com.mybatis03.test.testTeacher01,queryGroupAndTeachers
[lsjSso]2021-10-19 19:34:58,081-org.apache.ibatis.logging.LogFactory-0   [main]DEBUGorg.apache.ibatis.logging.LogFactory-Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
[lsjSso]2021-10-19 19:34:58,188-org.apache.ibatis.logging.LogFactory-107 [main]DEBUGorg.apache.ibatis.logging.LogFactory-Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[lsjSso]2021-10-19 19:34:58,208-org.apache.ibatis.datasource.pooled.PooledDataSource-127 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections.
[lsjSso]2021-10-19 19:34:58,208-org.apache.ibatis.datasource.pooled.PooledDataSource-127 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections.
[lsjSso]2021-10-19 19:34:58,208-org.apache.ibatis.datasource.pooled.PooledDataSource-127 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections.
[lsjSso]2021-10-19 19:34:58,209-org.apache.ibatis.datasource.pooled.PooledDataSource-128 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-PooledDataSource forcefully closed/removed all connections.
[lsjSso]2021-10-19 19:34:58,339-org.apache.ibatis.transaction.jdbc.JdbcTransaction-258 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Opening JDBC Connection
[lsjSso]2021-10-19 19:34:58,586-org.apache.ibatis.datasource.pooled.PooledDataSource-505 [main]DEBUGorg.apache.ibatis.datasource.pooled.PooledDataSource-Created connection 752316209.
[lsjSso]2021-10-19 19:34:58,587-org.apache.ibatis.transaction.jdbc.JdbcTransaction-506 [main]DEBUGorg.apache.ibatis.transaction.jdbc.JdbcTransaction-Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@2cd76f31]
[lsjSso]2021-10-19 19:34:58,590-com.mybatis03.mapper.TeacherMapper.queryGroupAndTeachers-509 [main]DEBUGcom.mybatis03.mapper.TeacherMapper.queryGroupAndTeachers-==>  Preparing: SELECT g.*,t.* FROM teachergroup g INNER JOIN teacher t ON g.groupid = t.groupno WHERE g.groupid =? 
[lsjSso]2021-10-19 19:34:58,626-com.mybatis03.mapper.TeacherMapper.queryGroupAndTeachers-545 [main]DEBUGcom.mybatis03.mapper.TeacherMapper.queryGroupAndTeachers-==> Parameters: 1(Integer)
[lsjSso]2021-10-19 19:34:58,642-com.mybatis03.mapper.TeacherMapper.queryGroupAndTeachers-561 [main]DEBUGcom.mybatis03.mapper.TeacherMapper.queryGroupAndTeachers-<==      Total: 2
實現一對多關聯,一個教研組對應多個教師,查詢結果為:TeacherGroup{groupID=1, groupName='chinese', teacherList{teaNo=1001,courseNo=7001,teaName=sara,groupNo=1,teaNo=1002,courseNo=7002,teaName=sakula,groupNo=1}}

Process finished with exit code 0

4.2 控制檯日誌

A、檔案目錄

  

 

B、file.log 檔案內容(檔案內容和控制檯列印日誌相同)

  

 日誌分析:

 

 

 總結:日誌新增說明

1、log4j,新增log4j的jar包(既:在mybatis-03.xml中引用了jar包)

2、開啟日誌 (mybatis-03.xml 中開啟日誌)

3、如果不指定,mybatis就會根據以下順序尋找日誌

      SLF4J-> Apache Commons Logging ---> Log4j 2 ----->Log4j---->JDK logging 

4、編寫日誌輸出檔案:log4j.properties 日誌級別:DEBUG<INFO<WARN<ERROR

 

相關文章