How to log stdout output in Tomcat?

前端 未结 3 1991
粉色の甜心
粉色の甜心 2020-12-29 06:04

Is there a way to log all stdout output to the catalina.log file in Tomcat? (i.e. everything that gets printed to System.out.println())

The console wind

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-29 06:31

    I've come across similar questions before, and haven't found a way to do this by logging System.out in Windows unless you are running Tomcat as a Windows service. This seems to work by default in Unix since startup.sh points to catalina.sh which logs stdout to the catalina.out file like below

    org.apache.catalina.startup.Bootstrap "$@" start >> "$CATALINA_BASE"/logs/catalina.out 2>&1 &
    

    In log4j, ConsoleAppender by itself does not append to a File, only to System.out

    However, I've modified your log4j properties to add a FileAppender and this config works, but of course this logs into a separate log file.

    New config

    # Set root logger level to DEBUG.
    log4j.rootLogger=DEBUG, console, myFile
    
    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    log4j.appender.console.layout.ConversionPattern=[%d{ABSOLUTE} %-5p %c{1}]: %m%n
    
    
    
    
    # myFile writes to file
    log4j.appender.myFile=org.apache.log4j.RollingFileAppender
    log4j.appender.myFile.File=logs/tomcatlog4j.log
    log4j.appender.myFile.MaxFileSize=100KB
    log4j.appender.myFile.layout=org.apache.log4j.PatternLayout
    log4j.appender.myFile.layout.ConversionPattern==[%d{ABSOLUTE} %-5p %c{1}]: %m%n
    

    Output

    =[15:24:03,819 INFO A1]: In my.jsp =[15:24:03,975 INFO A1]: Out of my.jsp =[15:24:04,880 INFO A1]: In my.jsp =[15:24:04,880 INFO A1]: Out of my.jsp

    also see

    How to log exceptions from a specific package deployed in tomcat

    log select events into a separate file

    https://serverfault.com/questions/201178/tomcat-5-5-how-to-redirect-the-logging-output-to-one-file-per-web-application

提交回复
热议问题