18

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.

SpaceOso
  • 255
  • 1
  • 2
  • 13
  • By console you mean the web browser console? – Simon Berthiaume Feb 17 '18 at 18:51
  • @SimonBerthiaume no sorry, I mean the console in my IDE. I can see it print logs like this.. `2018-02-17 13:51:10.916 DEBUG 31564 --- [ restartedMain] o.s.w.c.s.StandardServletEnvironment : Adding PropertySource 'servletConfigInitParams' with lowest search precedence` – SpaceOso Feb 17 '18 at 18:53
  • Unless you are explicitly building an app made to be operated from the commandline, it is considered bad practice to output straight to System.our and System.err but it should work. What should be done is using logging frameworks such as SLF4J on top of logback or log4j. – Simon Berthiaume Feb 17 '18 at 19:16
  • @SimonBerthiaume Oh ok, I didn't know this. I'll look into using a framework for this. Is the goal of the framework to be a dev / prod setup? So it will only do logs when run in dev? – SpaceOso Feb 17 '18 at 21:44

6 Answers6

18

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.

Thomas Smyth
  • 3,953
  • 5
  • 22
  • 33
Cimon
  • 359
  • 1
  • 10
  • 1
    Oh ok, I see it! Now..is there a way to print the contents of the job? Instead of what looks like an ID? – SpaceOso Feb 17 '18 at 19:46
  • 2
    You should implement inside the class Job methods that return instance variables (or whatever you want). So you could write `System.out.println(job.getSomething());`to print content. See about setters and getters in java documantation. Of course you could override the toString() method to gather all the information for you object content and then calling `System.out.println(job.toString())` to print them all. – Cimon Feb 17 '18 at 20:38
2

This is in addition to what @robocode posted. Override the toString method in the Job class to print the parameters the way you would like to see them.

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);
zmag
  • 6,257
  • 12
  • 26
  • 33
kreitcher
  • 21
  • 1
1

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>
lrathod
  • 874
  • 1
  • 6
  • 16
1

If the above doesn't solve the problem make sure you are making the appropriate requests which has the sysout statement

0

import log4j dependency in your pom file

<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");
Caffeine Coder
  • 344
  • 1
  • 11
0

You can try adding Project Lombok to your Maven or Gradle file.

Use the annotation @Slf4j at class level and add log.info('text') anywhere in your class.

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

   }
}
user9869932
  • 4,537
  • 3
  • 42
  • 42