Using logs
We can now use the log frameworks we have configured in our Spring Boot application to define logs on the different parts of our code. To do so, we must first create a new logger instance.
An example would be creating a log when a user attempts to get a list of all anti-heroes. In AntiHeroeController
, we will add the following code to create a new logger instance:
private static final Logger LOGGER = LoggerFactory.getLogger(AntiHeroController.class);
We must also be aware that LoggerFactory
and Logger
should be under the SLF4J dependency. It is always recommended to use SLF4J as this is an abstraction of logging frameworks and makes it easier to switch between them.
In this case, our import should be as follows:
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
Once we have created a new logger instance, we can now use it in our methods, for example, if we want to display a log when the user attempts to get a list of anti-heroes.
To accomplish...