在SpringBoot中禁用Logback

看来Springboot会自动configuration使用Logback和Tomcat。 我想禁用这个,并使用我在我的类path中提供的。

下面的错误消息。

LoggerFactory不是Logback LoggerContext,但Logback位于类path中。 删除Logback或竞争的实现(类org.slf4j.impl.SimpleLoggerFactory)类[org.slf4j.impl.SimpleLoggerFactory]的对象必须是类ch.qos.logback.classic.LoggerContext的实例

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>1.0.1.RELEASE</version> </parent> <groupId>com.fe</groupId> <artifactId>cloudapp</artifactId> <version>1.0.0</version> <name>Withinet-PaaS</name> <description>Develop your web applications in on our infrastructure and we will worry about administration and scalability of your app.</description> <properties> <java.version>1.7</java.version> <guava.version>16.0.1</guava.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>com.withinet.cloudapp</groupId> <artifactId>slave</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.apache.wicket</groupId> <artifactId>wicket-core</artifactId> <version>6.15.0</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.0.Final</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Hibernate validator --> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator-annotation-processor</artifactId> <version>4.1.0.Final</version> </dependency> <!-- Guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>${guava.version}</version> </dependency> <!-- Java EE --> <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> <!-- Search --> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>4.8.0</version> </dependency> <!-- Security <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <!-- Spring Boot Maven --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.withinet.cloud.Application</mainClass> <layout>JAR</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project> 

将排除项添加到spring-boot-starter和spring-boot-starter-web以解决冲突。

 <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-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> 

在Gradle中添加更好,更通用的解决scheme(所有实例都将被排除):

 configurations { all*.exclude module : 'spring-boot-starter-logging' } 

https://docs.gradle.org/current/userguide/dependency_management.html

在gradle中添加解决scheme。

 dependencies { compile ('org.springframework.boot:spring-boot-starter') { exclude module : 'spring-boot-starter-logging' } compile ('org.springframework.boot:spring-boot-starter-web') { exclude module : 'spring-boot-starter-logging' } } 

我喜欢这个解决我的问题

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> 

如果您说出您的首选logging器是什么,以及您尝试安装它,可能会有所帮助。 无论如何,Spring Boot会尝试使用类path中的任何东西,所以如果你不需要logback,就把它从classpath中取出来。 文档中有log4j的说明,但同样的事情将适用于其他支持的日志logging系统(任何slf4j,log4j或Java util)。

我发现排除完整的spring-boot-starter-logging模块是不必要的。 所有需要的是排除org.slf4j:slf4j-log4j12模块。

将此添加到Gradle构build文件将解决此问题:

 configurations { runtime.exclude group: "org.slf4j", module: "slf4j-log4j12" compile.exclude group: "org.slf4j", module: "slf4j-log4j12" } 

有关更多详细信息,请参阅其他StackOverflow 答案 。

添加这个在你的build.gradle

 configurations.all { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat' exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' exclude group: 'org.springframework.boot', module: 'logback-classic' } 

对于gradle,

你可以在http://www.idanfridman.com/how-to-exclude-libraries-from-dependcies-using-gradle/看到这个解决scheme。;

只需要在configurations添加exclude

 configurations { providedRuntime compile.exclude(group: 'ch.qos.logback') } 

我通过以下解决了我的问题:

 compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.0'){ exclude module: 'log4j-slf4j-impl' exclude module: 'logback-classic' } compile('org.springframework.boot:spring-boot-starter-web'){ exclude module: 'log4j-slf4j-impl' exclude module: 'logback-classic' } 

只需在你的类path中添加logback.xmlconfiguration,并添加所有configuration和根appender添加。 一旦Spring引导完成了bean加载,它将根据你的configuration开始logging。