slf4j log4j你好世界问题

时间:2017-12-07 10:40:54

标签: java maven java-8 log4j slf4j

我试图找到我的错误的解决方案,但是,由于类似的问题被大量询问,结果的沼泽并没有帮助找到我的问题的答案。

我基本上想要一个简单的" Hello World"在slf4j中使用log4j工作,以便我以后可以进一步自定义它。

我的项目基本上是maven hello world项目,稍作调整:

App.java:

package xx.xxxx.logging;

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

public class App {

    private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

    public static void main(String[] args) {
        System.out.println("Hello World!");

        LOGGER.info("Hello Logger!");
    }
}

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

        <Appenders>
                <Console name="STDOUT" target="System.out">
                        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
                </Console>
        </Appenders>

        <Loggers>
                <Root level="ALL">
                        <AppenderRef ref="STDOUT"/>
                </Root>
        </Loggers>

</Configuration>

的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 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>xx.xxxx.logging</groupId>
  <artifactId>logging-test</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>

  <name>logging-test</name>

  <properties>
    <jdk.version>1.8</jdk.version>
    <java.version>1.8</java.version>

    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <sonar.java.source>${java.version}</sonar.java.source>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <version.sl4j>1.7.21</version.sl4j>
    <version.sl4j.log4j>2.0.1</version.sl4j.log4j>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${version.sl4j}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>${version.sl4j.log4j}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>${version.sl4j.log4j}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-slf4j-impl</artifactId>
      <version>${version.sl4j.log4j}</version>
    </dependency>
  </dependencies>

</project>

尝试运行App.java会产生以下错误:

2017-12-07 11:14:33,263 ERROR catching java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:133)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:744)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:683)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:675)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:349)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:150)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:364)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:422)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:146)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:75)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:37)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:263)
    at org.apache.logging.slf4j.Log4jLoggerFactory$PrivateManager.getContext(Log4jLoggerFactory.java:98)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:84)
    at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:44)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
    at xx.xxxx.logging.App.<clinit>(App.java:8)
Caused by: java.lang.IllegalArgumentException: No enum constant org.apache.logging.log4j.core.appender.ConsoleAppender.Target.System.out
    at java.lang.Enum.valueOf(Enum.java:238)
    at org.apache.logging.log4j.core.appender.ConsoleAppender$Target.valueOf(ConsoleAppender.java:57)
    at org.apache.logging.log4j.core.appender.ConsoleAppender.createAppender(ConsoleAppender.java:98)
    ... 22 more

2017-12-07 11:14:33,268 ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.ConsoleAppender for element Console.
2017-12-07 11:14:33,269 ERROR Null object returned for Console in Appenders.
2017-12-07 11:14:33,275 ERROR Unable to locate appender STDOUT for logger 
Hello World!

如何解决这个问题的任何线索都将非常感激。

2 个答案:

答案 0 :(得分:1)

target="System.out"显然不正确。它与ConsoleAppender.Target枚举有关。我假设您希望target="SYSTEM_OUT"https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/appender/ConsoleAppender.Target.html

或者只是忽略该属性,因为默认情况下它已经是SYSTEM_OUT

答案 1 :(得分:0)

尝试根据https://logging.apache.org/log4j/2.x/manual/configuration.html

遵循log4j语法
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
        <Appenders>
          <Console name="Console-Appender" target="SYSTEM_OUT">
             <PatternLayout>
               <pattern>
                   %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
               </pattern>
             </PatternLayout>
          </Console>
        </Appenders>

      <Loggers>
         <Root level="ALL">
             <AppenderRef ref="Console-Appender"/>
         </Root>
      </Loggers>
 </Configuration>