aspose file tools*
The moose likes Tomcat and the fly likes Setting up logging Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Setting up logging" Watch "Setting up logging" New topic
Author

Setting up logging

Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 582
I am trying to set up logging:

I have a control servlet
with the line of code String prefix = getServletContext().getRealPath("/");

that returns prefix = "C:\jakarta-tomcat-5.0.19\webapps\WebBay"

Is there anyway to interrogate the context that will return
C:\jakarta-tomcat-5.0.19 only. The reason is that I can then append log to the prefix and open a logging file in C:\jakarta-tomcat-5.0.19\log.

Otherwise I guess its playing around with string functions.

Thanks for any help.

Tony
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Is there a reason you're not using a perfectly good, highly configurable, and very performant logging package (like log4j )

As for what you're asking, there is no built-in way to get this. You'll have to parse the String yourself. One gotcha: Tomcat can be configured to serve a web application from *anywhere*, not necessarily just from the CATALINA_HOME directories. So if you expect something of the form:
c:\tomcat\webapps\foo
what happens when the application is actually deployed to:
c:\foo ?
c:\webapps\foo?

A second gotcha is that Tomcat can be configured to run from a packed WAR file, in which case getRealPath() will return null.

One option is to specify (as part of your application's setup specs or 'administrators guide') a variable in JNDI provides an absolute path to find the log file directory. So it would be a configuration option in web.xml, and your code would then retrieve the path to use from this file.

But if we're going that far, I'd start to really look hard at log4j.

If your concern is someone being able to retrieve your logs by entering (guessing) the filename and typing it directly into a browser window, you can place your logs in WEB-INF/logs (that's what I do, with log4j).
[ May 25, 2004: Message edited by: Mike Curwen ]
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 582
Thanks Mike, I will look at log4, I have had a brief look but all the examples I looked at set logging to standard putput not a file.

I will also look at JNDI for mapping the path name for the file.

Tony
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I like this one:
http://www.vipan.com/htdocs/log4jhelp.html

It shows using log4j and files.
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 582
Thanks Mike,

got it up and running last night, I passed in the path and file name as a parameter in the web.xml file.

Tony
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting up logging