Alo*_*lok 11 java eclipse log4j2
我试图实现log4j 2但它不断抛出以下错误.
> ERROR StatusLogger Log4j2 could not find a logging implementation.
> Please add log4j-core to the classpath. Using SimpleLogger to log to
> the console...
> ERROR LogExample This Will Be Printed On Error
> FATAL LogExample This Will Be Printed On Fatal
Run Code Online (Sandbox Code Playgroud)
我已经尝试过在网上给出的解决方案.但似乎并不适合我.
这是我试图运行的代码.
package demo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class LogExample {
private static final Logger LOG = LogManager.getLogger(LogExample.class);
public static void main(String[] args) {
LOG.debug("This Will Be Printed On Debug");
LOG.info("This Will Be Printed On Info");
LOG.warn("This Will Be Printed On Warn");
LOG.error("This Will Be Printed On Error");
LOG.fatal("This Will Be Printed On Fatal");
LOG.info("Appending string: {}.", "Hello, World");
}
}
Run Code Online (Sandbox Code Playgroud)
在pom.xml中添加了项目和依赖项:
任何帮助表示赞赏.谢谢.
小智 8
如果您已经将 log4j-core 添加到 Maven pom,那么您不需要任何其他内容,它应该可以工作。问题可能出在您的 IDE 上,它看不到您的 Maven 依赖项。尝试重新加载或重新导入 Maven 依赖项,或者重新启动 IDE 也会有所帮助。
通过设置 log4j2 配置文件的系统属性解决了错误信息。下面是下面的代码。
System.setProperty("log4j.configurationFile","./path_to_the_log4j2_config_file/log4j2.xml");
Logger log = LogManager.getLogger(LogExample.class.getName());
Run Code Online (Sandbox Code Playgroud)
这种依赖有助于避免 lambda 的这个错误。
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.8.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在 Mac 上的 intellij 中,由于某种原因,log4j 依赖项未添加到类路径中。所以我所做的是
将以下内容添加到pom.xml文件后
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
log4j zip file我手动从https://www.apache.org/dyn/closer.lua/logging/log4j/2.20.0/apache-log4j-2.20.0-bin.zip下载
解压文件后,我将其添加log4j-core到log4j-api项目路径中,如下所示
为了解决 Spring Boot 的类似问题,我添加了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
到我的 POM
| 归档时间: |
|
| 查看次数: |
33568 次 |
| 最近记录: |