简体   繁体   English

在项目中找不到 log4j.properties 文件

[英]log4j.properties file not found in project

I am using Log4j in my project and trying to run my application but it seems it cannot find log4.properties file.我在我的项目中使用 Log4j 并尝试运行我的应用程序,但它似乎找不到 log4.properties 文件。

This is my Maven configuration:这是我的 Maven 配置:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>log4j.properties</include>
                <include>hibernate.cfg.xml</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <finalName>File-Name</finalName>
                <archive>
                    <manifest>
                        <mainClass>my.main.class</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>.</Class-Path>
                    </manifestEntries>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

And this is how I am loading the log4j.properties file in my code.这就是我在代码中加载 log4j.properties 文件的方式。

PropertyConfigurator.configure("log4j.properties");

When I try to run the application I get the following error:当我尝试运行该应用程序时,出现以下错误:

log4j:ERROR Could not read configuration file [log4j.properties]. log4j:ERROR 无法读取配置文件 [log4j.properties]。 java.io.FileNotFoundException: log4j.properties (No such file or directory) java.io.FileNotFoundException: log4j.properties(没有这样的文件或目录)

The weird thing is that if I package my application with mvn package and open the jar file I can see the log4j.properties file inside.奇怪的是,如果我用mvn package打包我的应用程序并打开 jar 文件,我可以看到里面的 log4j.properties 文件。 I don't understand what is wrong since I declare the resources folder in the pom file.我不明白出了什么问题,因为我在 pom 文件中声明了资源文件夹。

EDIT: I am adding the dependency I use for log4j in the project编辑:我在项目中添加我用于 log4j 的依赖项

   <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

log4j.properties file is inside the src/main/resources folder log4j.properties 文件位于 src/main/resources 文件夹中

在此处输入图片说明

如果您使用的是 log4j 2.X,则配置不同。

It turns out that this line was causing the error事实证明,这条线导致了错误

PropertyConfigurator.configure("log4j.properties");

Apparently as long as the <directory>/src/main/resource</directory> is set in maven and the file log4j.properties file exists inside the resources folder I can use the Logger directly without this line显然,只要<directory>/src/main/resource</directory>在 maven 中设置并且文件 log4j.properties 文件存在于资源文件夹中,我就可以直接使用 Logger 而无需这一行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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