SpringBoot使用log4j2

先在maven里把spring-boot-starter里的spring-boot-starter-logging依赖去掉

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

添加log4j2的依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

在application.yml里配置日志设置文件

logging:
  config: classpath:log4j2.properties

log4j2.properties内容

status = warn
name = MyApp

filter.threshold.type = ThresholdFilter
filter.threshold.level = debug

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %-d{yyyy-MM-dd HH:mm:ss} [ %-5p ] %m (%c:%L)%n

appender.rolling.type = File
appender.rolling.name = log
appender.rolling.append = true
appender.rolling.fileName = d:\\test1.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern =  %-d{yyyy-MM-dd HH:mm:ss} [ %-5p ] %m (%c:%L)%n

rootLogger.level = debug
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.log.ref = log

log4j2的官方文档logging.apache.org/log4j/2.x/index.html

posted @ 2018-02-02 14:49  A_yes  阅读(173)  评论(0编辑  收藏  举报