如何在 Tomcat 中记录 stdout 输出?

发布于 2024-12-09 04:55:28 字数 708 浏览 0 评论 0 原文

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

运行 TOMCAT/bin/startup.bat 时打开的控制台窗口显示来自 stdout 的输出,但是它不会保存到 TOMCAT/logs/catalina..log

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

编辑:

这是我的 log4j.properties 文件。它位于 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牛↙奶布丁 2024-12-16 04:55:29

我以前遇到过类似的问题,但还没有找到通过在 Windows 中记录 System.out 来做到这一点的方法 除非您将 Tomcat 作为 Windows 服务运行。这似乎在 Unix 中默认有效,因为 startup.sh 指向 catalina.sh ,它将 stdout 记录到 catalina.out 文件,如下

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

所示log4j, ConsoleAppender 本身不会附加到文件,仅附加到 System.out

但是,我已经修改了您的 log4j 属性以添加 FileAppender 并且此配置有效,但是当然这个日志到一个单独的日志文件中。

新配置

# 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]:来自 my.jsp

另请参阅

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

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

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

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

潦草背影 2024-12-16 04:55:29

您是否检查过,是否可以从您的应用程序中找到 log4j.properties 文件?
也许您可以通过设置硬编码文件路径来检查,例如

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

如果在这些更改之后写入日志,则 log4j 文件的相对路径错误。

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.

栀子花开つ 2024-12-16 04:55:29

如果我在logging.properties中查看tomcat 5.5的默认日志配置:

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

在我看来,Web应用程序的标准输出可能仅记录到INFO及以上级别的文件中,考虑到http://tomcat.apache.org/tomcat-5.5-doc/logging.html 指出在 tomcat 中当 JULI 日志记录配置记录器被分配自己的处理程序时,它们不使用父级的处理程序。此外,该文件应以 localhost 为前缀,而不是 catalina。但后来我不明白输出是如何到达你的输出窗口的:/

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 :/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文