log4.properties 应该在类路径上吗?

发布于 2024-12-27 11:51:58 字数 1244 浏览 4 评论 0原文

我在将 log4j.properties 文件放在类路径上时遇到一些问题。我在开发 (Eclipse Indigo) 时可以使用它,但是当我将应用程序导出为 JAR 时,我不能使用它。

我为导出的 JAR 手工制作了 MANIFEST.MF 文件:

Manifest-Version: 1.0
Main-Class: main.Program
Class-Path: lib/log4j.properties lib/log4j-1.2.15.jar

然后将 JAR 放在此文件组织中:

folder
  |-------- app.jar
  |-------- lib
             |--------- log4j.properties
             |--------- log4j-1.2.15.jar

当我尝试运行 app.jar 时,它们找到 log4j.jar 但找不到 log4j.properties:

log4j:WARN No appenders could be found for logger (main.Program).
log4j:WARN Please initialize the log4j system properly.

我的 log4j.properties 文件是这样的:

log4j.rootLogger=INFO, stdout, file

PATTERN=[%d] [%p] [%c{1}]: %m%n

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=${PATTERN}

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.File=${logger_file_path}
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=${PATTERN}

I'm having some problems putting my log4j.properties file on classpath. I can use it when I'm developing (Eclipse Indigo) but, when I export my app as a JAR, I can't.

I've made by hand a MANIFEST.MF file for the exported JAR:

Manifest-Version: 1.0
Main-Class: main.Program
Class-Path: lib/log4j.properties lib/log4j-1.2.15.jar

And then with put the JAR on this file organization:

folder
  |-------- app.jar
  |-------- lib
             |--------- log4j.properties
             |--------- log4j-1.2.15.jar

When I try to run app.jar, they find log4j.jar but not log4j.properties:

log4j:WARN No appenders could be found for logger (main.Program).
log4j:WARN Please initialize the log4j system properly.

My log4j.properties file it's like this:

log4j.rootLogger=INFO, stdout, file

PATTERN=[%d] [%p] [%c{1}]: %m%n

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=${PATTERN}

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.File=${logger_file_path}
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=${PATTERN}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

沉睡月亮 2025-01-03 11:51:58

另一种方法可以做到这一点:
-Dlog4j.configuration=文件:“./log4j.properties”

one more way to do this:
-Dlog4j.configuration=file:"./log4j.properties"

孤君无依 2025-01-03 11:51:58

我知道的有三种方法

  1. log4j.properties 添加到 app.jar
  2. log4j.properties 放入“文件夹”(JAR 所在的位置)并更改类 - .lib/log4j-1.2.15.jar 的路径。
  3. 例如,将 log4j.properties 放入名为“conf”的文件夹,并将类路径更改为 ./conf/

我试过了,有效。

There are three ways l know.

  1. Add log4j.properties to app.jar
  2. Put the log4j.properties to the "folder" (where the JAR exists) and change the class-path to .lib/log4j-1.2.15.jar.
  3. Put the log4j.properties to the folder named "conf" for example, and change the class-path to ./conf/.

I have tried, it works.

青春有你 2025-01-03 11:51:58

不要将 log4j.properties 本身放在类路径中,而应将其放在包含该文件的目录中。

Class-Path: lib lib/log4j-1.2.15.jar

Do not put the log4j.properties itself in the classpath, but the directory that contains that file.

Class-Path: lib lib/log4j-1.2.15.jar
段念尘 2025-01-03 11:51:58

log4j.properties 添加到 app.jar

Add log4j.properties to app.jar.

人间☆小暴躁 2025-01-03 11:51:58

我尝试了Jesper的答案,一开始不起作用。然后我尝试了一下

Class-Path: lib/ lib/log4j-1.2.15.jar

,之后就成功了。

I tried the answer of Jesper, and at first it didn't work. Then I tried with

Class-Path: lib/ lib/log4j-1.2.15.jar

And it worked after that.

我不咬妳我踢妳 2025-01-03 11:51:58

尝试执行:

javar -jar -Dlog4j.configuration=file:"./log4j.properties" app.jar

try to execute:

javar -jar -Dlog4j.configuration=file:"./log4j.properties" app.jar

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文