如何在Eclipse Maven中使用Log4j2?

时间:2018-05-16 18:26:00

标签: java maven log4j2

我用maven-archetype-quickstart开始了一个新的maven项目。我在版本中使用Eclipse Java EE IDE for Web Developers:Oxygen.3a Release(4.7.3a),在Ubuntu 17.10上构建id:20180405-1200。

我的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.rogatio</groupId>
  <artifactId>helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>helloworld</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.11.0</version>
    </dependency>
    <dependency>
       <groupId>org.apache.logging.log4j</groupId>
       <artifactId>log4j-core</artifactId>
       <version>2.11.0</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-surefire-plugin</artifactId>
           <version>2.17</version>
           <configuration>
        <systemProperties>
          <property>
            <name>log4j.configurationFile</name>
            <value>log4j2.properties</value>
          </property>
        </systemProperties>
      </configuration>
    </plugin>
  </plugins>

Main-Class是     包org.rogatio.helloworld;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class App {
final static Logger logger = LogManager.getLogger(App.class);

public static void main(String[] args) {

    logger.info("App started.");

    System.out.println("Hello World!");

    sayHi();

}

public static String sayHi() {
    logger.info("Hi!");
    System.out.println("Hi!");
    return "Hi!";
}
}

我的项目结构是

Filestructure

测试类是

package org.rogatio.helloworld;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class AppTest 
extends TestCase
{
/**
 * Create the test case
 *
 * @param testName name of the test case
 */
public AppTest( String testName )
{
    super( testName );
}

/**
 * @return the suite of tests being tested
 */
public static Test suite()
{
    return new TestSuite( AppTest.class );
}

public void testApp()
{
    assertTrue( true );
}

public void testHi()
{
    assertEquals( App.sayHi(), "Hi!" );
}
}

问题是运行Maven-Test会抛出

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.rogatio.helloworld.AppTest
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Hi!
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.233 sec - in org.rogatio.helloworld.AppTest

同时运行App会抛出

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Hello World!
Hi!

我没有弄错。一切都已设置,资源在类路径中。有什么想法错了吗?我的POM错了吗?

您可以在https://ufile.io/8zy0a

下找到完整的项目

0 个答案:

没有答案