aspose file tools*
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "log4j: right path for the log file" Watch "log4j: right path for the log file" New topic
Author

log4j: right path for the log file

Max Qua
Greenhorn

Joined: Sep 27, 2008
Posts: 20
Hello,
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
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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
Greenhorn

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 log4j.properties as ${LOG_LOCATION}/app.log
3) if some thing goes wrong the google out.

Thanks,
Sachin
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

"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
Greenhorn

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?
Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

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
Sheriff

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
 
Consider Paul's rocket mass heater.
 
subject: log4j: right path for the log file