Default Logging Format in Spring Boot
2016-07-24 20:31:45.280 INFO 10220 --- [ main] c.d.SpringBootHelloWorldApplication : Starting SpringBootHelloWorldApplication on NODTBSL206AG with PID 10220 (started by Dinesh.Rajput in D:personal dataspring-boot-workspaceSpringBootHelloWorld) 2016-07-24 20:31:45.286 INFO 10220 --- [ main] c.d.SpringBootHelloWorldApplication : No active profile set, falling back to default profiles: default 2016-07-24 20:31:45.502 INFO 10220 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1809907: startup date [Sun Jul 24 20:31:45 IST 2016]; root of context hierarchy 2016-07-24 20:31:48.201 INFO 10220 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2016-07-24 20:31:48.226 INFO 10220 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2016-07-24 20:31:48.228 INFO 10220 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.3 2016-07-24 20:31:48.485 INFO 10220 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2016-07-24 20:31:48.485 INFO 10220 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2993 ms
- Date and Time: Millisecond precision and easily sort able.
- 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).
Create Spring Boot Maven Project
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dineshonjava</groupId>
<artifactId>SpringBootHelloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBootHelloWorld</name>
<description>SpringBootHelloWorld project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RC1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.dineshonjava;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SpringBootHelloWorldApplication {
private static final Logger logger = LoggerFactory.getLogger(SpringBootHelloWorldApplication.class);
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(SpringBootHelloWorldApplication.class, args);
logger.error("Message logged at ERROR level");
logger.warn("Message logged at WARN level");
logger.info("Message logged at INFO level");
logger.debug("Message logged at DEBUG level");
}
}
2016-07-24 20:57:40.828 ERROR 3024 --- [ main] c.d.SpringBootHelloWorldApplication : Message logged at ERROR level 2016-07-24 20:57:40.828 WARN 3024 --- [ main] c.d.SpringBootHelloWorldApplication : Message logged at WARN level 2016-07-24 20:57:40.828 INFO 3024 --- [ main] c.d.SpringBootHelloWorldApplication : Message logged at INFO level
Customizing default Configuration for Logging:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- Log message format -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<!-- Setting the root level of logging to INFO -->
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
- We are using a appender STDOUT using ConsoleAppender which prints to the console
- We are giving pattern to the appender to build the log message
- Set up a root logger which logs any message above INFO level using the STDOUT appender

21:14:46.352 [main] ERROR c.d.SpringBootHelloWorldApplication - Message logged at ERROR level 21:14:46.352 [main] WARN c.d.SpringBootHelloWorldApplication - Message logged at WARN level 21:14:46.352 [main] INFO c.d.SpringBootHelloWorldApplication - Message logged at INFO level
Printing Logs into a File:
- If No logging.path and No logging.file then Console only logging.
- If No logging.path and Specify logging.file then writes to the specified log file. Names can be an exact location or relative to the current directory.
- If Specify logging.path and No logging.file then writes spring.log to the specified directory. Names can be an exact location or relative to the current directory.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- Log message format -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- Need appender to write to file -->
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<!-- Name of the file where the log messages are written -->
<file>MySpringBootApp.log</file>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.dineshonjava.service" level="warn">
<appender-ref ref="FILE" />
</logger>
<!-- Setting the root level of logging to INFO -->
<root level="info">
<appender-ref ref="FILE" />
</root>
</configuration>
package com.dineshonjava.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloService {
private Logger logger = LoggerFactory.getLogger(HelloService.class);
public void service(){
logger.info("Message at INFO level from HelloService.service()");
logger.warn("Message at WARN level from HelloService.service()");
}
}

Custom log configuration
- Logback -> logback-spring.xml, logback-spring.groovy, logback.xml or logback.groovy
- Log4j2 -> log4j2-spring.xml or log4j2.xml
- JDK (Java Util Logging) -> logging.properties
- logging.file -> Used in default log configuration if defined for logging file name.
- logging.path -> Used in default log configuration if defined for logging file path of directory.
- logging.pattern.console -> The log pattern to use on the console.
- logging.pattern.file -> The log pattern to use in a file.
- logging.pattern.level -> The format to use to render the log level.
- logging.exception-conversion-word -> The conversion word that’s used when logging exceptions.
Summary
Spring Boot Related Topics
- Introduction to Spring Boot
- Essentials and Key Components of Spring Boot
- Spring Boot CLI Installation and Hello World Example
- Spring Boot Initializr Web Interface
- Spring Boot Initializr With IDEs
- Spring Boot Initializr With Spring Boot CLI
- Installing Spring Boot
- Developing your first Spring Boot application
- External Configurations for Spring Boot Applications
- Spring Boot and Spring MVC
- Working with SQL Databases and Spring Boot
- MySQL Configurations
- Spring Data JPA using Spring Boot Application
- Spring Boot with NoSQL technologies
- Spring Cache Tutorial
- Spring Security Tutorial with Spring Boot
- Spring Boot and MongoDB in REST Application
- Complete Guide for Spring Boot Actuator
- Microservices with Spring Boot



