Java 如何让 SLF4J“Hello World”与 log4j 一起工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4311026/
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
How to get SLF4J "Hello World" working with log4j?
提问by ripper234
The "Hello World" example from SLF4Jis 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?
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.
Update: I added log4j initialization, and it still doesn't work:
更新:我添加了 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");
}
And I'm getting:
我得到:
log4j:ERROR No output stream or file set for the appender named [null].
采纳答案by darioo
If you want to use slf4j simple
, you need these jar
files on your classpath:
如果你想使用slf4j simple
,你jar
的类路径中需要这些文件:
- slf4j-api-1.6.1.jar
- slf4j-simple-1.6.1.jar
- 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
and log4j
,你jar
的类路径中需要这些文件:
- slf4j-api-1.6.1.jar
- slf4j-log4j12-1.6.1.jar
- log4j-1.2.16.jar
- 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 simple
,您将在INFO
级别或更高级别获得对控制台的基本日志记录。使用时log4j
,您必须相应地对其进行配置。
回答by Lund Wolfe
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:
我有同样的问题。直接使用 log4j api 时,我从代码中的 log4j.properties 文件中调用了我自己的自定义记录器。如果您正在使用 slf4j api 调用,您可能正在使用默认的根记录器,因此您必须将其配置为与 log4j.properties 中的附加程序相关联:
# Set root logger level to DEBUG and its only appender to A1. log4j.rootLogger=DEBUG, A1 # A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender
回答by ylu
Following is an example. You can see the details http://jkssweetlife.com/configure-slf4j-working-various-logging-frameworks/and download the full codes here.
下面是一个例子。您可以在http://jkssweetlife.com/configure-slf4j-working-various-logging-frameworks/查看详细信息并在此处下载完整代码。
Add following dependency to your pom if you are using maven, otherwise, just download the jar files and put on your classpath
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency>
Configure log4j.properties
log4j.rootLogger=TRACE, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd'T'HH:mm:ss.SSS} %-5p [%c] - %m%n
Java example
public class Slf4jExample { public static void main(String[] args) { Logger logger = LoggerFactory.getLogger(Slf4jExample.class); final String message = "Hello logging!"; logger.trace(message); logger.debug(message); logger.info(message); logger.warn(message); logger.error(message); } }
如果您使用的是 maven,请将以下依赖项添加到您的 pom 中,否则,只需下载 jar 文件并放在您的类路径中
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.7</version> </dependency>
配置 log4j.properties
log4j.rootLogger=TRACE, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd'T'HH:mm:ss.SSS} %-5p [%c] - %m%n
Java 示例
public class Slf4jExample { public static void main(String[] args) { Logger logger = LoggerFactory.getLogger(Slf4jExample.class); final String message = "Hello logging!"; logger.trace(message); logger.debug(message); logger.info(message); logger.warn(message); logger.error(message); } }
回答by Shantonu
you need to add 3 dependency ( API+ API implementation + log4j dependency)
Add also this
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
# And to see log in command line , set log4j.properties
# Root logger option
log4j.rootLogger=INFO, file, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
#And to see log in file , set log4j.properties
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./logs/logging.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n