logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Kjome <h...@visi.com>
Subject RE: Must use relative path for log file
Date Wed, 24 Aug 2005 13:43:56 GMT

Just as long as you realize that context.getRealPath() may return null in the
case where the webapp is run directly from the .war file rather than being
unpacked to a directory by the servlet container.  The servelet spec allows for
this behavior so you are depending on container-specific, and probably even
configuration-specific, container behavior.

Ulitmately, you should probably define a path outside the webapp for storing log
files.  But as far as referencing a property in the config file for a path
determined at run-time, this is a good example for everyone to see.  Thanks for
posting it.

Jake

Quoting "Elsberry, Richard C - Eagan, MN - Contractor"
<Richard.C.Elsberry@usps.gov>:

> 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:listid@qos.ch]
> 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: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>




---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message