In java & spring boot, we need to disable console logging for security reason. The Spring Boot application starts with a banner. The default logs shows in info mode in the console log. The spring boot supports enable and disable console log, info and debug messages should be disabled in console logging and redirected to a log file. The application properties and log back is used to enable and disable the console logging in spring boot application.

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);
	}
}



Leave a Reply