Hello Ceki,
I had to tweak the code a bit but now it works like a charm. Thanks for getting
me on the right track. You made my day. I'll post my solution below for others.
This block sits in a servlet.
Best regards,
Richard
///snip/////////////////////////////////////////////////////////////
Properties props = new Properties();
props.put("REL_PATH", this.getServletContext().getRealPath("/logs"));
InputStream in = null;
try
{
in = this.getClass().getClassLoader()
.getResourceAsStream("log4j.properties");
if(in != null)
props.load(in);
else
log.error(this.getClass()+": Could not load log4j.properties.");
}
catch(IOException ioe)
{
log.error(this.getClass()+": IO Exception loading log4j.properties.
"+ioe.getMessage());
}
finally
{
if (in != null) try { in.close ();
}
catch (Throwable ignore) {}}
PropertyConfigurator.configure(props);
///snip/////////////////////////////////////////////////////////////////////
-----Original Message-----
From: Ceki Gülcü [mailto:[EMAIL PROTECTED]
Sent: Tuesday, August 23, 2005 10:24 AM
To: Log4J Users List
Subject: Re: Must use relative path for log file
From what you describe, it sounds like you have all the ingredients
already in place. :-)
Modify your existing servlet to configure log4j as follows:
Properties props = new Properties();
// add the REL_PATH property with value of relative path
props.put("REL_PATH", this.getRelativePath());
// merge props with log4j.properties
props.load("log4j.properties");
PropertyConfigurator.configure(props);
In your log4j.properties file use log4j's parameter substitution
facility:
log4j.rootLogger=DEBUG, MY_LOG
...
log4j.appender.MY_LOG=org.apache.log4j.FileAppender
log4j.appender.MY_LOG.file=${REL_PATH}/mylog.txt
...
I hope this helps but at least should point you at the right direction.
At 04:45 PM 8/23/2005, you wrote:
>Hello,
>
>I've spent several hours trying to figure this out with no luck. I need
>to be able read a log file for a web application. I don't have access to
>the files in production so I've written a servlet that allows me to set
>logging levels as well as read the log file. The problem is I need to
>use a relative path which doesn't seem to be possible in log4j. I can't
>use environment an variable. This seems to be a solution offered by
>others. I did come across one suggestion to use the getRelativePath()
>but I don't know how to set this in the log4j.properties file.
>
>Is there a solution for this or even an alternative to log4j if this is
>not possible?
>
>Thanks,
>-------------------------------------------------------
>Richard C. Elsberry
--
Ceki Gülcü
The complete log4j manual: http://www.qos.ch/log4j/
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]