Java LoggerFactor.getLogger 无法解析为类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22100973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LoggerFactor.getLogger cannot be resolved to a type
提问by smuggledPancakes
I setup a basic Java program, I am following this tutorialand have this exact code:
我设置了一个基本的 Java 程序,我正在关注本教程并拥有以下确切代码:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.info("Hello World");
}
}
I have the jars slf4j-api-1.7.5.jar and slf4j-log4j12-1.7.5 jar on my build path. I do not understand what gives, the getLogger method exists in the LoggerFactory class which I can F3 (source code look-up) to. I Googled about this and appear to be the only dope with this problem. Any ideas?
我的构建路径上有 jars slf4j-api-1.7.5.jar 和 slf4j-log4j12-1.7.5 jar。我不明白是什么给出的,getLogger 方法存在于 LoggerFactory 类中,我可以 F3(源代码查找)到它。我对此进行了 Google 搜索,似乎是唯一解决此问题的方法。有任何想法吗?
Here is my .classpath for Eclipse:
这是我用于 Eclipse 的 .classpath:
<xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdit..../>
<classpathentry kind="lib" path="/home/Desktop/slf4j-api-1.7.5.jar" sourcepath="/home/Desktop/slf4j-api-1.7.5.jar"/>
<classpathentry kind="lib" path="slf4j-log4j12-1.7.5.jar"/>
<classpathentry kind="lib" path="log4j-1.2.17.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
采纳答案by David Hodgson
On the tutorial page you link to, there is the following note:
在您链接到的教程页面上,有以下说明:
slf4j-log4j12-1.7.6.jar
Binding for log4j version 1.2, a widely used logging framework. You also need to place log4j.jar on your class path.
slf4j-log4j12-1.7.6.jar
绑定 log4j 1.2 版,这是一个广泛使用的日志框架。您还需要将 log4j.jar 放在您的类路径上。
Did you include log4j.jar?
你包括 log4j.jar 吗?
回答by Alan CN
My problem was solved after the inclusion of
我的问题在包含后解决了
slf4j-api-1.7.7.jarand slf4j-simple-1.7.7.jar
slf4j-api-1.7.7.jar和 slf4j-simple-1.7.7.jar
on classpath.
在类路径上。
回答by ephemeralCoder
You can always use the logger statically instead of using loggerfactory and creating an instance every time you need it:
您始终可以静态使用记录器,而不是使用 loggerfactory 并在每次需要时创建一个实例:
final static Logger logger = Logger.getLogger(HelloWorld.class);
According to this SO answer, there does not seem to be much overhead of using either way:
根据this SO answer,使用任何一种方式似乎都没有太多开销:
What's the overhead of creating a SLF4J loggers in static vs. non-static contexts?
回答by yaghob abbasi
I solved it with
我解决了
import org.apache.log4j.Logger;
instead of
代替
import java.util.logging.Logger;
回答by Thanuja
For me, when I added the Maven dependency below, it worked:
对我来说,当我在下面添加 Maven 依赖项时,它起作用了:
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>