How to Set the Logging Level with application.properties?
Last Updated :
23 Apr, 2024
In Spring Boot, logging is crucial for monitoring, debugging, and troubleshooting Java applications. Spring Boot provides a convenient way to configure logging levels using properties files, particularly the application.properties file of the Spring application. This allows developers to control the amount of detail in logging messages without altering the application code.
Spring Boot uses the popular logging framework Logback by default. To set the logging level, we need to edit the application.properties file by specifying the name of the logger and the desired log level of the Spring application.
Key Terminologies:
- Logger: An object that records log messages in the software application. Loggers are typically associated with specific classes or packages and are used to categorize and manage log messages.
- Log Level: Indicates the severity or importance of the log message. Common log levels include:
- DEBUG: Shows detailed information for debugging purposes.
- INFO: Shows informational messages highlighting the progress of the application.
- WARN: Shows warnings indicating potential issues or unexpected behavior.
- ERROR: Indicates error messages for failures or problems requiring attention.
- TRACE: Provides very detailed information, more granular than DEBUG, often used for tracing program execution.
- Logging Framework: Library or tool used to implement logging functionality in software applications. Examples include Logback, SLF4J, Log4j, and java.util.logging.
- Appender: The part of the logging framework that is responsible for sending log messages to various locations such as consoles, files, databases, and remote servers.
- Pattern Layout:The format of the log message, including placeholders for log levels, timestamps, class names, and custom text..
- Log Configuration: Process of specifying logging settings such as log levels, appenders, and pattern layouts to control how log messages are generated and outputted.
- Root Logger: Default logger in the logging framework that captures all log messages not explicitly associated with specific loggers in the application.
- Logger Hierarchy: Structure of loggers organized based on their names, allowing for fine-grained control over logging settings at different levels of the application.
- Log Message: The text generated by the application during runtime, which typically contains information about events, errors, and actions.
- Log Rolling: The process of managing log files by rotating or hiding them based on criteria such as file size, time interval, or number of log entries.
Project Implementation to Set the logging level with application.properties
We can develop a simple Spring web application that allows setting the logging level via the application.properties file of the project.
Step 1: Create a spring project using spring initializer and add the below dependencies to the project.
Dependencies:
- Spring Web
- Spring Dev Tools
- Lombok
Once we create the spring project, the file structure will look like the image below.

Step 2: Create a new package named logDemo, in that package, create the new Java class and it named as LogExample.
Go to src > org.example.springlogdemo > logDemo > LogExample and put the below code.
Java
package org.example.springlogdemo.logDemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Controller class for logging example.
*/
@RestController
public class LogExample {
private static final Logger logger = LoggerFactory.getLogger(LogExample.class);
/**
* Endpoint to perform an action and log messages at different levels.
*/
@GetMapping("/performAction")
public void performAction() {
logger.debug("Debug message from LogExample");
logger.info("Info message from LogExample");
logger.warn("Warning message from LogExample");
logger.error("Error message from LogExample");
}
}
Step 3: Now, open the main class and write the below code.
Java
package org.example.springlogdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringLogDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringLogDemoApplication.class, args);
}
}
Step 4: Open the application.properties file and write the below code in the project for server port and logging configuration.
spring.application.name=spring-log-demo
server.port=8080
# Set logging level for the root logger
logging.level.root=INFO
# Set logging level for a specific package
logging.level.org.example.springlogdemo.LogDemo.LogExample=DEBUG
pom.xml:
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>spring-log-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-log-demo</name>
<description>spring-log-demo</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 5: After finishing setting up our Spring project, when we run it as a Spring application successfully, it will start running on port 8080.

Endpoint:
GET http://localhost:8080/performAction
Output:
We will not see any output related to logging because It's the console output of our Spring application.

Logger info:

In the above example,
- We have the LogExample class which can handles the HTTP requests and its methods containing the own logging statements.
- In the application.properties file we can set the root logger INFO level and configure the DEBUG level logging for the org.example.springlogdemo package which can encompasses all the classes in our application.
- It can ensure that all the classes in the org.example.springlogdemo will the log messages at the DEBUG level and the root logger and other classes outside the package will log messages at the INFO level of the application.
If we follow the above steps, then we can successfully demonstrate the logging level with application.properties in the Spring applications.
Similar Reads
Spring Boot - Application Properties
Spring Boot is built on top of the Spring Framework and includes all its features while simplifying configuration and setup. It has become a favorite among developers because it provides a rapid, production-ready environment that allows them to focus on business logic instead of dealing with complex
3 min read
How to Setup Central Logging Server with Rsyslog in Linux
This article will show us how to use Rsyslog to set up Linux as a centralized logging service. When managing logs from various systems, a centralized Rsyslog setup is advantageous. All log entries from client servers will be sent to the host server, which will allow them to be monitored and preserve
4 min read
How to Access Values From application.properties in Spring Boot?
In a Spring Boot application, application.properties is the central repository for defining configuration properties. These properties are accessible across the application to customize behavior. Accessing values from application.properties is a common requirement in Spring Boot projects. Spring Boo
3 min read
Add Build Properties to a Spring Boot Application
Spring Boot framework is an open-source framework for developing web applications with high performance and the Spring Boot makes it much easier to develop Spring-based applications developed by Java programming In this article, we will learn how to add build properties to a Spring Boot Application.
4 min read
Setting the Log Level in Spring Boot When Testing
When developing applications with Spring Boot, controlling log output is essential for diagnosing issues and understanding application behavior, especially during testing phases. Setting the log level allows developers to specify the type of information logged by the application, ranging from verbos
4 min read
What are the Reasons For the Exit in Android Application?
Developing Android Apps can be a tedious job, and frustrating too if our app is exiting unknowingly! In order to log exits in Android, we rely on a number of third-party libraries. With Android R, we can now log the exits in our app, which can assist us in resolving any issues that may arise. In thi
5 min read
How to put the y-axis in logarithmic scale with Matplotlib ?
In graphs, a logarithmic scale helps when numbers grow very fast, like 10, 100, 1000, and so on. Normally, in a graph, numbers increase by the same amount (like 1, 2, 3...), but in a logarithmic scale, they increase by multiplying (like 10, 100, 1000...). This makes it easier to see patterns in real
3 min read
How to Print to the Console in Android Studio?
In computer technology, the console is simply a combination of a monitor and an input device. Generally, the input device is referred to here as the pair of mouse and keyboard. In order to proceed with the topic, we have to understand the terminology of computer science, which is an important part o
6 min read
Hibernate - Logging By Log4j Using Properties File
Apache log4j is a java-based logging utility. Apache log4j role is to log information to help applications run smoothly, determine whatâs happening, and debug processes when errors occur. log4j may logs login attempts (username, password), submission form, and HTTP headers (user-agent, x-forwarded-h
2 min read
Capture File Properties Dialog in Wireshark
In Wireshark, after capturing some traffic of a network we can save the capture file on our local device so that it can be analyzed thoroughly in the future. We can save captured packets by using the File â Save or File â Save Asâ¦â menu items. Sometimes we need the details and properties of the capt
2 min read