Junior — Middle
How can logging be configured and maintained in an application developed with Spring Boot?
sobes.tech AI
Answer from AI
Logging in Spring Boot is primarily configured through built-in support for SLF4J and Logback (by default). You can configure logging using the application.properties or application.yml file.
Example configuration in application.properties:
logging.level.root=INFO
logging.level.com.example=DEBUG
logging.file.name=app.log
logging.level.root— the default logging level.logging.level.com.example— the logging level for thecom.examplepackage.logging.file.name— the filename for log output.
For more fine-tuned configuration, you can create a custom Logback configuration file (logback-spring.xml), where you can define appenders, output patterns, and logging levels.
Logging in code is done via SLF4J:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyService {
private static final Logger logger = LoggerFactory.getLogger(MyService.class);
public void doWork() {
logger.info("Starting work");
try {
// logic
} catch (Exception e) {
logger.error("Error during work", e);
}
}
}
Thus, Spring Boot simplifies logging setup, allowing quick changes to levels and log output without modifying code.