views:

24

answers:

1

Is there a way to pass data or setting to log4j before it loads and then use that property within the config file.

I was assuming there is a system properties I could use:

log4j.appender.R.File=/usr/local/pfs/logs/${ws.host}/log4j.log

Where ws.host is the property I want to use.

But how can I set that value?

Also, I am in a web environment. How can I know at what point to set the property setting before log4j loads.

A: 

The default log4j PropertiesConfigurator supports variable substitution.

So, you could pass system properties like this "-DmyProject.logFile="/temp/test.log" to your Java startup, and then in the properties files have "log4j.appender.R.File=${myProject.logFile}".

If working from a web environment, you might want to check out Spring's Log4jConfigListener. It uses a listener (Servlet API 2.4+) to initialization log4j ahead of other components. Even if not using Spring, you should be able to use the source as an example to easily create your own listener.

kaliatech