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 !!)
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.
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.
An IDE is no substitute for an Intelligent Developer.
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.