HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring Boot / Spring Boot Logging with application.yml

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.

Read More: Spring Boot Logging with application.properties
Table of Contents

Understand default spring boot logging
Set logging level
Set logging pattern
Set logging output to file
Using active profiles to load environment specific logging configuration
Color-coded logging output

Understand default spring boot logging

To understand default spring boot logging, lets put logs in spring boot hello world example. Just to mention, there is no logging related configuration in application.yml file. If you see any configuration in downloaded application, please remove it.

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 application at browser and verify log messages in 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

Note down the observation that Default logging level is INFO – because debug log message is not present.
There is fixed default log message pattern which 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

Set logging 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.

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 above configuration, I upgraded 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

Set logging pattern

To change the loging 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 console logging pattern in 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

Set logging output to file

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

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

Verify the logs in file.

2017-03-03 13:02:50.608 DEBUG 10424 --- [http-nio-8080-exec-1] c.h.app.controller.IndexController       : This is a debug message
2017-03-03 13:02:50.608  INFO 10424 --- [http-nio-8080-exec-1] c.h.app.controller.IndexController       : This is an info message
2017-03-03 13:02:50.608  WARN 10424 --- [http-nio-8080-exec-1] c.h.app.controller.IndexController       : This is a warn message
2017-03-03 13:02:50.609 ERROR 10424 --- [http-nio-8080-exec-1] c.h.app.controller.IndexController       : This is an error message

Using active profiles to load environment specific logging configuration

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: 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: 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 application, key spring.profiles.active is passed to runtime.

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

Color-coded logging output

If your terminal supports ANSI, 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 comments section.

Happy Learning !!

References:

Spring Boot – How to logging
Spring boot features – Logging

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Madhavan Ananthan

    June 26, 2020

    Hello Guys,
    I need to split the log files after executed for every modules in application.yml file.
    For example: I’m having 5 modules, when i run the first module separate log file should create and if i run 3rd module separate log file should create, but now all the logs are adding on the same log file.

    Here the code for logging configuration
    ——————————————————————————–
    spring:
    application:
    name: RevenueValidationEngine
    profiles:
    active: test,GRT
    #main:
    #web-application-type: none

    # Loggging configuration
    logging:
    level:
    root: INFO
    com.fedex: DEBUG
    pattern:
    console: “%-5level %d [%X{context}]- %msg%n”
    #file: “%-5level %d [%X{context}]- %msg%n”
    file: logs/${spring.application.name}/${spring.application.name}.log

    ————————————————————– Please respond ASAP, Thanks

  2. Alexis LEGROS

    July 19, 2018

    Hi there,

    Thanks for this great post!

    Just a minor fix on the url to Spring Boot base configuration files, it’s now available at https://github.com/spring-projects/spring-boot/tree/master/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback

    Best regards from France!
    Alex.

    • Lokesh Gupta

      July 19, 2018

      Thanks you Alex for your time in reporting this. Much appreciated!!

  3. elvin

    December 22, 2017

    i wander how to config the yml .
    i use mybatis as my dataSource frame and i want to print sql on my console , can anyone help me ?

  4. Carlos

    August 22, 2017

    Hi! how can i configure two differents log files ( one for info lv and one for error lv ) using YAML?

Comments are closed on this article!

Search Tutorials

Spring Boot Logging

  • Spring Boot – Guide to Logging
  • Spring Boot – log4j2.properties
  • Spring Boot – log4j2.xml
  • Spring Boot – application.yml
  • SB – application.properties
  • Spring Boot – Console log
  • Spring Boot – Performance Log
  • Spring Boot – Multiple log files
  • Spring Boot – Embed server logs
  • SB – Active profile logging
  • SB – Logging with Lombok

Spring Boot 2 Tutorial

  • Spring Boot – Introduction
  • Spring Boot – Starter parent
  • Spring Boot – Starter templates
  • Spring Boot – Multi-module project
  • Spring Boot – Annotations
  • Spring Boot – Auto configuration
  • Spring Boot – AOP
  • Spring Boot – Logging
  • Spring Boot – DevTools
  • Spring Boot – WAR Packaging
  • Spring Boot – REST API
  • Spring Boot – CRUD
  • Spring Boot – OAuth2
  • Spring Boot – Testing
  • Spring Boot – RestTemplate
  • Spring Boot – Thymeleaf
  • Spring Boot – Hibernate
  • Spring Boot – DataSource
  • Spring Boot – Error Handling
  • Spring Boot – Caching
  • Spring Boot – Retry
  • Spring Boot – BasicAuth
  • Spring Boot – H2 Database
  • Spring Boot – Ehcache 3.x
  • Spring Boot – Gson
  • Spring Boot – RMI
  • Spring Boot – Send Email
  • Spring Boot – Interview Questions

Spring Boot Tutorial

  • Spring Boot – CommandLineRunner
  • Spring Boot – Configure Jetty
  • Spring Boot – Tomcat Default Port
  • Spring Boot – Context Root
  • Spring Boot – SSL [https]
  • Spring Boot – Get all loaded beans
  • Spring Boot – PropertyEditor
  • Spring Boot – @EnableScheduling
  • Spring Boot – Jersey
  • Spring Boot – SOAP Webservice
  • Spring Boot – SOAP Client
  • Spring Boot – JMSTemplate
  • Spring Boot – REST APIs
  • Spring Boot – JSP View
  • Spring Boot – Actuator endpoints
  • Spring Boot – Role Based Security
  • Spring Boot – RSS / ATOM Feed
  • Spring Boot – Ehcache 2.x

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)