aspose file tools*
The moose likes Other Open Source Projects and the fly likes log4j.properties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "log4j.properties" Watch "log4j.properties" New topic
Author

log4j.properties

Kevin Eddy
Ranch Hand

Joined: Feb 24, 2010
Posts: 74
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?





David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

How are you trying to access it?

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

Joined: Feb 24, 2010
Posts: 74
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?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I guess my question is still "why are you trying to access the Log4J config file programatically"? Do you need to?
Kevin Eddy
Ranch Hand

Joined: Feb 24, 2010
Posts: 74
I don't understand where your going with your question. What other alternative is there?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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?
Kevin Eddy
Ranch Hand

Joined: Feb 24, 2010
Posts: 74
So I don't have to use the propertyConfigurator? Is that what you are saying?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Kevin Eddy
Ranch Hand

Joined: Feb 24, 2010
Posts: 74
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.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: log4j.properties