53

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].
4

5 回答 5

83

如果你想使用slf4j simple,你的类路径中需要这些jar文件:

  • slf4j-api-1.6.1.jar
  • slf4j-simple-1.6.1.jar

如果你想使用slf4jand log4j,你的类路径中需要这些jar文件:

  • slf4j-api-1.6.1.jar
  • slf4j-log4j12-1.6.1.jar
  • log4j-1.2.16.jar

不多也不少。使用slf4j simple,您将在INFO级别或更高级别将基本日志记录到您的控制台。使用log4j时,您必须进行相应的配置

于 2010-11-30T06:40:42.250 回答
54

下面是一个例子。您可以查看详细信息http://jkssweetlife.com/configure-slf4j-working-various-logging-frameworks/并在此处下载完整代码。

  • 如果您使用 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);
        }
    }
    
于 2014-07-09T01:27:23.213 回答
4
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
于 2016-02-22T06:59:35.753 回答
2

这是一个在后端使用 slf4j 作为外观和 log4j 的工作示例:

pom.xml

<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/resources/log4j.properties

# 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

src/main/java/Main.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    /**
     * Default private constructor.
     */
    private Main() {
    }

    /**
     * Main method.
     *
     * @param args Arguments passed to the execution of the application
     */
    public static void main(final String[] args) {
        logger.info("Message to log");
    }
}
于 2020-08-12T09:24:53.417 回答
0

我有同样的问题。直接使用 log4j api 时,我在 log4j.properties 文件中从代码中调用了我自己的自定义记录器。如果您正在使用 slf4j api 调用,您可能正在使用默认的根记录器,因此您必须将其配置为与 log4j.properties 中的附加程序相关联:

    # 将 root logger 级别设置为 DEBUG,并将其唯一的 appender 设置为 A1。
    log4j.rootLogger=调试,A1

    # A1 设置为 ConsoleAppender。
    log4j.appender.A1=org.apache.log4j.ConsoleAppender

于 2014-03-02T22:27:49.577 回答