如何获得SLF4J“Hello World”使用log4j? [英] How to get SLF4J "Hello World" working with log4j?
问题描述
SLF4J 中的Hello World示例对我不起作用。我想这是因为我在我的类路径中添加了slf4j-log4。我应该直接配置log4j以使hello world工作吗?
The "Hello World" example from SLF4J is not working for me. I guess this is because I added slf4j-log4 to my classpath. Should I configure log4j directly for the hello world to work?
log4j:WARN No appenders could be found for logger (HelloWorld).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
更新:我添加了log4j初始化,它仍然无法正常工作:
Update: I added log4j initialization, and it still doesn't work:
public static void main(String[] params) {
org.apache.log4j.Logger.getRootLogger().addAppender(new ConsoleAppender());
Logger logger = org.slf4j.LoggerFactory.getLogger(TestBase.class);
logger.info("Hello World");
}
我收到了:
log4j:ERROR No output stream or file set for the appender named [null].
推荐答案
如果你想使用 slf4j简单
,您需要在类路径中使用这些 jar
文件:
If you want to use slf4j simple
, you need these jar
files on your classpath:
- slf4j-api-1.6.1.jar
- slf4j-simple-1.6.1.jar
如果你想使用 slf4j
和 log4j
,你需要这些 jar
类路径上的文件:
If you want to use slf4j
and log4j
, you need these jar
files on your classpath:
- slf4j-api-1.6.1.jar
- slf4j-log4j12-1.6.1.jar
- log4j-1.2.16.jar
不多也不少。使用 slf4j simple
,您将获得 INFO
级别或更高级别的基本日志记录。使用 log4j
,您必须相应地配置它。
No more, no less. Using slf4j simple
, you'll get basic logging to your console at INFO
level or higher. Using log4j
, you must configure it accordingly.
这篇关于如何获得SLF4J“Hello World”使用log4j?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!