views:

66

answers:

2

Im using ubuntu and tomcat6 for my server. When im calling a servlet or a jsp page, the "logger" (System.out.println()) logs into the syslog of the server /var/log/syslog. How can i change this, that the server will write in a own log file like the catalina.out?

The problem is that there are no line breaks in my syslog (i used \n in the system.out), so it looks really "dirty".

A: 

You would want to look into log4j

http://logging.apache.org/log4j/1.2/manual.html

and set up a log4j.properties that will do what you want it to do.

Generally, you do not want to use System.out.println().. everything should go through log4j. So like

logger.debug("whatever i am debugging");

and

logger.warn("danger!");

This way you can change your log4j level and turn off debugging spam, without having to remove it from your code.

bwawok
exactly what im looking for :) thank you very much
accept my answer please :)
bwawok
+1  A: 

Several ways:

  1. Change the destination of the stdout using System#setOut() and eventually also the stderr.

    System.setOut(new PrintStream(new File("/new.log")));
    

    This is however not recommended. Don't do it. Use a logger.

  2. Configure the webapp context to swallow the output. Set the swallowOutput of the <Context> element to true. This will redirect all stdout/stderr to Tomcat's logfile.

    <Context swallowOutput="true">
    

    Still not really recommended. Using stdout/stderr instead of logger is a poor practice. Also, this will clutter Tomcat's logfile with webapp-specific logs. But if this is exactly what you're after...

  3. Replace all stdout/stderr calls by a fullworthy logger like the (currently legacy) log4j or its successor slf4j which gives you a high degree of freedom in configuring the logged information, logging destination, logging format, etcetera.

BalusC