Board logo

标题: Spring Boot配置log4j记录日志(1) [打印本页]

作者: look_w    时间: 2019-5-16 15:19     标题: Spring Boot配置log4j记录日志(1)

引入log4j依赖

在创建Spring Boot工程时,我们引入了spring-boot-starter,其中包含了spring-boot-starter-logging,该依赖内容就是Spring Boot默认的日志框架Logback,所以我们在引入log4j之前,需要先排除该包的依赖,再引入log4j的依赖。

需要注意的是
Spring Boot 只有1.3.x和1.3.x以下版本才支持log4j的日志配置,1.3.x以上版本只支持log4j2。

区别在于需要引入的包

<artifactId>spring-boot-starter-log4j</artifactId>

    1



<artifactId>spring-boot-starter-log4j2</artifactId>

    1

Spring Boot 1.3.x和1.3.x以下版本的pom文件如下:

<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-log4j</artifactId>
</dependency>

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

Spring Boot 1.3.x以上版本的pom文件如下:

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

如果找不到

  <artifactId>spring-boot-starter</artifactId>

    1

则查找

<artifactId>spring-boot-starter-web</artifactId>

    1

因为log4j2对于log4j来说有很多变动,如果不喜欢用log4j2,1.3.x版本以上的spring boot框架也可以引入以下两个包使用log4j。

<dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
</dependency>

<dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
</dependency>




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0