简体   繁体   中英

log4j in simple java maven project

I have simple java project. I manage my dependancies using maven. I added log4j dependency in my pom.xml

Now I want to make use of

package com.abc.xyz;

import org.apache.log4j.Logger;

/**
 * Hello world!
 *
 */
public class App 
{
    static final Logger logger = Logger.getLogger(App.class);
    public static void main( String[] args )
    {
        logger.debug("Hello world!");
    }
}

I have my log4j.properties file in resources folder.

When I run App as a Java application I get following error.

log4j:WARN No appenders could be found for logger (com.abc.xyz.App).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

So, my question is how to make runtime know that my log4j.properties file is in resources folder.

EDIT

Here is my 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>com.abc.xyz</groupId>
  <artifactId>learning</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>learning</name>
  <url>http://maven.apache.org</url>
  <dependencies>
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
  <plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
            <source>1.6</source>
            <target>1.6</target>
        </configuration>
    </plugin>
 </plugins>
 </build>
</project>

and here is my log4j.properties file

# Define the root logger with appender file
log4j.rootLogger = DEBUG

Set the system property log4j.configuration to the runtime path of the file. This can be done in the application launcher. Something like this, assuming your configuration file is in the root of your working directory (since you said your file is in src/main/resources maven will move it to the root)

-Dlog4j.configuration=log4j.properties

But i have to say, if your file is really called log4j.properties and it really is in src/main/resources you should not need this since log4j will look for it there by default

I modified my App class as follows:

public class App 
{
    static final Logger logger = Logger.getLogger(App.class);
    public static void main( String[] args )
    {
        PropertyConfigurator.configure("/absolute/path/to/log4j.properties");
        logger.debug("Hello world!");
    }
}

您应该将log4j.properties放置在项目的src/main/resources文件夹下,以避免出现警告或使用-Dlog4j.configuration=<FILE_PATH>提供文件路径。

change your pom.xml dependency for log4j to this:

<dependencies>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0-beta9</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.0-beta9</version>
  </dependency>
</dependencies>

and see if it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM