如何获得SLF4J“Hello World"使用 log4j? [英] How to get SLF4J "Hello World" working with log4j?
问题描述
SLF4J 中的Hello World"示例对我不起作用.我想这是因为我将 slf4j-log4 添加到我的类路径中.我应该直接配置 log4j 以便 hello world 工作吗?
log4j:WARN 找不到记录器 (HelloWorld) 的附加程序.log4j:WARN 请正确初始化 log4j 系统.log4j:WARN 有关更多信息,请参阅 http://logging.apache.org/log4j/1.2/faq.html#noconfig.
更新:我加了log4j初始化,还是不行:
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 没有为名为 [null] 的 appender 设置输出流或文件.
如果你想使用 slf4j simple
,你的类路径中需要这些 jar
文件:>
- slf4j-api-1.6.1.jar
- slf4j-simple-1.6.1.jar
如果你想使用 slf4j
和 log4j
,你的类路径中需要这些 jar
文件:
- slf4j-api-1.6.1.jar
- slf4j-log4j12-1.6.1.jar
- log4j-1.2.16.jar
不多也不少.使用 slf4j simple
,您将在 INFO
级别或更高级别获得对控制台的基本日志记录.使用 log4j
,您必须相应地配置.
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.
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");
}
And I'm getting:
log4j:ERROR No output stream or file set for the appender named [null].
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
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
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屋!