spring boot 使用log4j2

 spring boot 默认是使用logback来进行日志记录 如果要使用log4j2就需要把logback从spring boot中去掉 最终目的 如果有依赖引用了slf4j 或log4j那就把它们从一来中排除掉

spring-boot-starter-web和spring-boot-starter-Redis依赖中需要exclude掉log4j-over-slf4j或spring-boot-starter-logging,因为spring-boot-starter-log4j中已经有了log4j的slf4j依赖,如果不去掉,会出现重复依赖的情况。这个时候,在初始化log4j的时候就会报出 

根据上面的名字得出类似spring-boot-starter的jar包最好都要排除sl4j 不过spring-boot-starter-test经过测试可以不用排除 当然可以为了统一所有包含spring-boot-starter的依赖都可以干掉sl4j

如果还有其他依赖引用了也许干掉

Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError.的异常信息,具体可以参见以下地址:
http://www.slf4j.org/codes.html#log4jDelegationLoop
http://blog.csdn.net/lee_decimal/article/details/38515119

pom.xml

<!-- exclude掉spring-boot的默认log配置 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</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>
<!-- 加上这个才能辨认到log4j2.yml文件 -->
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-yaml</artifactId>
</dependency>

下面是跟log4j和sl4j有关的jar包 不过不需排除这么多 这需要根据上面的pom.xml排除即可

<exclusions>
    <exclusion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </exclusion>
    <exclusion>
        <artifactId>log4j-over-slf4j</artifactId>
        <groupId>org.slf4j</groupId>
    </exclusion>
    <exclusion>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </exclusion>
    <exclusion>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
    </exclusion>
    <exclusion>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
    </exclusion>
    <exclusion>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
    </exclusion>
    <exclusion>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
    </exclusion>
    <exclusion>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
    </exclusion>
    <exclusion>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
    </exclusion>
    <exclusion>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </exclusion>
</exclusions>

附:

表 1. Spring Boot 推荐的基础 POM 文件

名称

说明

spring-boot-starter

核心 POM,包含自动配置支持、日志库和对 YAML 配置文件的支持。

spring-boot-starter-amqp

通过 spring-rabbit 支持 AMQP。

spring-boot-starter-aop

包含 spring-aop 和 AspectJ 来支持面向切面编程(AOP)。

spring-boot-starter-batch

支持 Spring Batch,包含 HSQLDB。

spring-boot-starter-data-jpa

包含 spring-data-jpa、spring-orm 和 Hibernate 来支持 JPA。

spring-boot-starter-data-mongodb

包含 spring-data-mongodb 来支持 MongoDB。

spring-boot-starter-data-rest

通过 spring-data-rest-webmvc 支持以 REST 方式暴露 Spring Data 仓库。

spring-boot-starter-jdbc

支持使用 JDBC 访问数据库。

spring-boot-starter-security

包含 spring-security。

spring-boot-starter-test

包含常用的测试所需的依赖,如 JUnit、Hamcrest、Mockito 和 spring-test 等。

spring-boot-starter-velocity

支持使用 Velocity 作为模板引擎。

spring-boot-starter-web

支持 Web 应用开发,包含 Tomcat 和 spring-mvc。

spring-boot-starter-websocket

支持使用 Tomcat 开发 WebSocket 应用。

spring-boot-starter-ws

支持 Spring Web Services。

spring-boot-starter-actuator

添加适用于生产环境的功能,如性能指标和监测等功能。

spring-boot-starter-remote-shell

添加远程 SSH 支持。

spring-boot-starter-jetty

使用 Jetty 而不是默认的 Tomcat 作为应用服务器。

spring-boot-starter-log4j

添加 Log4j 的支持。

spring-boot-starter-logging

使用 Spring Boot 默认的日志框架 Logback。

spring-boot-starter-tomcat

使用 Spring Boot 默认的 Tomcat 作为应用服务器。

所有这些 POM 依赖的好处在于为开发 Spring 应用提供了一个良好的基础。Spring Boot 所选择的第三方库是经过考虑的,是比较适合产品开发的选择。但是 Spring Boot 也提供了不同的选项,比如日志框架可以用 Logback 或 Log4j,应用服务器可以用 Tomcat 或 Jetty。

 

推荐:http://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值