πŸ“ Logging in Spring Boot

Logging is how your application writes messages during its execution β€” errors, info, debug messages, etc.
It helps developers track what’s happening inside the app and troubleshoot problems.


πŸ”§ Logging in Spring Boot (Default)

Spring Boot uses SLF4J with Logback by default. You don’t need to add any extra dependency.

✍️ How to Write Logs?

In your Java class:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyService {
Β  Β  private static final Logger logger = LoggerFactory.getLogger(MyService.class);

Β  Β  public void process() {
Β  Β  Β  Β  logger.info("Process started");
Β  Β  Β  Β  logger.debug("Debugging info");
Β  Β  Β  Β  logger.error("Something went wrong!");
Β  Β  }
}

βš™οΈ Configure Log Levels

You can set log levels in application.properties:

logging.level.root=INFO
logging.level.com.myapp=DEBUG
Levels (in order):
TRACE < DEBUG < INFO < WARN < ERROR < OFF

πŸ“‚ Log Output

By default, logs are shown in the console.

You can also log to a file:

logging.file.name=app.log
logging.file.path=logs/

βœ… Why Logging is Important

  • Helps debug issues in development & production
  • Keeps record of errors, warnings, and system behavior
  • Integrates easily with tools like ELK Stack, Splunk, or CloudWatch

πŸ“Œ Bonus: Change Log Format (optional)

logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
Back to blog

Leave a comment