简体   繁体   English

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

[英]How to print to console in Spring Boot Web Application

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(); System.out.println(); is how I know to do it in Java but it doesn't seem to appear in my console.是我知道在 Java 中使用它的方式,但它似乎没有出现在我的控制台中。 Using IntelliJ.使用 IntelliJ。

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

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 方法是否会像它必须做的那样执行。

Did you tried adding console appender in 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>

This is in addition to what @robocode posted.这是@robocode 发布的内容的补充。 Override the toString method in the Job class to print the parameters the way you would like to see them.覆盖Job类中的toString方法,以您希望看到的方式打印参数。

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

You can try adding Project Lombok to your Maven or Gradle file.您可以尝试将 Project Lombok 添加到您的 Maven 或 Gradle 文件中。

Use the annotation @Slf4j at class level and add log.info('text') anywhere in your class.在类级别使用注释@Slf4j并在类中的任何位置添加log.info('text')

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MyController {
   
   public void getAll() {
      
      // log is already initialized
      // use it anywhere in the annotated class
      log.info('This is a test.');

   }
}

如果以上没有解决问题,请确保您正在发出具有 sysout 语句的适当请求

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");

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

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