jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes Writing a log file for my servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Writing a log file for my servlet" Watch "Writing a log file for my servlet" New topic

Writing a log file for my servlet

surendar prabu
Ranch Hand

Joined: Jul 24, 2006
Posts: 102
I need to write a log file for my servlet. I am using a FileWriter now and it is working fine. The problem is the file path.

now i am using absolute path , the exact hardcoded location of the file. but i donot want to do this.

I want to give a relative location to the file path, that is, inside the server/webapps folder. so that even if my server location changes i need not change the log file path.
how can i get this?


SCJP 1.4
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Personally, I use log4j which allows you to configure all of this stuff from a properties file.

As to your question:
One thing to keep in mind is that a Java webapp doesn't necessarily reside in a directory. They can also be deployed and run from a packed war archive.
The war file doesn't even need to be on the same machine.

The ServletContext.getRealPath method will find the file system path to any resource in your webapp. Using getRealPath("/") will find the root of your webapp. If, as mentioned earlier, your app hasn't been deployed as an exploded file system, get real path will return null.
For this reason, relying on getRealPath makes your webapp less portable.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link: http://aspose.com/file-tools
subject: Writing a log file for my servlet
It's not a secret anymore!