Spring Boot Logging with application.yml

Learn spring boot logging configuration via application.yml file in simple and easy-to-follow instructions. In the default structure of a Spring Boot web application, you can locate the application.yml file under the /resources folder.

Spring boot uses Logback as the default logging provider.

1. Default Logging with Spring Boot

To understand default spring boot logging, let’s put logs in spring boot hello world example. Do not configure anything application.yaml related to logging.

private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());

@RequestMapping("/")
public String home(Map<String, Object> model) {

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

	model.put("message", "HowToDoInJava Reader !!");
	return "index";
}

Start the application. Access the application in the browser and verify log messages in the console.

2017-03-02 23:33:51.318  INFO 3060 --- [nio-8080-exec-1] c.h.app.controller.IndexController : info log statement printed
2017-03-02 23:33:51.319  WARN 3060 --- [nio-8080-exec-1] c.h.app.controller.IndexController : warn log statement printed
2017-03-02 23:33:51.319 ERROR 3060 --- [nio-8080-exec-1] c.h.app.controller.IndexController : error log statement printed
  • The default logging level is INFO, so the debug log message is not present.
  • The default pattern is configured in different base configuration files.
%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta}
%clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan}
%clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}

The above pattern print these listed log message parts with respective color coding applied:

  • Date and Time — Millisecond precision.
  • Log Level — ERROR, WARN, INFO, DEBUG or TRACE.
  • Process ID.
  • A — separator to distinguish the start of actual log messages.
  • Thread name — Enclosed in square brackets (may be truncated for console output).
  • Logger name — This is usually the source class name (often abbreviated).
  • The log message

2. Set Log Level

When a message is logged via a Logger it is logged with a certain log level. In the application.yml file, you can define log levels of Spring Boot loggers, application loggers, Hibernate loggers, Thymeleaf loggers, and more. To set the logging level for any logger, add keys starting with logging.level.

The logging level can be one of one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. The root logger can be configured using logging.level.root.

logging:
  level:
  	root: ERROR
    org.springframework.web: ERROR
    com.howtodoinjava: DEBUG
    org.hibernate: ERROR

In the above configuration, I upgraded the log level for application classes to DEBUG (from default INFO). Now observe the logs:

2017-03-02 23:57:14.966 DEBUG 4092 --- [nio-8080-exec-1] c.h.app.controller.IndexController       : debug log statement printed
2017-03-02 23:57:14.967  INFO 4092 --- [nio-8080-exec-1] c.h.app.controller.IndexController       : info log statement printed
2017-03-02 23:57:14.967  WARN 4092 --- [nio-8080-exec-1] c.h.app.controller.IndexController       : warn log statement printed
2017-03-02 23:57:14.967 ERROR 4092 --- [nio-8080-exec-1] c.h.app.controller.IndexController       : error log statement printed

3. Set Log Pattern

To change the logging patterns, use logging.pattern.console and logging.pattern.file keys.

logging:
	pattern:
	    console: %d{yyyy-MM-dd HH:mm:ss} - %msg%n
	    file: %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n

After changing the console logging pattern in the application, log statements are printed as below:

2017-03-03 12:59:13 - This is a debug message
2017-03-03 12:59:13 - This is an info message
2017-03-03 12:59:13 - This is a warn message
2017-03-03 12:59:13 - This is an error message

4. Output to File Appender

To print the logs in the file, use logging.file.name or logging.file.path key.

logging:
	file.name: /logs/application-debug.log

If we want to configure multiple file appenders logging into different files then we must define them in either a logback.xml or a logback-spring.xml in the classpath. Spring Boot’s default YAML configuration only allows a single file appender.

5. Using Spring Profiles

It is desirable to have multiple configurations for any application – where each configuration is specific to a particular runtime environment. In spring boot, you can achieve this by creating multiple application-{profile}.yml files in same location as application.yml file.

Profile-specific keys always override the non-profile-specific ones. If several profiles are specified, a last-wins strategy applies.

If I have two environments for my application i.e. prod and dev. Then I will create two profile-specific yml files.

  • application-dev.yml
logging:
  file.name: logs/application-debug.log
  pattern:
    console: "%d %-5level %logger : %msg%n"
    file: "%d %-5level [%thread] %logger : %msg%n"
  level:
    org.springframework.web: ERROR
    com.howtodoinjava: DEBUG
    org.hibernate: ERROR
  • application-prod.yml
logging:
  file.name: logs/application-debug.log
  pattern:
    console: "%d %-5level %logger : %msg%n"
    file: "%d %-5level [%thread] %logger : %msg%n"
  level:
    org.springframework.web: ERROR
    com.howtodoinjava: INFO
    org.hibernate: ERROR

To supply profile information to the application, key spring.profiles.active is passed to runtime.

$ java -jar -Dspring.profiles.active=prod spring-boot-demo.jar

6. Color-coded Logs

If your terminal supports ANSI, the color output will be used to aid readability. You can set spring.output.ansi.enabled value to either ALWAYS, NEVER or DETECT.

spring:
	output:
	ansi:
		enabled: DETECT


Color coding is configured using the %clr conversion word. In its simplest form, the converter will color the output according to the log level.

  • FATAL and ERROR – Red
  • WARN – Yellow
  • INFO, DEBUG and TRACE – Green

Drop me your questions in the comments section.

Happy Learning !!

Comments

Subscribe
Notify of
guest
8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode