Meaningless Drivel is fun!*
The moose likes Tomcat and the fly likes How to redirect tomcat console output to a text file in webapps folder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Products » Tomcat
Bookmark "How to redirect tomcat console output to a text file in webapps folder" Watch "How to redirect tomcat console output to a text file in webapps folder" New topic
Author

How to redirect tomcat console output to a text file in webapps folder

Sandeep Ramachandran
Greenhorn

Joined: Jun 30, 2008
Posts: 3
Hi.
I am running a servlet on tomcat 6. I want to redirect System.out.println and System.out.err messages from the tomcat console into a file in the "webapps" folder. This is coz i am running my servlet on my school's tomcat server where I have access to only the webapps folder. I have tomcat installed on my laptop and when i redirect the console output using System.setOut and System.setErr into a text file, the text file is generated in the bin folder. Hence I gave a relative path to webapps folder from bin folder. This works fine on the tomcat on my laptop but does not work on my school's server. I am not able to view any other folder than the webapps due to security restrictions on the school server. Please help me with a work around

Thanks in advance

Sandeep
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
I'm guessing your main goal here is not outputting anything BUT to get log files so that you can debug your application. Am I correct? If that is the case then one workaround is to discard all your efforts to that direction and use some ready logging mechanism. Like log4j
http://logging.apache.org/log4j/1.2/download.html

Altought it takes some time to get yourself familiar with it and learning how to configure it, it pays off in the end. And once you know how to do it you can copy the configuration to all your future projects as well and so on...
Ben Zung
Ranch Hand

Joined: Mar 25, 2004
Posts: 109
You could also look for some code online to intercept the system output and save it to someplace. But it's kind of shaky if you had only webapps directory to put the file. You probably don't want to reside it right under webapps. And if you put it under your application's, say log or tmp directory, you may have problem to re-deploy your application -- I had done that before. I was not able to un-deploy the application successfully because the log file was open.

Good luck.
Ben
Sandeep Ramachandran
Greenhorn

Joined: Jun 30, 2008
Posts: 3
Hi Ben and Ilari,

Thank you for your suggestions. Ilari I have downloaded log4j. Thank you. Meanwhile I have tried Ben's suggestion and it works. Thank you.

Sandeep
Amir Iqbal
Ranch Hand

Joined: Jun 23, 2007
Posts: 97
hey sandeep .
yar if you have done this meanz redirected your console output to the .txt file then plase do tell me i am doing the same thing but unfortunatly failed because i have no idea how to use log4j so please help me . because i got stuck in my final project.

waiting for your response

Amir Iqbal


i am Java +ve Now !
Sachin Joshi
Ranch Hand

Joined: Aug 06, 2008
Posts: 83

Download log4j.jar from Download

You need to use below sentance to initialize.

Logger logger = org.apache.log4j.Logger.getLogger("MyLoggerName");


and use any of below instead of System.out.println()

logger.debug("")
logger.info("")
logger.warn("")
logger.error("")

Hope this helps...


Web Development Tips and Tutorials - By Sachin
Amir Iqbal
Ranch Hand

Joined: Jun 23, 2007
Posts: 97
thanks alot Sachin brother.

i will try your suggestion
Mary Taylor
Ranch Hand

Joined: Sep 11, 2000
Posts: 319
Where do you put log4j when installing it?

Thanks.
 
GeeCON Prague 2014
 
subject: How to redirect tomcat console output to a text file in webapps folder