Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
Logging is a critical component of any application. Proper logging can aid in debugging, monitoring, and understanding the flow and state of an application. Spring Boot provides a default logging configuration via the Spring Boot Starter Logging, which includes the SLF4J API and its implementation Logback.
Let's explore the different facets of logging with Spring Boot:
With the spring-boot-starter-logging
dependency (included by default with spring-boot-starter-web
), Spring Boot configures Logback to log to the console and also offers a file-based logging option. By default, logs will have the format %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
.
You can customize the logging level for your entire application or for specific packages or classes in the application.properties
or application.yml
file:
# Setting the global logging level logging.level.root=WARN # Setting the logging level for a specific package logging.level.org.springframework.web=DEBUG logging.level.com.example.demo=TRACE
To log to a file, set the logging.file or logging.path properties:
# Log to a specific file logging.file=myapp.log # Or, specify a directory to log in, Spring Boot will create a spring.log file inside this directory logging.path=/var/log
Spring Boot 2.2+ provides the concept of log groups, which are predefined sets of loggers that can be configured together for convenience. For example:
logging.group.web=org.springframework.core.codec, org.springframework.http logging.level.web=DEBUG
To customize the format of the log, you can define properties like:
logging.pattern.console= %d{HH:mm:ss} - %msg%n logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
You can have different logging configurations for different profiles. For example, you might want verbose logging in a development
profile but concise logging in production
.
Create a logback-spring.xml
in src/main/resources
:
<configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <jmxConfigurator/> <springProfile name="development"> <!-- configuration for development --> </springProfile> <springProfile name="production"> <!-- configuration for production --> </springProfile> </configuration>
For modern applications, especially those that run inside containers and are monitored by tools like the ELK stack (Elasticsearch, Logstash, Kibana), it's beneficial to log in a structured format like JSON. You can use libraries like logstash-logback-encoder
to produce JSON logs.
Though Spring Boot uses Logback by default, you can switch to another system (like Log4j2) by excluding the default logging and adding your preferred logging starter.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
Logging configurations and setups can get complex, especially for larger applications. Still, the above guide offers an introduction and overview of the primary facets of logging with Spring Boot.
Configuring logging in Spring Boot:
// Application properties for logging logging.level.root=INFO
Using SLF4J and Logback in Spring Boot:
<!-- Dependency in pom.xml --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
Logging levels and categories in Spring Boot:
// Logging in a class import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MyClass { private static final Logger logger = LoggerFactory.getLogger(MyClass.class); public void someMethod() { logger.debug("Debug message"); logger.info("Info message"); logger.warn("Warning message"); logger.error("Error message"); } }
Customizing log formats in Spring Boot:
<!-- Logback configuration in src/main/resources/logback.xml --> <configuration> <property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n" /> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>${LOG_PATTERN}</pattern> </encoder> </appender> <root level="INFO"> <appender-ref ref="STDOUT" /> </root> </configuration>
Logging with different appenders in Spring Boot:
<!-- Logback with File Appender --> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>logs/application.log</file> <encoder> <pattern>${LOG_PATTERN}</pattern> </encoder> </appender>
Debugging and troubleshooting with Spring Boot logs:
// Example of debugging with logs public class MyService { private static final Logger logger = LoggerFactory.getLogger(MyService.class); public void performOperation() { logger.debug("Entering performOperation method"); // Your code logic logger.debug("Exiting performOperation method"); } }
Integration of logging frameworks like Log4j in Spring Boot:
<!-- Dependency for Log4j --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>