如何在Tomcat中记录stdout输出? [英] How to log stdout output in Tomcat?

查看:648
本文介绍了如何在Tomcat中记录stdout输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将所有stdout输出记录到Tomcat中的catalina.log文件? (即打印到 System.out.println()的所有内容)

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())

当你打开的控制台窗口运行 TOMCAT / bin / startup.bat 显示stdout的输出,但它没有保存到 TOMCAT / logs / catalina。< date> .log

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.

我的具体问题是我在log4j中定义了一个控制台appender以输出到控制台。这些日志消息在Tomcat控制台窗口中正确显示,但它们不会写入catalina.log。我在Windows上运行Tomcat 5.5。谢谢。

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.

编辑:

这是我的log4j.properties文件。它位于 TOMCAT / webapps / app / WEB-INF / classes / log4j.properties

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


推荐答案

之前我遇到过类似的问题,并且没有找到办法在Windows中记录System.out < a href =https://stackoverflow.com/questions/4142650/tomcat-logging-using-log4j>除非您将Tomcat作为Windows服务运行。这似乎在Unix中默认工作,因为 startup.sh 指向 catalina.sh ,它将stdout记录到 catalina.out 如下文件

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 &

在log4j中, ConsoleAppender 本身不会附加到文件,仅发送到 System.out

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

但是,我已经修改了你的log4j属性来添加一个FileAppender和这个配置有效,但当然这会记录到一个单独的日志文件中。

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.

新配置

# 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

输出


= [15:24:03,819 INFO A1]:在my.jsp
= [15:24:03,975 INFO A1]:my.jsp之外
= [15:24:04,880 INFO A1]:在my.jsp
= [15:24:04,880 INFO A1]:out of my.jsp

=[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

另见

如何记录tomcat中部署的特定包中的异常

将选择事件记录到单独的文件中

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

这篇关于如何在Tomcat中记录stdout输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆