SpringBoot集成slf4j&log4j

  • Post author:
  • Post category:其他




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
    }
}



版权声明:本文为qq_41946543原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。