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

huangapple go评论73阅读模式
英文:

log4j.properties file not found in project

问题

以下是翻译好的内容:

我在我的项目中使用Log4j,试图运行我的应用程序,但似乎找不到log4.properties文件。

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

这是我在代码中加载log4j.properties文件的方式。

PropertyConfigurator.configure("log4j.properties");

当我尝试运行应用程序时,我会收到以下错误:

log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (No such file or directory)

奇怪的是,如果我使用 mvn package 打包我的应用程序并打开jar文件,我可以看到log4j.properties文件在其中。我不明白出了什么问题,因为我在pom文件中声明了资源文件夹。

编辑: 我正在添加项目中用于log4j的依赖项

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

log4j.properties文件位于src/main/resources文件夹中。

英文:

I am using Log4j in my project and trying to run my application but it seems it cannot find log4.properties file.

This is my Maven configuration:

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

And this is how I am loading the log4j.properties file in my code.

PropertyConfigurator.configure(&quot;log4j.properties&quot;);

When I try to run the application I get the following error:

> log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (No such file or directory)

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. I don't understand what is wrong since I declare the resources folder in the pom file.

EDIT: I am adding the dependency I use for log4j in the project

   &lt;dependency&gt;
        &lt;groupId&gt;log4j&lt;/groupId&gt;
        &lt;artifactId&gt;log4j&lt;/artifactId&gt;
        &lt;version&gt;1.2.17&lt;/version&gt;
    &lt;/dependency&gt;

log4j.properties file is inside the src/main/resources folder

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

答案1

得分: 0

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

英文:

If you are using log4j 2.X, the configuration is different.

答案2

得分: 0

这一行实际上是导致错误的原因:

PropertyConfigurator.configure("log4j.properties");

显然,只要在maven中设置了<directory>/src/main/resource</directory>,并且log4j.properties文件存在于资源文件夹中,我就可以直接使用Logger,而不需要这一行。

英文:

It turns out that this line was causing the error

PropertyConfigurator.configure(&quot;log4j.properties&quot;);

Apparently as long as the &lt;directory&gt;/src/main/resource&lt;/directory&gt; is set in maven and the file log4j.properties file exists inside the resources folder I can use the Logger directly without this line

huangapple
  • 本文由 发表于 2020年10月4日 19:03:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64193814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定