来自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].
答案 0 :(得分:73)
如果您想使用slf4j simple
,则需要在类路径中使用这些jar
个文件:
如果您想使用slf4j
和log4j
,则需要在类路径中使用这些jar
个文件:
不多也不少。使用slf4j simple
,您将获得INFO
级或更高级别的基本日志记录。使用log4j
,您必须configure it accordingly。
答案 1 :(得分:42)
以下是一个例子。您可以查看详细信息http://jkssweetlife.com/configure-slf4j-working-various-logging-frameworks/并下载完整代码here。
如果您正在使用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);
}
}
答案 2 :(得分:3)
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
答案 3 :(得分:0)
我遇到了同样的问题。当直接使用log4j api时,我从log4j.properties文件中调用了我自己的自定义记录器。如果您正在使用slf4j api调用,那么您可能正在使用默认的根记录器,因此您必须将其配置为与log4j.properties中的appender关联:
# 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
答案 4 :(得分:0)
下面是一个使用slf4j作为幕墙,并在后端使用log4j的工作示例:
pom.xml
/products/${node.category_id}?diameter=15,25&length=100,200
src / main / resources / log4j.properties
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.3</version>
</dependency>
</dependencies>
</project>
src / main / java / Main.java
# Root logger option
log4j.rootLogger=DEBUG, stdout
# Direct log messages to 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