Java 如何在 Spring Boot Web 应用程序中打印到控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48844755/
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-12 02:55:01  来源:igfitidea点击:

How to print to console in Spring Boot Web Application

javaspringspring-bootspring-web

提问by SpaceOso

Coming from a Node background, what is the equivalent of console.log() in spring boot?

来自 Node 背景,Spring Boot 中的 console.log() 相当于什么?

For example I'd like to see in my console the job info in the following method.

例如,我想通过以下方法在我的控制台中查看作业信息。

@RequestMapping(value = "jobposts/create", method = RequestMethod.POST)
public Job create(@RequestBody Job job){
    System.out.println(job);
    return jobRepository.saveAndFlush(job);
}

System.out.println(); is how I know to do it in Java but it doesn't seem to appear in my console. Using IntelliJ.

System.out.println(); 是我知道在 Java 中使用它的方式,但它似乎没有出现在我的控制台中。使用 IntelliJ。

采纳答案by robocode

System.out.println(job);like you have done.

System.out.println(job);就像你所做的那样。

It prints something like yourpackage.Job@2g45e0f9

它打印出类似 yourpackage.Job@2g45e0f9 的东西

Try to execute you code using debug mode and see if the post method will be executed as it has to do.

尝试使用调试模式执行你的代码,看看 post 方法是否会像它必须做的那样执行。

回答by lrathod

Did you tried adding console appenderin you logging configuration file.? Here is how you can do in slf4j + logback ecosystem

您是否尝试在日志记录配置文件中添加控制台附加程序。?这是您在 slf4j + logback 生态系统中的操作方法

in logback.xml,

在 logback.xml 中,

<configuration>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
        <timeZone>UTC</timeZone>
    </encoder>
</appender>
<logger name="com.yourcompany.packagename" level="INFO" additivity="false">
    <appender-ref ref="consoleAppender" />
</logger>
<root level="ERROR">
    <appender-ref ref="consoleAppender" />
</root>
</configuration>

回答by kreitcher

This is in addition to what @robocode posted. Override the toStringmethod in the Jobclass to print the parameters the way you would like to see them.

这是@robocode 发布的内容的补充。覆盖类中的toString方法,Job以您希望看到的方式打印参数。

public class Job{
   String p1;
   int p2;
   .
   .
   @Override
   public String toString(){
      return "p1: "+p1+", p2: "+p2;
   }
}

Makes it easier to simply sysout your objects.

使简单地系统输出您的对象变得更容易。

Job job = new Job();
System.out.println(job);

回答by DareDevil

import log4j dependency in your pom file

在 pom 文件中导入 log4j 依赖项

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

define logger in your controller like:

在您的控制器中定义记录器,如:

private static Logger logger = LoggerFactory.getLogger(YourClassName.class);

then use

然后使用

logger.info("your message");