【发布时间】:2014-10-30 05:59:39
【问题描述】:
我正在使用spring-boot-starter,并想将log4j2.xml 配置为将异步+不同的内容记录到不同的日志文件中。
我创建了 log4j2 文件,但 Spring 仍然使用 spring-boot 默认日志记录。如何切换日志记录?
【问题讨论】:
标签: java spring log4j spring-boot log4j2
我正在使用spring-boot-starter,并想将log4j2.xml 配置为将异步+不同的内容记录到不同的日志文件中。
我创建了 log4j2 文件,但 Spring 仍然使用 spring-boot 默认日志记录。如何切换日志记录?
【问题讨论】:
标签: java spring log4j spring-boot log4j2
我有更好的办法:
排除 logback 记录器:
<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>
享受吧!
【讨论】:
configurations.all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging',然后在我的依赖项{}部分我添加了runtime 'org.springframework.boot:spring-boot-starter-log4j2'
这对我有用。有两个额外的排除。否则应用程序没有获取 log4j 并且发生冲突
实际绑定的类型是 [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<exclusion>
<artifactId>log4j-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
【讨论】:
试试这个:
排除 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>
为您的日志接口添加依赖项,例如slf4j
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.0.2</version>
</dependency>
添加指向所选日志接口的其他日志实现,例如
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</dependency>
添加您的目标日志记录实现,例如
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0.2</version>
</dependency>
它应该可以工作。
【讨论】:
spring-boot-starter-log4j2,因为 spring-boot-1.2.0.RELEASE。仅供参考。