This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Servlets and the fly likes log4j: right path for the log file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "log4j: right path for the log file" Watch "log4j: right path for the log file" New topic

log4j: right path for the log file

Max Qua

Joined: Sep 27, 2008
Posts: 20
I have the following problem. I am using log4j for logging my servlets but I don't understand how configure log4j in order to write the log file in the WEB-INF directory. I don't want to use an absolute path, in order to work on localhost and on the server without change the configuration. Is there a way?
Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

Why would you want to write there? I consider WEB-INF to be read only.
Better to write logs to /var/log or /tmp or /log
Sachin Kumar

Joined: Aug 31, 2008
Posts: 20
As Pat said its not a good practice but still have to do then.
1)Use servlet method getRealPath (deprecated) and set the same into system property as LOG_LOCATION.
2)you can read system prperties into as ${LOG_LOCATION}/app.log
3) if some thing goes wrong the google out.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63227

"Sachin Kr", please check your private messages for an important administrative matter.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Max Qua

Joined: Sep 27, 2008
Posts: 20
Thanks, guys.
Yes, WEB-INF maybe is not the best place for the log. I would like to have the log in a directory of my web application space.
But why should I use a deprecated method? Is there another way?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63227

I'd rethink that. Placing dynamic files inside your web app alters its structure and can make it harder to redeploy as the app no longer matches its war file.

That said, the ServletContext.getRealPath method is not deprecated. This is something you could have determined by simply looking at the JavaDoc for the Servlet API.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

One thing to consider, whether you use an absolute path or if you use getRealPath is that writing anything inside your webapp's file structure will make your application non-portable and may limit how it can be deployed.

JEE web applications can, but aren't required to, be deployed from an operating system's file structure. Containers can also deploy a war file without unpacking it. Some do this by default. There may be others than only deploy application's this way. If the application is not deployed from the file system, getRealPath will return null. This makes sense because there won't be a path to the resource.

If you write your application so that it is required to be deployed in a particular way, you have made it non-portable and have limited what environments on which it can be run.

Maybe the right question is:
Why do you want to write the log files to your application's file structure?
Is it just so that you can read the logs online?
If so, you might consider writing a servlet that can read the file and stream it to the browser.

Don't forget to consider the security implications of making log files open to the web.
[ November 27, 2008: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: log4j: right path for the log file
It's not a secret anymore!