How to print to console in Spring Boot Web Application
Asked Answered
S

8

28

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.

Shelbashelbi answered 17/2, 2018 at 18:48 Comment(4)
By console you mean the web browser console?Alchemist
@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 precedenceShelbashelbi
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.Alchemist
@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?Shelbashelbi
F
24

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.

Fraud answered 17/2, 2018 at 19:24 Comment(2)
Oh ok, I see it! Now..is there a way to print the contents of the job? Instead of what looks like an ID?Shelbashelbi
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.Fraud
M
3

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>
Moderato answered 17/2, 2018 at 19:15 Comment(0)
L
3

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);
Lection answered 26/11, 2019 at 5:11 Comment(0)
H
3

You can try Lombok

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

    import lombok.extern.slf4j.Slf4j;
    
    @Slf4j
    public class MyController {
       
       public void getAll() {
          log.info('This is a test.');
       }
    }
Hilde answered 23/9, 2020 at 3:58 Comment(0)
P
2

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");
Psychologize answered 5/4, 2020 at 17:51 Comment(1)
This log4j version is reported as Vulnerability at 2022Permeate
H
2

The logging situation with Java is a bit complicated. Any application you write is going to have a framework and a bunch of libraries and they all write to loggers and different jars may use different loggers. I don't know about Node, maybe there everybody logs to stdout? That's not the case in Java land.

Because of that. even if for your own code you are ok with writing directly to stdout (with System.out.println), if you want all the logged info from your code and from all the other jars to go to the same place, you have to deal with loggers. So you might as well use loggers for your own code too and keep things consistent. It's not just about how to log your own output, it's how to capture the log output from all parts of the application and make sure it's going to the right place and has the granularity that you need.

Spring-boot provides a logging library for you to use by default, called logback. If you don't set up any appenders it will default to writing to the console. Your code doesn't use logback directly, it uses a facade called slf4j. Which sounds absurd I know because we've added 2 libraries just to do what System.out.println does already, but slf4j helps by providing adapters so that if a 3rd party jar uses log4j2 or java.util.logging then it can redirect the output to slf4j and from there to logback, making sure you get a unified view of all the logs from the application, and also giving you a way to configure what level of logging you want to see for the different parts of the application. That's something you don't get with writing to System.out.

Hassanhassell answered 14/6, 2023 at 21:46 Comment(0)
S
1

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

Suppliant answered 19/5, 2021 at 10:27 Comment(0)
H
0

You seem to be confused it is an address where the object is stored you need to make the object useful by defining some methods and giving some properties

Huguenot answered 14/6, 2023 at 20:43 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Hithermost

© 2022 - 2024 — McMap. All rights reserved.