Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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.

like image 694
SpaceOso Avatar asked Feb 17 '18 18:02

SpaceOso


People also ask

How do I log a console output to spring boot?

Configuring the file name and path To make Spring Boot write its log to disk, set the path and filename. With this configuration, Spring Boot will write to the console and also to a log file called spring. log , at the path you specify.

How do I run a spring boot console application?

Run Spring Boot Application with the Command LineOpen terminal window and change directory to the root folder of your Spring Boot application. If you list files in this directory, you should see a pom. xml file. You can also run your Spring Boot application as an executable Java jar file.

Which is the default logging file in spring boot?

By default, Spring Boot will only log to the console and will not write log files. If you want to write log files in addition to the console output you need to set a logging. file or logging. path property (for example in your application.

What is the use of spring boot framework?

Spring Boot helps developers create applications that just run. Specifically, it lets you create standalone applications that run on their own, without relying on an external web server, by embedding a web server such as Tomcat or Netty into your app during the initialization process.


2 Answers

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

It prints something like 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.

like image 149
Cimon Avatar answered Sep 30 '22 20:09

Cimon


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

in 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>
like image 30
lrathod Avatar answered Sep 30 '22 20:09

lrathod