第一步:在pom.xml中导入依赖
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
第二步:在springboot的resources新建log4j.properties文件,然后粘贴以下内容:
log4j.rootLogger=TRACE, 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
# Print the date in ISO 8601 format
#log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
#下面的是制定输出到打印台格式的内容
log4j.appender.stdout.layout.ConversionPattern=%-4r %-5p %d{yyyy-MM-dd HH:mm:ssS} %c -%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=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
#log4j.appender.R.layout.ConversionPattern=[%p] %t %c - %m%n -%d{yyyy-MM-dd HH:mm}
log4j.appender.R.layout.ConversionPattern=[%p] %t %d{yyyy-MM-dd HH:mm:ss,SSS} %c - {%m} %n
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN
第三步:编写测试代码:
@Test
public void testLog4J(){
Logger logger = LogManager.getLogger(testMd5.class);
logger.info("this is the first message");
logger.info("this is the second message");
int sum = 1+4;
logger.info("The Sum is"+sum);
}
第四步:测试输出
0 INFO 2020-09-27 20:36:07677 com.test.testMd5 -this is the first message
2 INFO 2020-09-27 20:36:07679 com.test.testMd5 -this is the second message
2 INFO 2020-09-27 20:36:07679 com.test.testMd5 -The Sum is5
Process finished with exit code 0
第五步:查看生成的日志文件