Log4j 下載地址
Log4j是Apache的一個開源項目,通過使用Log4j,我們可以控制日志信息輸送的目的地是控制台、文件、GUI組件
log4j-1.2.17.jar:https://repo1.maven.org/maven2/log4j/log4j/1.2.17/log4j-1.2.17.jar
Maven
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
文件大小分割日志文件
效果如下
logs
log.log
log.log.1
log.log.2
log.log.3
log4j.properties 配置文件
### 將等級為debug的日志輸出到console和file, console和file的定義在下面 ###
log4j.rootLogger = DEBUG,console,file
### console 配置輸出到控制台 ###
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
#log4j.appender.console.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
### file 配置輸出到文件 ###
### RollingFileAppender: 以文件大小分割達到指定大小就產生新文件, MaxFileSize: 指定文件大小, MaxBackupIndex: 最多保存多少個文件 ###
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize = 10kb
log4j.appender.file.MaxBackupIndex = 10
log4j.appender.file.File = logs/log.log
log4j.appender.file.Threshold = DEBUG
log4j.appender.file.Append = true
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = [%p] [%d{yyyy-MM-dd HH:mm:ss}] [%c]%m%n
#log4j.appender.file.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 設置輸出sql的級別,其中logger后面的內容全部為jar包中所包含的包名 ###
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
#log4j.logger.java.sql.Connection=DEBUG
以日期分割每天產生一個日志文件
效果如下
logs
log.log
log.log_2021-01-15.log
log.log_2021-01-16.log
log.log_2021-01-17.log
log4j.properties 配置文件
### 將等級為debug的日志輸出到console和file, console和file的定義在下面 ###
log4j.rootLogger = DEBUG,console,file
### console 配置輸出到控制台 ###
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
#log4j.appender.console.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
### file 配置輸出到文件 ###
### DailyRollingFileAppender: 以日期分割每天產生一個文件, DatePattern: 日志文件名稱格式 ###
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern = '_'yyyy-MM-dd'.log'
log4j.appender.file.File = logs/log.log
log4j.appender.file.Threshold = DEBUG
log4j.appender.file.Append = true
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern = [%p] [%d{yyyy-MM-dd HH:mm:ss}] [%c]%m%n
#log4j.appender.file.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 設置輸出sql的級別,其中logger后面的內容全部為jar包中所包含的包名 ###
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
#log4j.logger.java.sql.Connection=DEBUG
自定義信息輸出到日志文件
-
在要輸出日志的類中加入 Log4j
-
包一定不要導錯,導包:
import org.apache.log4j.Logger;
-
日志對象, 參數為當前類的class
static Logger logger = Logger.getLogger(UserMapperTest.class);
- 使用
import org.apache.log4j.Logger;
import org.junit.Test;
public class UserMapperTest {
static Logger logger = Logger.getLogger(UserMapperTest.class);
@Test
public void testLog4j() {
logger.info("UserMapperTest testLog4j info --> 日志輸出");
logger.debug("UserMapperTest testLog4j debug --> 日志輸出");
logger.error("UserMapperTest testLog4j error --> 日志輸出");
}
}
運行完可以在日志文件中看到我們輸出的信息
[DEBUG] [2021-01-17 00:07:18] [com.pro.dao.UserMapper.getUserById]==> Preparing: select * from users where id = ?
[DEBUG] [2021-01-17 00:07:18] [com.pro.dao.UserMapper.getUserById]==> Parameters: 1(Integer)
[DEBUG] [2021-01-17 00:07:18] [com.pro.dao.UserMapper.getUserById]<== Total: 1
[INFO] [2021-01-17 00:15:01] [com.pro.dao.UserMapperTest]UserMapperTest testLog4j info --> 日志輸出
[DEBUG] [2021-01-17 00:15:01] [com.pro.dao.UserMapperTest]UserMapperTest testLog4j debug --> 日志輸出
[ERROR] [2021-01-17 00:15:01] [com.pro.dao.UserMapperTest]UserMapperTest testLog4j error --> 日志輸出