20

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 window that opens when you run TOMCAT/bin/startup.bat displays output from stdout, but it's not saved to TOMCAT/logs/catalina.<date>.log.

My specific problem is that I have a console appender defined in log4j to output to the console. These log messages appear correctly in the Tomcat console window, but they are not written to catalina.log. I'm running Tomcat 5.5 on Windows. Thanks.

EDIT:

Here is my log4j.properties file. It is located at TOMCAT/webapps/app/WEB-INF/classes/log4j.properties:

log4j.rootCategory=DEBUG, console
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
Michael
  • 34,873
  • 17
  • 75
  • 109
  • Does tomcat log to catalina.log, when you start it as a windows service instead of using startup bat? – Gandalf Oct 12 '11 at 13:22
  • @Gandalf I don't know. I don't want to run it as a service. – Michael Oct 12 '11 at 13:38
  • Where do you have log4j configured with console appender? Per web application or on tomcat itself? – Gandalf Oct 12 '11 at 13:38
  • @Gandalf It's in my web application. So it's located at: `TOMCAT/webapps/app/WEB-INF/classes/log4j.properties` – Michael Oct 12 '11 at 13:48
  • @Gandalf Tomcat seems to use java.util.logging...is it possible that log4j is not compatible? – Michael Oct 12 '11 at 13:58
  • I don't think so, but I am not exactly sure in this case. We also use log4j together with tomcat 5 and it works like a charm but we never use console appenders but rolling file appenders. Have you tested only with debug log or also with error? Maybe tomcat is configured with a threshold for file output. – Gandalf Oct 12 '11 at 14:26

3 Answers3

11

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

Community
  • 1
  • 1
JoseK
  • 31,141
  • 14
  • 104
  • 131
  • 1
    So you're saying on Linux, a ConsoleAppender would write to catalina.out, but on Windows it doesn't unless Tomcat is run as a service? – Michael Oct 13 '11 at 13:19
  • @Michael: I can confirm ConsoleAppender **does** write to `catalina.out` on Linux. I've tested this. – JoseK Oct 14 '11 at 06:17
  • Ok, thanks, I just wanted to make sure I understood what you where saying. So it sounds like the answer to my question is, "No, ConsoleAppender will only write to catalina.log on Windows if Tomcat is run as a service" – Michael Oct 14 '11 at 12:54
1

Did you checked, whether the log4j.properties file can be found from your application? Maybe you can check, by setting a hardcoded file path like

-Dlog4j.configuration=file:///C:\Dev\log4j.properties

If the logs are written after these change, the relativ path to the log4j file is wrong.

Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66
  • It's in the root of my classpath. So it's located at: `TOMCAT/webapps/app/WEB-INF/classes/log4j.properties`. – Michael Oct 12 '11 at 13:48
1

If I look at the default logging config of tomcat 5.5 in logging.properties:

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.FileHandler

That looks to me as if stdout of web applications might be logged to files only for level INFO and above, regading that http://tomcat.apache.org/tomcat-5.5-doc/logging.html states that in tomcat JULI logging configuration loggers do not use parent's handlers when they are assigned their own handlers. Also the file should be prefixed localhost and not catalina. But then I do not understand how the output comes to your output window :/

Gandalf
  • 2,350
  • 20
  • 28
  • 1
    Thanks. I tried changing all those levels from "INFO" to "DEBUG", but none of the stdout output appears in any of the log files (catalina, localhost, manager, or host-manager). I'll have to read that link in more detail. – Michael Oct 12 '11 at 19:48