开发者

Logging Tomcat6 on Ubuntu

Im using ubuntu and tomcat6 for my server. When im calling a servlet or a jsp p开发者_如何学运维age, 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".


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜