我正在尝试在 Eclipse 项目中使用 log4j.2.x 进行日志记录。我的名为 log4j2.xml 的 conf 文件直接位于 java 项目下,并且我在类路径中有必要的 jar。当我测试时,我看到以下错误消息。谁能告诉我这个问题的解决方案?log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?> <configuration status="WARN"> <appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console> </appenders> <loggers>
<root level="error">
<appender-ref ref="Console"/>
</root> </loggers></configuration>
控制台中的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/ILoggerFactory
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at org.apache.logging.slf4j.SLF4JLoggerContextFactory.<init>(SLF4JLoggerContextFactory.java:34)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at org.apache.logging.log4j.LogManager.<clinit>(LogManager.java:84)
at Browserbot.<clinit>(Browserbot.java:17)Caused by: java.lang.ClassNotFoundException: org.slf4j.ILoggerFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356) ... 23 more
我的 Java 文件:
public class Browserbot {
static Logger logger = LogManager.getLogger(Browserbot.class.getName());
public static void main(String[] args){
logger.error("File Not Found");}}