• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

servlet to write to file

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, if you'd like finer grained control over your logging, explore packages like Log4J.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic