简体   繁体   English

Eclipse中的Tomcat控制台输出:我的System.out.print()输出在哪里,如何调试?

[英]Tomcat console output in Eclipse: where is my System.out.print() output, how to debug?

In Eclipse's console I can only see the tomcat server start logs, but then nothing else, none of the system.out.print() from my application are displayed, which makes it difficult to debug. 在Eclipse的控制台中,我只能看到tomcat服务器的启动日志,但是除此之外,没有任何显示,我的应用程序中的system.out.print()都不会显示,这使得调试变得很困难。

What settings can I change in Eclipse to get my system.out.print() out put back in the console? 我可以在Eclipse中更改哪些设置以将system.out.print()放回控制台?

It's not the right practice but you could switch to System.err.println() as a switch instead to capture values in console. 这不是正确的做法,但是您可以切换到System.err.println()作为开关,而不是在控制台中捕获值。 The right approach will be to use loggers (like log4j) as pointed in comments. 正确的方法是使用注释中指出的记录器(如log4j)。

Ok, the best approach is always to use a logger and log4j is my preferred tool. 好的,最好的方法是始终使用记录器,而log4j是我的首选工具。

First configure the logger by creating a log4j.properties file. 首先,通过创建log4j.properties文件来配置记录器。 Here is a good example that matches your problem: 这是一个与您的问题匹配的好例子:

# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE, LOGFILE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n

This configuration will send info and above messages to the console. 此配置会将信息和以上消息发送到控制台。 Now how you want your log4j.properties to initialize and how you want to reference the the log4j jar libraries dependends upon how you deploy your application. 现在,您希望如何初始化log4j.properties以及如何引用log4j jar库取决于您如何部署应用程序。

A good reference for configuration on stackoverflow is here: Where should I put the log4j.properties file? 有关stackoverflow配置的一个很好的参考资料在这里: 应该将log4j.properties文件放在哪里?

A good reference on log4j usage is here: http://www.codejava.net/coding/how-to-configure-log4j-as-logging-mechanism-in-java 关于log4j使用的一个很好的参考在这里: http : //www.codejava.net/coding/how-to-configure-log4j-as-logging-mechanism-in-java

Note you can configure in multiple ways. 请注意,您可以通过多种方式进行配置。 It is your call. 是你的电话。 Now in your class file initialize the logger: 现在在您的类文件中初始化记录器:

public class MyClass {
  private static final Logger log = Logger.getLogger(MyClass.class);

  ....

  log.info("A log message to the console!!!!");

That's it really. 就是这样。 Now you have complete control. 现在,您可以完全控制。 It's almost always best to use a logger to control output in general. 通常,通常最好使用记录器来控制输出。 System.XXX.println's can't be controlled via a central configuration and you will forget to remove them later. 不能通过中央配置来控制System.XXX.println,而您以后会忘记删除它们。 Also you can add additional appenders to persist output like so: 您还可以添加其他追加程序来持久化输出,如下所示:

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.Threshold=INFO
log4j.appender.LOGFILE.MaxFileSize=5MB
log4j.appender.LOGFILE.File=mylogfile.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p [%c]: %m%n

This will generate a mylogfile.log and will keep the files at 5MB max and will keep several (forget the default) versions with a timestamp appended. 这将生成一个mylogfile.log,并将文件最大保留为5MB,并保留多个(忘记默认)版本并附加时间戳。

I hope that helps. 希望对您有所帮助。 log4j has been around forever and is excellent. log4j已经存在很久了,非常好。 There are more tutorials on the web than you can shake a stick at. 网络上有更多的教程供您参考。

您也可以尝试清理项目(项目->清理),它对我有所帮助,并且System.out开始再次显示在控制台中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM