Maven项目中使用日志slf4j的步骤

Maven项目中使用日志slf4j的步骤

1、在POM.XML中加入依赖(红色部分)

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
   <exclusions>
      <exclusion>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-log4j12</artifactId>
      </exclusion>
      <exclusion>
         <groupId>log4j</groupId>
         <artifactId>log4j</artifactId>
      </exclusion>
   </exclusions>
</dependency>

2、在资源文件夹(resources)新建属性文件(log4j.properties),内容如下:

log4j.rootLogger=INFO, 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
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

3、在使用到的类中导入、定义和使用

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger logger = LoggerFactory.getLogger(MyClass.class);//红色替换为使用的类名
logger.info(" LOG test.");//在需要的地方使用。
posted @ 2022-03-25 09:42  甜菜波波  阅读(1790)  评论(0编辑  收藏  举报