如何获得SLF4J“Hello World"使用 log4j? [英] How to get SLF4J "Hello World" working with log4j?

查看:36
本文介绍了如何获得SLF4J“Hello World"使用 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

如果你想使用 slf4jlog4j,你的类路径中需要这些 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆