简体   繁体   English

如何在jar文件外移动log4j.properties?

[英]How to move log4j.properties outside jar file?

I build a jar file with gradle, my app uses log4j . 我用gradle构建一个jar文件,我的应用程序使用log4j Initially my log4j.properties was embedded in my jar file. 最初我的log4j.properties嵌入在我的jar文件中。 I moved it outside to be able to modify it in production. 我把它移到外面是为了能够在生产中修改它。 I updated the META-INF/MANIFEST.MF file embedded in my jar file to reflect this: 我更新了jar文件中嵌入的META-INF/MANIFEST.MF文件,以反映这一点:

Class-Path: lib/log4j-1.2.1 lib/slf4j-api-1.4.2.jar
            lib/slf4j-api-1.5.6.jar lib/slf4j-log4j12-1.5.6.jar
            lib/log4j-1.2.14.jar
            ...
            lib/log4j.properties

My filetree is as follows on a windows7 32bit machine: 我的filetree在windows7 32bit机器上如下:

root/myapp.jar
root/lib/log4j.properties

I start my app with this command: 我用这个命令启动我的应用程序:

java -Dlog4j.configuration='lib\log4j.properties' -Dlog4j.debug=true -jar myapp.jar > logs/output.log

my logs/output.log says that log4j could not find its property file: 我的logs/output.log说log4j找不到它的属性文件:

log4j: Trying to find ['lib\log4j.properties'] using context classloader sun.misc.Launcher$AppClassLoader@17fa65e.
log4j: Trying to find ['lib\log4j.properties'] using sun.misc.Launcher$AppClassLoader@17fa65e class loader.
log4j: Trying to find ['lib\log4j.properties'] using ClassLoader.getSystemResource().
log4j: Could not find resource: ['lib\log4j.properties'].

I also tried with: 我也尝试过:

-Dlog4j.configuration='lib/log4j.properties'
-Dlog4j.configuration='log4j.properties'
-Dlog4j.configuration=log4j.properties

When I embed the file in the jar, it works: 当我将文件嵌入jar中时,它可以工作:

log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@18385e3.
log4j: Using URL [jar:file:/C:/Users/User/Desktop/myapp/myapp.jar!/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL jar:file:/C:/Users/User/Desktop/myapp/myapp-2.1.9.jar!/log4j.properties
log4j: Parsing for [root] with value=[debug, A].

But I need it outside... Any idea? 但我需要在外面......任何想法?

Try this: 尝试这个:

-Dlog4j.configuration=file:lib/log4j.properties

but i'm curious: does the Class-Path definition in the manifest really work? 但我很好奇:清单中的Class-Path定义是否真的有效? Jars, yes, that's standard, but does it work for log4j.properties, too? 罐子,是的,这是标准的,但它也适用于log4j.properties吗?

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

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