如何获取 SLF4J“Hello World”使用 log4j?
SLF4J 中的“Hello World”示例不适合我。我想这是因为我将 slf4j-log4 添加到了我的类路径中。我应该直接配置 log4j 以使 hello world 正常工作吗?
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 初始化,但它仍然不起作用:
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].
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].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想使用 slf4j simple,您的类路径中需要这些
jar
文件:如果如果您想使用
slf4j
和log4j
,您的类路径中需要这些jar
文件:不多也不少。使用
slf4j simple
,您将在INFO
级别或更高级别获得控制台的基本日志记录。使用log4j
,您必须进行相应配置。If you want to use
slf4j simple
, you need thesejar
files on your classpath:If you want to use
slf4j
andlog4j
, you need thesejar
files on your classpath:No more, no less. Using
slf4j simple
, you'll get basic logging to your console atINFO
level or higher. Usinglog4j
, you must configure it accordingly.下面是一个例子。您可以查看详细信息 http://jkssweetlife.com/configure-slf4j-working -various-logging-frameworks/ 并在此处下载完整代码。
如果您使用的是 maven,请将以下依赖项添加到您的 pom,否则,只需下载 jar 文件并放在您的类路径中
配置 log4j.properties
Java 示例
Following is an example. You can see the details http://jkssweetlife.com/configure-slf4j-working-various-logging-frameworks/ and download the full codes here.
Add following dependency to your pom if you are using maven, otherwise, just download the jar files and put on your classpath
Configure log4j.properties
Java example
这是一个在后端使用 slf4j 作为外观和 log4j 的工作示例:
pom.xml
src/main/resources/log4j.properties
src/main/java/ Main.java
Here a working example to use slf4j as façade with log4j in the backend:
pom.xml
src/main/resources/log4j.properties
src/main/java/Main.java
我也有同样的问题。直接使用 log4j api 时,我从代码中在 log4j.properties 文件中调用了我自己的自定义记录器。如果您使用 slf4j api 调用,则可能使用默认的根记录器,因此您必须将其配置为与 log4j.properties 中的附加程序关联:
I had the same problem. I called my own custom logger in the log4j.properties file from code when using log4j api directly. If you are using the slf4j api calls, you are probably using the default root logger so you must configure that to be associated with an appender in the log4j.properties: