简体   繁体   English

如何强制使用 log4j 但不使用 slf4j

[英]How to force log4j to be used but not slf4j

EDIT: To clarify the question, I added many explanations.编辑:为了澄清这个问题,我添加了许多解释。

In my project dependencies, I have both slf4j and log4j .在我的项目依赖项中,我同时拥有slf4jlog4j This can't be changed for some technical reason.由于某些技术原因,这无法更改。

>gradlew dependencies
Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could 
not be reused, use --status for details
> Task :dependencies
.
.
|    |         |    +--- org.springframework.boot:spring-boot-starter-logging:2.5.7
|    |         |    |    +--- ch.qos.logback:logback-classic:1.2.7
|    |         |    |    |    +--- ch.qos.logback:logback-core:1.2.7
|    |         |    |    |    \--- org.slf4j:slf4j-api:1.7.32
|    |         |    |    +--- org.apache.logging.log4j:log4j-to-slf4j:2.14.1
|    |         |    |    |    +--- org.slf4j:slf4j-api:1.7.25 -> 1.7.32
|    |         |    |    |    \--- org.apache.logging.log4j:log4j-api:2.14.1 -> 2.17.0
|    |         |    |    \--- org.slf4j:jul-to-slf4j:1.7.32
|    |         |    |         \--- org.slf4j:slf4j-api:1.7.32
.
.
+--- org.apache.logging.log4j:log4j-core:2.17.0
|    \--- org.apache.logging.log4j:log4j-api:2.17.0
+--- org.apache.logging.log4j:log4j-api:2.17.0
BUILD SUCCESSFUL in 16s
1 actionable task: 1 executed

I want to set the log level in the code , and AFAIK that can only be done with log4j .我想在代码中设置日志级别,以及只能使用log4j完成的 AFAIK。 As a result, I simply look for a way to make this code line ( LogManager is part of log4j ) returns log4j implementation and not slf4j implementation:结果,我只是寻找一种方法来制作此代码行( LogManagerlog4j的一部分)返回log4j实现而不是slf4j实现:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
.
.
private final Logger logger = LogManager.getLogger(getClass());

Sadly, the logger's class returns is org.apache.logging.slf4j.SLF4JLogger :可悲的是,记录器的 class 返回是org.apache.logging.slf4j.SLF4JLogger

System.out.println("logger class is: [" + logger.getClass() + "]");

output: output:

logger class is: [class org.apache.logging.slf4j.SLF4JLogger]

Even though it sounds easy, I haven't been able to do it or find an example online.尽管听起来很简单,但我无法做到这一点,也无法在网上找到示例。 What can be done?可以做什么?

Whats the full class path of LogManager? LogManager 的完整 class 路径是什么? It has to be imported, there you should see the full class.它必须被导入,在那里你应该看到完整的 class。

My guess is that you need to change the Import to import some log4j LogManager, not the slf4j one.我的猜测是您需要更改 Import 以导入一些 log4j LogManager,而不是 slf4j。

Log4j 2.x Core and the Log4j to SLF4J Adapter are two implementations of the Log4j 2.x API. Log4j 2.x Core and the Log4j to SLF4J Adapter are two implementations of the Log4j 2.x API. If they are both present on the classpath log4j-to-slf4j is used.如果它们都存在于类路径中,则使用log4j-to-slf4j That is what happens in your case: messages logged using the Log4j 2.x API are sent to SLF4J, which sends them to Logback.这就是您的情况:使用 Log4j 2.x API 记录的消息被发送到 SLF4J,后者将它们发送到 Logback。

That is the standard logging configuration brought by the spring-boot-starter-logging .也就是spring-boot-starter-logging带来的标准日志配置。 If you want to use Log4j 2 as backend instead you need to exclude that artifact and add spring-boot-starter-log4j2 .如果您想使用 Log4j 2 作为后端,则需要排除该工件并添加spring-boot-starter-log4j2

Anyway, since you are using Spring Boot, you have two ways to set the level of a logger:无论如何,由于您使用的是 Spring 引导,因此您有两种方法可以设置记录器的级别:

  1. Using Spring Boot's LoggingSystem abstraction (works for all available logging systems):使用 Spring Boot 的LoggingSystem抽象(适用于所有可用的日志系统):

     final LoggingSystem loggingSystem = LoggingSystem.get(getClass().getClassLoader()); loggingSystem.setLogLevel("loggerName", LogLevel.DEBUG);
  2. Using the underlying logging system.使用底层日志系统。 In your case it is Logback, so you should use:在您的情况下,它是 Logback,因此您应该使用:

     final Logger logger = LoggerFactory.getLogger("loggerName"); if (logger instanceof ch.qos.logback.classic.Logger) { ((ch.qos.logback.classic.Logger) logger).setLevel(Level.DEBUG); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM