Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes servlet to write to file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet to write to file" Watch "servlet to write to file" New topic
Author

servlet to write to file

keddy lewis
Greenhorn

Joined: Jul 04, 2007
Posts: 5
To whom that may help,

I want to be able to have a log file that a servlet could write to (mainly for debugging purposes when using esoteric APIs).

I have a Resin account with a web hosting site and there is no console, configuration panels or logs so when things go wrong with a servlet i have no way of tracking it and seeing what the problem is (except using the grey matter between my ears and lots of time !). ALthoug they maybe a way that i do not know.

Could someone tell me how to write to a file or have some other accessible log of what is happening in my servlets.

I have tried writing to files but never see them when i look (i am not even sure where they will go, i know the did not end up where i can see them !!)

Some code i have tried...


and i tried this:



Neither produced something i could find !!

Thank you, regards and smile

Keddy
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Before writing code like that, you should try the following:



Or something similar. You should NOT then try to read from this location. The point is that the local or default directory is usually not where you think it is, and you shouldn't rely on it being correct. You should consider using the ServletContext to write your logs to a place you define instead.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Or, if you'd like finer grained control over your logging, explore packages like Log4J.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Agreed. Personally I also like keeping logs in the container's log directory, but I reduced my advice since your were using an external host and did not have full control of the environment.
keddy lewis
Greenhorn

Joined: Jul 04, 2007
Posts: 5
Thank you both for your assistance

I would like to clarify what you meant if i may. When you stated to use the ServletContext did you mean :

1) use the log() method. If so it is not appropriate for the reasons you mentioned, that is, i believe i do not have access the the container log directory.

2) If you mean to use methods within the ServletContext to obtain a path that is useful, what exactly would you suggest.

using getRealPath("/dit.log")

which i could then use some kind of stream to write out log info.

3) or something total different !!

a little code may make explanation a little clearer.. thank you in advance

Keddy Lewis
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16137
    
  21

Using a logger such as Log4J or the java.util.logging facility is the best way to manage things. You get the ability to log to multiple locations at different levels of detail and you can change logging parameters without having to rebuild the app.

If you must do plain-vanilla file writes, it's best to pass in an absolute file location as an application parameter. There's no guarantee that you'll have a predictable "current directory" if you use a relative pathname, much less that you'll have write permissions for it. Double that for relative-to-the webapp, since you can't update a WAR. File and resource locations relative to the web app itself should be considered as read-only if you want maximum joy.

Logging needn't be to a file (or just to a file, if you're using multiple log destinations). It's quite common for example to log to a completely different computer. See how the Unix syslog facility works for an example. Custom loggers can even fire pagers etc.

While your options are somewhat constrained if you're working within a confined space within someone else's server, I believe that Log4J will at least let you set up a log destination using a Log4j config file that you include inside the webapp.


Customer surveys are for companies who didn't pay proper attention to begin with.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
One more thing you might want to check with your provider is whether the container is running under security manager. In that case you might have to ask them to grant certain permission for writing files to your code base.


Rahul Bhattacharjee
LinkedIn - Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet to write to file