In this article, we’ll see about writing a console log to file using the spring boot logging framework. The Spring Boot application logs begins with a banner. The default logs are seen in the info mode in the console. The spring boot logging framework supports to redirect log to file using spring boot configurations.

When developing large application, showing logs in console is a security issue. Logs must be redirected to log file or disable the console log. Spring boot supports easy way of writing console log to file

Console Log

The following logs are example logs that are seen when the spring boot application begins. We’re expected to save log to file. Otherwise, logs would be deleted from the console browser.

.   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |___, | / / / /
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-02-09 16:34:44.942  INFO 3972 --- [           main] l.SpringBootDisableConsoleLogApplication : Starting SpringBootDisableConsoleLogApplication on YawinLaptop1-PC with PID 3972 (D:stsworkspaceSpringBootDisableConsoleLogtargetclasses started by Karthik in D:stsworkspaceSpringBootDisableConsoleLog)
2019-02-09 16:34:44.947  INFO 3972 --- [           main] l.SpringBootDisableConsoleLogApplication : No active profile set, falling back to default profiles: default
2019-02-09 16:34:49.114  INFO 3972 --- [           main] l.SpringBootDisableConsoleLogApplication : Started SpringBootDisableConsoleLogApplication in 10.461 seconds (JVM running for 20.27)

Steps to write console log to file in Spring Boot

Console logs have two sections, banners and logs. The banner will be shown when the spring boot application begins. The spring boot logging framework logs will be shown as the spring boot loads the application. To delete logs from the console window, the banner and console log message must be written to the configured file.

Here, we’ll see different ways to write the spring boot console log to a configured log file.


The simplest way to write a console log to a file is to configure in the file. The path for the file is src/main/resources. The following log configuration would delete all the banner and logs from the console and write the log to the file.


2. Application.yml file

The other way to write a console log to a file is to configure in the application.yml file. The path for the application.yml file is src/main/resources. The following log configuration should be added in yaml file to delete all the banner and logs from the console and write the log to the file.

    banner-mode: "log"
  file: "logs/test.log"

3. SpringBootApplication main method

The console logs can be moved to file by configuring the logger in the program. The spring boot logging framework api supports to configure to redirect log to file.

package com.yawintutor.log;

import java.util.Properties;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootWriteConsoleLogApplication {

	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(SpringBootWriteConsoleLogApplication.class);
		Properties properties = new Properties();
		properties.setProperty("spring.main.banner-mode", "log");
		properties.setProperty("logging.pattern.console", "");

4. As command line argument

If the spring boot application is built and jar is created, the spring boot logging can be configured using the command line arguments. The command line argument to write log to file is as shown below.

java -Dspring.main.banner-mode=log -Dlogging.pattern.console= -jar logging.file=logs/test.log SpringBootWriteConsoleLogApplication-0.0.1.jar

The location of the log file is logs / test.log. The console banner and console logs are written in this log file instead of in a console.

Leave a Reply