Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move log4j.properties outside jar file?

Tags:

java

gradle

log4j

I build a jar file with gradle, my app uses log4j. Initially my log4j.properties was embedded in my jar file. 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:

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:

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:

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:

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?

like image 308
m-ric Avatar asked Apr 29 '15 16:04

m-ric


People also ask

Where do I put log4j properties file?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file. The level of the root logger is defined as INFO and attaches the ROLLINGFILE appender to it.

Can we use log4j without properties file?

println() over Log4j, of course for testing purposes, because it doesn't require any configuration, you can just use it, without bothering about XML or properties file configuration, but the most programmer will agree that they would prefer to use Log4j over println statements, even for test programs if it's easy to ...

Where is log4j properties file located in Tomcat?

Log4j and Tomcat Libraries are placed in common library directory. The configuration file for Tomcat is in common/classes directory, the configuration file for a application is placed in the WEB-INF/classes folder of the application.


1 Answers

Try this:

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

but i'm curious: does the Class-Path definition in the manifest really work? Jars, yes, that's standard, but does it work for log4j.properties, too?

like image 55
wallenborn Avatar answered Sep 19 '22 15:09

wallenborn