Different Log Levels for File and Console in Spring Boot
In Spring Boot applications, logging plays a critical role in monitoring and troubleshooting. By default, Spring Boot uses Logback as the logging framework and applies the same log level configuration for both console and file appenders. However, in many real-world scenarios, developers need different log levels for the console and file outputs. Let’s explore how Spring Boot configures different log levels with file and console appenders.
1. What is Logback?
Logback is a powerful and flexible logging framework for Java applications. It was created as a successor to Log4j by the same developer (Ceki Gülcü). Logback is widely used in enterprise applications and is the default logging framework in Spring Boot.
1.1 Key Features of Logback
- High Performance: Logback is faster and more efficient compared to older logging frameworks like Log4j.
- Rich Configuration: Supports XML-based configuration with powerful options for appenders, encoders, and filters.
- Appender Support: Can log messages to multiple destinations such as the console, files, databases, or remote servers.
- Rolling Policy: Allows automatic rotation of log files based on size or date.
- Integration with SLF4J: Logback is built on
SLF4J(Simple Logging Facade for Java), making it easy to plug into various Java projects.
1.2 Architecture of Logback
Logback is divided into three modules:
logback-core: The base module that provides the foundation for other modules.logback-classic: An implementation of theSLF4JAPI, fully compatible with existing log4j applications.logback-access: Integrates with servlet containers to provide HTTP access logging.
2. Code Example
2.1 Add Dependencies (pom.xml)
To demonstrate, create a Spring Boot application using Spring Initializr or your preferred IDE. Add the following dependencies in pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
</dependencies>
Now create a custom logback-spring.xml in the src/main/resources directory.
2.2 logback-spring.xml
<configuration>
<!-- Console Appender -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- File Appender -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/app.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- Logger for Console (INFO and above) -->
<logger name="com.example" level="INFO" additivity="false">
<appender-ref ref="CONSOLE" />
</logger>
<!-- Root logger for File (DEBUG and above) -->
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
The given logback-spring.xml configuration defines two appenders: a console appender and a rolling file appender. The console appender prints logs to the console in a specific format showing timestamp, thread, log level, logger name, and message, while the file appender writes logs to logs/app.log and rotates daily, keeping logs for the last 7 days. A logger is configured for the package com.example at INFO level so that only INFO and higher-level messages are displayed in the console, whereas the root logger is set at DEBUG level, ensuring all debug and above messages are written to the file. This setup allows different log levels for console and file outputs, keeping the console clean while maintaining detailed logs in the file.
2.3 Creating a Controller Class
In this step, we create a simple Spring Boot REST controller that demonstrates how logging works inside an endpoint.
package com.example.loggingdemo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class HelloController {
private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
@GetMapping("/hello")
public String hello() {
logger.debug("Debugging /hello endpoint");
logger.info("Handling /hello request");
return "Hello, World!";
}
}
In the above code, the HelloController class exposes a /hello endpoint. When this endpoint is hit, a debug message and an info message are logged using the SLF4J Logger. Depending on the logging configuration, the debug message will appear only in the file logs, while the info message will be shown in both console and file logs.
2.4 Creating a Main Class
Next, we create the main Spring Boot application class, which initializes the application and logs messages at various levels.
package com.example.loggingdemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LoggingDemoApplication {
private static final Logger logger = LoggerFactory.getLogger(LoggingDemoApplication.class);
public static void main(String[] args) {
SpringApplication.run(LoggingDemoApplication.class, args);
logger.trace("This is a TRACE message");
logger.debug("This is a DEBUG message");
logger.info("This is an INFO message");
logger.warn("This is a WARN message");
logger.error("This is an ERROR message");
}
}
In this class, the main method starts the Spring Boot application and then logs messages at all levels (TRACE, DEBUG, INFO, WARN, and ERROR). Based on the logback-spring.xml configuration, trace and debug logs will be written only to the log file, while info, warn, and error logs will appear in both console and file outputs. This demonstrates how different log levels can be managed for console and file appenders in Spring Boot.
2.5 Code Run and Output
After completing the controller and main class setup along with the logback-spring.xml configuration, run the Spring Boot application using your IDE or the command:
mvn spring-boot:run
Once the application starts, you will see log messages in the console and in the log file (logs/app.log).
2025-09-05 07:00:00 [main] TRACE com.example.loggingdemo.LoggingDemoApplication - This is a TRACE message 2025-09-05 07:00:00 [main] DEBUG com.example.loggingdemo.LoggingDemoApplication - This is a DEBUG message 2025-09-05 07:00:00 [main] INFO com.example.loggingdemo.LoggingDemoApplication - This is an INFO message 2025-09-05 07:00:00 [main] WARN com.example.loggingdemo.LoggingDemoApplication - This is a WARN message 2025-09-05 07:00:00 [main] ERROR com.example.loggingdemo.LoggingDemoApplication - This is an ERROR message
Since we configured INFO level for the console and DEBUG level for the file, the outputs will differ as shown below.
2025-09-05 07:00:00 [main] INFO com.example.loggingdemo.LoggingDemoApplication - This is an INFO message 2025-09-05 07:00:00 [main] WARN com.example.loggingdemo.LoggingDemoApplication - This is a WARN message 2025-09-05 07:00:00 [main] ERROR com.example.loggingdemo.LoggingDemoApplication - This is an ERROR message
3. Conclusion
Configuring different log levels for console and file appenders in Spring Boot provides flexibility and efficiency. Developers can keep the console output clean while still capturing detailed logs in files for debugging and auditing. This approach is widely used in production environments where concise console logs and verbose file logs are both required.




