Log formatting configuration helps make log messages clear and easy to read. It organizes information like time, level, and message in a consistent way.
Log formatting configuration in Spring Boot
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{36} - %msg%nThis example shows a common pattern for console logs in Spring Boot.
Use placeholders like %d for date, %level for log level, %thread for thread name, %logger for class, and %msg for the message.
logging.pattern.console=%d{HH:mm:ss} %-5level - %msg%nlogging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{20} - %msg%nlogging.pattern.file=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%nThis Spring Boot app logs two messages with a custom format showing date, level, thread, logger, and message.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class LogFormatExampleApplication { private static final Logger logger = LoggerFactory.getLogger(LogFormatExampleApplication.class); public static void main(String[] args) { SpringApplication.run(LogFormatExampleApplication.class, args); logger.info("Application started successfully."); logger.error("An error occurred."); } } # application.properties logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{36} - %msg%n
Use logging.pattern.console for console log format and logging.pattern.file for file logs.
Make sure your pattern includes %n at the end to add a new line after each log message.
Test your log format by running the app and checking the console or log files.
Log formatting makes logs easier to read and understand.
Use placeholders like %d, %level, %thread, %logger, and %msg to customize logs.
Configure patterns in application.properties for console and file outputs.