Spring Boot – How to disable console log

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 disabling console logs

.   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |___, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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 Disable console logs in Spring Boot

Console logs have two parts, banner and logs. To disable the console log, we must disable the banner and block the logger message.

Here, we’ll see different ways to disable the spring boot console log.

1. Application.properties file

spring.main.banner-mode=off
logging.pattern.console=

2. Application.yml file

spring:
  main:
    banner-mode: "off"
logging:
  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 SpringBootDisableConsoleLogApplication {

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

}

4. As command line argument

java -Dspring.main.banner-mode=off -Dlogging.pattern.console= -jar SpringBootDisableConsoleLog-0.0.1.jar

5. SpringBootApplication main method (Banner only)

package com.yawintutor.log;

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

@SpringBootApplication
public class SpringBootDisableConsoleLogApplication {

	public static void main(String[] args) {
		SpringApplication application = new SpringApplication(SpringBootDisableConsoleLogApplication.class);
		application.setBannerMode(Banner.Mode.OFF);
		application.run(args);
	}
}

Related Articles

Leave a Reply

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