Spring Boot – How to write console log to file

This article explains how to disable console log in spring boot. The Spring Boot application starts with a banner. The default logs shows in info mode in the console.

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

.   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |___, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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 parts, banner and logs. To disable console log, the banner and console log message must be written to a configured file.

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

1. Application.properties file

spring.main.banner-mode=log
logging.file=logs/test.log
logging.pattern.console=

2. Application.yml file

spring:
  main:
    banner-mode: "log"
logging:
  file: "logs/test.log"
  pattern:
    console:

3. SpringBootApplication main method

package com.yawintutor.log;

import java.util.Properties;

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

@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.file","logs/test.log");
		properties.setProperty("logging.pattern.console", "");
		application.setDefaultProperties(properties);
		application.run(args);
	}
}

4. As command line argument

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.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *