当前位置:网站首页>[log] what does a log do? What is a log factory? Configuration and use of log4j? log4j. Properties file configuration, log4j jar package coordinates
[log] what does a log do? What is a log factory? Configuration and use of log4j? log4j. Properties file configuration, log4j jar package coordinates
2022-07-28 09:37:00 【Songdanmin】
List of articles
journal
1 Log factory
If a database operation , Something is wrong , We need to make a mistake , The journal is the best assistant !
once :sout 、 debug
Now? : Log factory
https://mybatis.org/mybatis-3/zh/configuration.html#settings

SLF4J simple logging facade for java Abbreviation ,java Simple appearance log
LOG4J(3.5.9 Start and discard ) 【 master 】
LOG4J2
JDK_LOGGING
COMMONS_LOGGING
STDOUT_LOGGING 【 master 】
NO_LOGGING
stay mybatis Try which log to use in , stay setting Set the !
STDOUT_LOGGING Standard log output

mybaits-config.xml
<settings>
<!-- journal -->
<!--STDOUT_LOGGING Standard log output -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
2 log4j
What is? log4j?
- yes apache An open source project of
- adopt log4j, The destination where we can control the delivery of log information is the console or In the file or GUI Components
- Can pass log4j Control the output format of each log
- By defining the level of each log message , The production process of logs can be controlled more carefully
- Flexible configuration through a configuration file ,【log4j.properties 】, No code changes required
How to use log4j?
Import log4j jar package \
pom.xml
<!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>stay resources Set up under the log4j.properties
log4j.properties
# Rank as DEBUG Log information output to console and file These two destinations ,console and file Is defined in the following code log4j.rootLogger=DEBUG,console,file # Related settings of console output log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.Threshold=DEBUG log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=[%c]-%m%n # File output related settings log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File=./log/kuang.log log4j.appender.file.MaxFileSize=10mb log4j.appender.file.Threshold=DEBUG log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n # Log output level log4j.logger.org.mybatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUGTo configure log4j For the implementation of log
mybatis-config.xml
<settings> <!--LOG4J log4j Log output --> <setting name="logImpl" value="LOG4J"/> </settings>Log4j Use , test run findUserById
UserMapperTest.java
package com.kuang.dao; import com.kuang.pojo.User; import com.kuang.utils.MyBatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; public class UserMapperTest { @Test public void findUserByIdTest(){ SqlSession sqlSession = MyBatisUtils.getSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = userMapper.findUserById(2); System.out.println(user); sqlSession.close(); } }test result :

Automatically generated log file :

Easy to use
To use log4j In the class , Import package import org.apache.log4j.Logger;
Log object , The parameter is of the current class class
static Logger logger = Logger.getLogger(UserDaoTest.class);The level of logging
logger.info("info: Into the testLog4j Method "); logger.error("error: Into the testLog4j Method "); logger.debug("debug: Into the testLog4j Method ");
UserMapperTest.java
package com.kuang.dao;
import com.kuang.pojo.User;
import com.kuang.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;
public class UserMapperTest {
@Test
public void findUserByIdTest(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
// Get UserMapper Object instances
Logger logger = Logger.getLogger(UserMapper.class);
logger.info(" test , go in getUserById Method success !");
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findUserById(2);
System.out.println(user);
sqlSession.close();
}
@Test
public void testLog4j(){
// Get UserMapper Object instances
Logger logger = Logger.getLogger(UserMapper.class);
logger.info("info: Into the testLog4j Method ");
logger.error("error: Into the testLog4j Method ");
logger.debug("debug: Into the testLog4j Method ");
}
}
kuang.log

边栏推荐
- Regular expressions for positive and negative values
- What is the difference between these two sets of code?
- 2.9.5 ext JS object type processing and convenient methods
- Heuristic merging on tree
- ARouter源码解析(三)
- 个人博客小程序
- ECCV 2022 | can be promoted without fine adjustment! Registration based anomaly detection framework for small samples
- Technology sharing | quick intercom integrated dispatching system
- Magic brace- [group theory] [Burnside lemma] [matrix fast power]
- 使用 OpenSSL 创建ssl证书
猜你喜欢

js数组去重,id相同对某值相加合并

2022 examination question bank and simulation examination of crane driver (limited to bridge crane)

376. 摆动序列【贪心、动态规划------】

Talk to the father of MySQL: code completion at one time is a good programmer

IJCAI 2022 | the latest overview of graph structure learning: research progress and future prospects
![[Download] several tools for brute force cracking and dictionary generation are recommended](/img/c6/f4a9c566ff21a8e133a8a991108201.png)
[Download] several tools for brute force cracking and dictionary generation are recommended

正负数值的正则表达式
![[package deployment]](/img/6f/93a35436947311bc2305adcb0df1a6.png)
[package deployment]

【C语言】详解顺序表(SeqList)
![[C language] detailed explanation sequence table (seqlist)](/img/60/c8cee6a6afe57247aba583291cc99b.png)
[C language] detailed explanation sequence table (seqlist)
随机推荐
The cooperation between starfish OS and metabell is just the beginning
Promise实例如何解决地狱回调
Face warp - hand tear code
对话MySQL之父:代码一次性完成才是优秀程序员
QT基础练手小程序-简单计算器设计(附带源码,解析)
Window源码解析(四):Window的删除机制
MATLAB的数列与极限运算
Activiti startup error: cannot create poolableconnectionfactory (could not create connection to database server
RGB-T追踪——【多模态融合】Visible-Thermal UAV Tracking: A Large-Scale Benchmark and New Baseline
canvas常用原型方法及绘制图片应用
IntelliJ idea associated database
js数组去重,id相同对某值相加合并
FPGA开发学习开源网站汇总
[package deployment]
Pytorch deep learning practice lesson 9 multi classification problems (handwritten numeral MNIST)
19c SYSAUX表空间SQLOBJ$PLAN表过大,如何清理
Heuristic merging on tree
2022 examination question bank and simulation examination of crane driver (limited to bridge crane)
2022 safety officer-b certificate examination simulated 100 questions and answers
2022 safety officer-b certificate examination simulated 100 questions and answers