spring-boot系列——使用log4j2

        原创文章,转载请注明

        spring-boot中使用log4j2其实非常简单,因为spring-boot本身也使用了,而且还不止log4j2。我们如果要用的话必须把spring-boot-starter-logging的依赖去掉,不然会是spring-boot默认的日志,该starter在spring-boot-starter中。既然去除了官方的日志依赖,同时也去除了log4j2的依赖,因此我们还需要把log4j2的依赖加进来。

         之后就可以自己配置log4j2了,官方的说明文档在76.2章节。

        摘录如下:

76.2 Configure Log4j for logging

Spring Boot supports Log4j 2 for loggingconfiguration if it is on the classpath. If you are using the starters forassembling dependencies that means you have to exclude Logback and then include log4j 2instead. If you aren’t using the starters then you need to provide jcl-over-slf4j(at least) in addition to Log4j 2.

The simplest path is probably through the starters, even though it requires somejiggling with excludes, .e.g. in Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
[Note]

The use of the Log4j starters gathers together the dependencies for common loggingrequirements (e.g. including having Tomcat use java.util.logging but configuring theoutput using Log4j 2). See the Actuator Log4j 2 samples for more detail and to see it inaction.


76.2.1 Use YAML or JSON to configure Log4j 2

In addition to its default XML configuration format, Log4j 2 also supports YAML and JSONconfiguration files. To configure Log4j 2 to use an alternative configuration file format,add the appropriate dependencies to the classpath and name yourconfiguration files to match your chosen file format:

Format DependenciesFile names

YAML

com.fasterxml.jackson.core:jackson-databind com.fasterxml.jackson.dataformat:jackson-dataformat-yaml

log4j2.yaml log4j2.yml

JSON

com.fasterxml.jackson.core:jackson-databind

log4j2.json log4j2.jsn



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值