简单java maven项目中的log4j

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19919200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:22:13  来源:igfitidea点击:

log4j in simple java maven project

javaeclipsemavenlog4j

提问by riship89

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

我有一个简单的java项目。我使用 maven 管理我的依赖项。我在 pom.xml 中添加了 log4j 依赖项

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.

我在资源文件夹中有我的 log4j.properties 文件。

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

当我将 App 作为 Java 应用程序运行时,出现以下错误。

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.

所以,我的问题是如何让运行时知道我的 log4j.properties 文件在资源文件夹中。

EDIT

编辑

Here is my pom.xml

这是我的 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

这是我的 log4j.properties 文件

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

采纳答案by riship89

I modified my App class as follows:

我修改了我的 App 类如下:

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!");
    }
}

回答by Satya

change your pom.xml dependency for log4j to this:

将 log4j 的 pom.xml 依赖项更改为:

<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

看看它是否有效

回答by Hilikus

Set the system property log4j.configurationto 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/resourcesmaven will move it to the root)

将系统属性设置log4j.configuration为文件的运行时路径。这可以在应用程序启动器中完成。像这样,假设您的配置文件在您的工作目录的根目录中(因为您说您的文件在src/main/resources 中,maven 会将其移动到根目录)

-Dlog4j.configuration=log4j.properties

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

但我不得不说,如果你的文件真的被称为 log4j.properties 并且它真的在src/main/resources 中,你不应该需要它,因为默认情况下 log4j 会在那里寻找它

回答by Sam

You should place log4j.properties under src/main/resourcesfolder of your projects to avoid the warning or provide the file path using -Dlog4j.configuration=<FILE_PATH>

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