SpringBoot集成slf4j&log4j

SpringBoot下日志框架的使用 log4j/slf4j

依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.28</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

在 resources下创建log4j.properties

# 全局日志配置,file 为需要输出到本地才需加上

log4j.rootLogger=info, stdout,file
# 显示mapper下输出的日志级别为DEBUG
# 以下代码中的 com.june.ims.mapper 需要根据自己项目的命名改变
log4j.logger.com.june.ims.mapper=DEBUG
# 显示controller下的输出级别为DEBUG
log4j.logger.com.june.ims.controller=DEBUG
# 控制台输出的内容,默认一般不改

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

# log日志文件输出到本地

#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.file=c:/tmp/mylog.log

#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%5p [%t] - %m%n

简单使用slf4j

@Controller
@Slf4j
public class MyControlelr{
    @GetMapping("/demo")
    public String demo(){
        //使用SLF4J的格式化功能
        Object entry = new SomeObject();
        log.debug(“The entry is {}.”, entry);
        //连续的{}才被认为是格式化占位符,所以:
        log.debug(“Set {1,2} differs from {}”, “3”);//output:Set{1,2} differs from 3
        log.debug(“Set {1,2} differs from {{}}”, “3”);//output:Set{1,2} differs from {3}
        //用”\”转义{}占位符
        log.debug(“Set \{} differs from {}”, “3”); //output:Set {} differs from 3
        // 直接输出内容
        log.info("hello world"); // output:hello world
    }
}

SpringBoot集成slf4j&log4jSpringBoot集成slf4j&log4j 鼠色猫 发布了32 篇原创文章 · 获赞 8 · 访问量 2050 私信 关注
上一篇:4.springboot:日志


下一篇:log4j,logback,log4j2和slf4j关系