当前位置:网站首页>Use of log4j
Use of log4j
2022-07-19 05:23:00 【qq_ forty-two million forty-two thousand one hundred and fifty-】
What is? Log4j?
- Log4j yes Apache One An open source project , By using Log4j, The destination where we can control the delivery of log information is the console 、 file 、 GU| Components
- We can also control the output format of each log ;
- By defining the level of each log message , We can control the log generation process more carefully .
- Flexible configuration through a configuration file , Without changing the code of the application .
1、 First import log4j My bag
pom Import dependency in file
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
2、 Writing configuration files log4j.properties
Appender Output destination for log ,Log4j Provided appender There are the following :
org.apache.log4j.ConsoleAppender( Console ),
org.apache.log4j.FileAppender( file ),
org.apache.log4j.DailyRollingFileAppender( Generate a log file every day ),
org.apache.log4j.RollingFileAppender( A new file is generated when the file size reaches the specified size ),
org.apache.log4j.WriterAppender( Send log information in stream format to any specified place )
Layout: Log output format ,Log4j Provided layout There are the following :
org.apache.log4j.HTMLLayout( With HTML Tabular layout ),
org.apache.log4j.PatternLayout( The layout mode can be specified flexibly ),
org.apache.log4j.SimpleLayout( Contains the level of log information and the information string ),
org.apache.log4j.TTCCLayout( Include the time when the log was generated 、 Threads 、 Categories and so on )
Printing parameters : Log4J A similar C In language printf The print format of the function formats the log information , as follows :
%m The message specified in the output code
%p Output priority , namely DEBUG,INFO,WARN,ERROR,FATAL
%r The output starts from the application to the output log The number of milliseconds that information takes
%c The output belongs to , Usually the full name of the class
%t Output the name of the thread that generated the log event
%n Output a carriage return line feed ,Windows The platform is “\r\n”,Unix The platform is “\n”
%d Output the date or time of the log time point , The default format is ISO8601, You can also specify the format after , such as :%d{yyy MMM dd HH:mm:ss , SSS}, The output is similar to :2002 year 10 month 18 Japan 22 : 10 : 28 , 921
%l Where the output log event occurred , Including category name 、 The thread that happened , And the number of lines in the code . give an example :Testlog4.main(TestLog4.java: 10 )
### set log levels ###
log4j.rootLogger = DEBUG,Console,File
### Output to console ###
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%d{yy/MM/dd HH:mm:ss:SSS}]-%l:%m%n
### Output to a log file ###
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=${project}src\\main\\resources\\app.log
log4j.appender.File.MaxFileSize=10MB
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%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.sq1.PreparedStatement=DEBUG
3、 To configure log4j For the implementation of log
stay mybatis Set the log factory to log4j
<settings>
<!-- Appoint MyBatis The implementation of the logs used , Use LOG4J -->
<setting name="logImpl" value="LOG4J" />
</settings>
After completing these two steps , We can be like in application It is the same in development web application Apply anywhere log4j 了 . The following is the javabean An example of application in .
import org.apache.log4j.Logger;
public class InfoForm
{
static Logger logger = Logger.getLogger(InfoForm.class);
protected String title;
protected String content;
public InfoForm() {
}
public void setTitle(Object value)
{
logger.debug("nepalon:title = " + title);
title = value;
}
public String getTitle()
{
logger.debug("nepalon:title = " + title);
return title;
}
public void setContent(String value)
{
content = value;
logger.debug("nepalon: content() = " + content);
}
public String getContent()
{
logger.debug("nepalon: content = \n" + content);
return content;
}
}
边栏推荐
- mysql的锁
- Es6最新常用知识宝典(能够帮助你解决面试题困惑,编写程序中出现的问题等)
- 单臂路由配置
- Continue from the previous issue: the remaining two methods of the rotation chart
- redis 源码分析 动态字符串实现(sds)
- Easypoi之excel多sheet导入
- Implementation of synchronization interface of 6 libcurl based on libco
- 第一个智能合约程序Faucet.sol
- 实习项目3-更改所有者
- 基于libco的协程实现6 libcurl的同步接口的实现方案
猜你喜欢
随机推荐
云服务器部署WEB项目
Wechat applet wx Setclipboarddata copy text
UML(用例图,类图,对象图,包图)
Easypoi之excel多sheet导入
Swagger配置与使用
Two JS methods of rolling wheel loading and modal box dragging
[ES6] use multiple functions such as adding data, filtering and transmitting to the page to realize cases
遍历的方法总结
Log4j的使用
2.6.2 内存泄漏
热更新及其原理
MySQL安装配置教程(超级详细)
性能瓶颈查找-火焰图分析
第一个智能合约程序Faucet.sol
mysql的事务
Submit the uniapp form (input, radio, picker) to get the parameter value
【AI】利用简单神经网络做动作识别——基于coco关键点
基于PaddleOCR解决文本检测训练模型与inference模型预测效果不一致的问题
交换机用户模式、特权模式、全局模式、端口模式
Easypoi之excel简单导出









