Forums Register Login

log4j.properties

+Pie Number of slices to send: Send
I've built an executable jar that contains the log4j.properties file. When I execute the .jar, it cannot find the .properties file. I've tried many different variations on the path. What does work is hardcoding the path from the root drive. C:/blah/blah/log4j.properties.
I know this is probably a really dumb question but is there not a way to reference the log4j.properties that's inside the executable?





+Pie Number of slices to send: Send
How are you trying to access it?

And why, since if it's on the classpath Log4J will use it automatically?
+Pie Number of slices to send: Send
 

David Newton wrote:How are you trying to access it?

And why, since if it's on the classpath Log4J will use it automatically?



I have a jar I run from the prompt.
I'm trying to access the log4j.properties file that's inside of the jar. But I've not been able to pass the correct path to the PropertyConfigurator.
The path to the properties file is myJar-with-dependencies.jar/META-INF/log4j.properties.
I just looked at my MANIFEST.MF file and it's not on the class path. I guess this is why it can't find my properties file? I'm not entirely sure how to go about that as I'm using Maven for this project and I'm learning more about Maven as I go. I can pass a hard coded path to a log4j.properties file that lives outside the jar and the PropertyConfigurator will find it no problem. But I thought it would be cleaner If I could access the log4j.properties inside of the jar.

suggestions?
+Pie Number of slices to send: Send
I guess my question is still "why are you trying to access the Log4J config file programatically"? Do you need to?
+Pie Number of slices to send: Send
I don't understand where your going with your question. What other alternative is there?
+Pie Number of slices to send: Send
Log4J picks up config files on the classpath automatically:

I wrote:And why, since if it's on the classpath Log4J will use it automatically?

 
+Pie Number of slices to send: Send
So I don't have to use the propertyConfigurator? Is that what you are saying?
+Pie Number of slices to send: Send
Goodness. I don't know what you're doing with the config file, which is why I was asking what you were doing with the config file and why you needed it. Log4J will pick up configuration files on the classpath automatically.
+Pie Number of slices to send: Send
 

David Newton wrote:Goodness. I don't know what you're doing with the config file, which is why I was asking what you were doing with the config file and why you needed it. Log4J will pick up configuration files on the classpath automatically.



Well, the config file specifies my rolling appenders etc and also the the log file. I thought, and maybe erroneously on my part, the proper way to do that was to use
PropertyConfigurator.configure("META-INF/log4j.properties");

Is that the wrong approach to setting up log4j?

I don't know if this helps or not but here's the log4j.properties file contents.

log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=logs/SolrIndex.log
log4j.appender.rollingFile.MaxFileSize=2MB
log4j.appender.rollingFile.MaxBackupIndex=2
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.conversionPattern=%p %d %c – %m%n
log4j.rootLogger = DEBUG, rollingFile

I mean, the file exists inside of my jar. I just really want to know how to pick it up from my jar as opposed to the file not only being in my jar, but then me having to hand move it to where the thing is going to be deployed and then specifying something like this:
PropertyConfigurator.configure("C:/workspace/solr-maven/src/main/resources/META-INF/log4j.properties")
to be able to pick up the log4j file.

Kevin.
+Pie Number of slices to send: Send
Is it "wrong"? Of course not, there may well be reasons why someone would do it like that. I essentially never do, I just put it on my classpath, because Log4J will pick up configuration files on the classpath automatically.
These are the worst of times and these are the best of times. And this is the best tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3352 times.
Similar Threads
JVM: Could not find the main class. Program will exit
Jar files and manifest classpath
How classpath works for executable Jars
Running jar from anywhere
jar mainifest: Main-Class and Class-Path incompatible?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:37:34.