Java 如何从 Servlet 将文本输出到控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2484556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:16:40  来源:igfitidea点击:

How to ouput text to console from Servlet

javaloggingservletsconsolejconsole

提问by mithun1538

I have a servlet. But it is not working as desired. Hence for debugging purposes, I want to print statements to the java console(the one that can be opened using the java icon in taskbar). However, if I use System.out.println("message"), it doesnt display in java console.

我有一个 servlet。但它没有按预期工作。因此,出于调试目的,我想将语句打印到 java 控制台(可以使用任务栏中的 java 图标打开的控制台)。但是,如果我使用 System.out.println("message"),它不会显示在 java 控制台中。

Is there any alternative way where I can display messages to the console from the servlet? Or can anyone suggest me an alternative way to display messages to any other console?

有没有其他方法可以从 servlet 向控制台显示消息?或者有人可以建议我另一种向任何其他控制台显示消息的方法吗?

采纳答案by Bozho

In which console do you expect it to appear?

您希望它出现在哪个控制台中?

Depending on the servlet container (I assume Tomcat), the logs are stored in a logsfolder. For Tomcat this is tomcat/logs(or more often referred to as CATALINA_HOME/logs). If you are running it from within an IDE - they should be in the IDE console.

根据 servlet 容器(我假设是 Tomcat),日志存储在一个logs文件夹中。对于 Tomcat,这是tomcat/logs(或更常称为CATALINA_HOME/logs)。如果您在 IDE 中运行它 - 它们应该在 IDE 控制台中。

As a sidenote, using System.outisn't advisable for a real product - use a logging framework (like log4j).

作为旁注,System.out对于真正的产品,不建议使用 - 使用日志框架(如log4j)。

回答by uthark

You should use logging, e.g. java.util.loggingor log4j

您应该使用日志记录,例如java.util.logginglog4j

Example of relevant log4j configuration:

相关 log4j 配置示例:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="console" class="org.apache.log4j.ConsoleAppender"> 
    <param name="Target" value="System.out"/> 
    <layout class="org.apache.log4j.PatternLayout"> 
      <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/> 
    </layout> 
  </appender> 

  <root> 
    <priority value ="debug" /> 
    <appender-ref ref="console" /> 
  </root>

回答by BalusC

I want to print statements to the java console(the one that can be opened using the java icon in taskbar)

我想将语句打印到 java 控制台(可以使用任务栏中的 java 图标打开的那个)

You need to realize that servlets actually runs at the server side, not at the client side. Those are in fact two physically differentenvironments which communicates with each other through the network using the HTTP protocol. That Java console will only work for Java programs which runs at the client side, such as appletsand JNLP.

您需要意识到 servlet 实际上是在服务器端运行,而不是在客户端运行。这些实际上是两个物理上不同的环境,它们使用 HTTP 协议通过网络相互通信。该 Java 控制台仅适用于在客户端运行的 Java 程序,例如小程序JNLP

In your case, you just need to read the server logs. That's where the stdoutwill by default print to, or use a better configureable logging framework such as logback. The server logs are usually found in a straightforward folder/file in the server installation directory, such as /logsin case of Tomcat.

在您的情况下,您只需要阅读服务器日志。这就是stdout默认情况下将打印到的地方,或者使用更好的可配置日志记录框架,例如logback。服务器日志通常位于服务器安装目录中的一个简单的文件夹/文件中,例如/logs在 Tomcat 的情况下。

回答by david

Look for the log file in the log-folder of your servlet container (i.e. Tomcat).

在 servlet 容器(即 Tomcat)的日志文件夹中查找日志文件。

As suggested use log4jto generate debug messages. This logging framework provides a configuration file where you can set things like where the logs should be written into or how the log messages should be formatted. As soon it's in place you can replace

按照建议使用log4j生成调试消息。此日志记录框架提供了一个配置文件,您可以在其中设置日志应写入的位置或日志消息的格式等内容。一旦到位,您就可以更换

System.out.println("message");

with

log.debug("your debug message");

or

或者

log.info("in case of a message with info character");
log.error("routine foo has experienced an exception", e);

Now your code is much cleaner and there is even another benefit - when being placed correctly these logs act as documentation for your code segments.

现在您的代码更简洁了,甚至还有另一个好处——当正确放置这些日志时,这些日志可以作为您的代码段的文档。

回答by Andrey

Servlet (HttpServlet) has a method log(String s) inherited from GenericServlet class.

Servlet (HttpServlet) 有一个方法 log(String s) 继承自 GenericServlet 类。

So you can just include

所以你可以只包括

log("output text")

in the servlet's code and see output.

在 servlet 的代码中并查看输出。

If you use Eclipse log goes right into console.

如果您使用 Eclipse,日志会直接进入控制台。

If you use IntellijIdea log goes into Run --> "Tomcat Localhost Log" tab.

如果您使用 IntellijIdea 日志进入运行 -->“Tomcat 本地主机日志”选项卡。