log messages not printing in springboot using org.slf4j.Logger;
To enable printing log messages in Spring Boot using the org.slf4j.Logger
interface, you need to follow these steps:
- Add the required dependencies in your Maven or Gradle build file. For Maven, add the following dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
For Gradle, add the following dependency:
implementation 'org.springframework.boot:spring-boot-starter-web'
- Create a new class or open an existing class where you want to use the logger. Import the
org.slf4j.Logger
interface and its associatedLoggerFactory
class:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
- Declare a logger instance in your class:
private static final Logger logger = LoggerFactory.getLogger(YourClassName.class);
Replace YourClassName
with the name of your class.
- Use the logger to print log messages wherever needed in your class. For example:
logger.debug("This is a debug log message");
logger.info("This is an info log message");
logger.warn("This is a warning log message");
logger.error("This is an error log message");
You can use different log levels like debug
, info
, warn
, and error
depending on the severity of the message.
- Configure the log level and log file destination in your application's
application.properties
orapplication.yml
file. For example, to set the log level toDEBUG
and print logs to the console, add the following configuration:
In application.properties
:
logging.level.root=DEBUG
In application.yml
:
logging:
level:
root: DEBUG
These steps should enable you to print log messages in Spring Boot using the org.slf4j.Logger
interface. Remember to customize the log levels and destinations according to your application's requirements.