Spring Boot logging with tinylog

Tags: tinylog

In this Java Spring Boot tutorial we learn how to exclude the default Spring Boot logging dependency and using tinylog library for logging in Spring Boot web application.

Table of contents

  1. Create Spring Boot Web Application
  2. Exclude Spring Boot Default Logging Dependencies
  3. Add tinylog dependencies to the Spring Boot web project
  4. Write log entries with tinylog in Spring Boot
  5. How to configure tinylog
  6. Download Complete Source Code

Create Spring Boot Web Application

Open IntelliJ IDEA, select the menu File > New > Project. (or click on New Project button at IntelliJ Welcome dialog)

On the New Project dialog, select Spring Initializr and click Next button.

Spring Boot logging with tinylog

On the Spring Initializr Project Settings dialog input the new project information as below and click Next button.

  • Group: dev.simplesolution
  • Artifact: spring-boot-tinylog-example
  • Type: Gradle
  • Language: Java
  • Version: 1.0.0
  • Name: spring-boot-tinylog-example
  • Description: Spring Boot logging with tinylog library
  • Package: dev.simplesolution.tinylogexample

Spring Boot logging with tinylog

On the Dependencies dialog, select below dependencies and click Next button.

  • Spring Web

Spring Boot logging with tinylog

Select the location for your project and click Finish button to create new Spring Boot project.

Spring Boot logging with tinylog

Exclude Spring Boot Default Logging Dependencies

First step, we need to exclude the spring-boot-starter-logging dependencies which is default logging library of Spring Boot.

To exclude spring-boot-starter-logging dependencies with Gradle build project we can update dependencies in build.gradle file as below.

implementation('org.springframework.boot:spring-boot-starter-web') {
	exclude group: 'org.springframework.boot', module:'spring-boot-starter-logging'
}

testImplementation ('org.springframework.boot:spring-boot-starter-test') {
	exclude group: 'org.springframework.boot', module:'spring-boot-starter-logging'
}

To exclude spring-boot-starter-logging dependencies with Gradle build project we can update dependencies in pom.xml file as below.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</exclusion>
	</exclusions>
</dependency>

Add tinylog dependencies to the Spring Boot web project

At this step we add tinylog dependencies to the Spring Boot web project.

To use tinylog library in the Gradle project, add the following dependency to the build.gradle file.

implementation group: 'org.tinylog', name: 'tinylog-api', version: '2.4.1'
implementation group: 'org.tinylog', name: 'tinylog-impl', version: '2.4.1'
implementation group: 'org.tinylog', name: 'slf4j-tinylog', version: '2.4.1'
implementation group: 'org.tinylog', name: 'jcl-tinylog', version: '2.4.1'
implementation group: 'org.tinylog', name: 'log4j1.2-api', version: '2.4.1'

If you are using Maven build, add the following tinylog dependencies to the pom.xml file.

<dependency>
    <groupId>org.tinylog</groupId>
    <artifactId>tinylog-api</artifactId>
    <version>2.4.1</version>
</dependency>
<dependency>
    <groupId>org.tinylog</groupId>
    <artifactId>tinylog-impl</artifactId>
    <version>2.4.1</version>
</dependency>
<dependency>
    <groupId>org.tinylog</groupId>
    <artifactId>slf4j-tinylog</artifactId>
    <version>2.4.1</version>
</dependency>
<dependency>
    <groupId>org.tinylog</groupId>
    <artifactId>jcl-tinylog</artifactId>
    <version>2.4.1</version>
</dependency>
<dependency>
    <groupId>org.tinylog</groupId>
    <artifactId>log4j1.2-api</artifactId>
    <version>2.4.1</version>
</dependency>

To have more information about the tinylog library you can visit the project home page at tinylog.org

Write log entries with tinylog in Spring Boot

Create a new Java class named TestLogging as below to verify the log entries be written by tinylog.

java/dev/simplesolution/tinylogexample/TestLogging.java

package dev.simplesolution.tinylogexample;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class TestLogging implements CommandLineRunner {
    private Logger logger = LoggerFactory.getLogger(TestLogging.class);

    @Override
    public void run(String... args) throws Exception {
        logger.trace("Hello tinylog");
        logger.info("Hello tinylog");
        logger.debug("Hello tinylog");
        logger.warn("Hello tinylog");
        logger.error("Hello tinylog");
    }
}

Run the Spring Boot application we will get a lot of log messages as the default severity level config of tinylog is trace level.

....
TRACE: Returning cached instance of singleton bean 'applicationAvailability'
2022-04-07 22:25:12 [main] org.springframework.boot.availability.ApplicationAvailabilityBean.onApplicationEvent()
DEBUG: Application availability state LivenessState changed to CORRECT
2022-04-07 22:25:12 [main] org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean()
TRACE: Returning cached instance of singleton bean 'testLogging'
2022-04-07 22:25:12 [main] dev.simplesolution.tinylogexample.TestLogging.run()
TRACE: Hello tinylog
2022-04-07 22:25:12 [main] dev.simplesolution.tinylogexample.TestLogging.run()
INFO: Hello tinylog
2022-04-07 22:25:12 [main] dev.simplesolution.tinylogexample.TestLogging.run()
DEBUG: Hello tinylog
2022-04-07 22:25:12 [main] org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean()
TRACE: Returning cached instance of singleton bean 'springApplicationAdminRegistrar'
2022-04-07 22:25:12 [main] org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean()
TRACE: Returning cached instance of singleton bean 'springApplicationAdminRegistrar'
2022-04-07 22:25:12 [main] org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean()
TRACE: Returning cached instance of singleton bean 'applicationAvailability'
2022-04-07 22:25:12 [main] org.springframework.boot.availability.ApplicationAvailabilityBean.onApplicationEvent()
DEBUG: Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
2022-04-07 22:25:12 [main] dev.simplesolution.tinylogexample.TestLogging.run()
WARN: Hello tinylog
2022-04-07 22:25:12 [main] dev.simplesolution.tinylogexample.TestLogging.run()
ERROR: Hello tinylog

How to configure tinylog

To configure tinylog we add a new file under resources directory named tinylog.properties

For example the following configuration to update the message format and change log severity level to info.

resources/tinylog.properties

writer.format = {date: yyyy-MM-dd HH:mm:ss.SSS} {level} {class}.{method} {message}
writer.level  = info

Run the Spring Boot application again we will get the log entries as below.

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.6)

2022-04-07 22:31:50.379 INFO org.springframework.boot.StartupInfoLogger.logStarting Starting SpringBootTinylogExampleApplication using Java 1.8.0_231 on SimpleSolution with PID 6172 (D:\SimpleSolution\spring-boot-tinylog-example\build\classes\java\main started by HP in D:\SimpleSolution\spring-boot-tinylog-example)
2022-04-07 22:31:50.382 INFO org.springframework.boot.SpringApplication.logStartupProfileInfo No active profile set, falling back to 1 default profile: "default"
2022-04-07 22:31:50.918 INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize Tomcat initialized with port(s): 8080 (http)
Apr 07, 2022 10:31:50 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
Apr 07, 2022 10:31:50 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.60]
Apr 07, 2022 10:31:51 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring embedded WebApplicationContext
2022-04-07 22:31:51.030 INFO org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.prepareWebApplicationContext Root WebApplicationContext: initialization completed in 620 ms
2022-04-07 22:31:51.244 INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer.start Tomcat started on port(s): 8080 (http) with context path ''
2022-04-07 22:31:51.250 INFO org.springframework.boot.StartupInfoLogger.logStarted Started SpringBootTinylogExampleApplication in 1.107 seconds (JVM running for 2.601)
2022-04-07 22:31:51.251 INFO dev.simplesolution.tinylogexample.TestLogging.run Hello tinylog
2022-04-07 22:31:51.251 WARN dev.simplesolution.tinylogexample.TestLogging.run Hello tinylog
2022-04-07 22:31:51.251 ERROR dev.simplesolution.tinylogexample.TestLogging.run Hello tinylog

Download Complete Source Code

The source code in this article can be found at: github.com/simplesolutiondev/spring-boot-tinylog-example

or clone at:

git clone https://github.com/simplesolutiondev/spring-boot-tinylog-example.git

or download at:

Download Source Code

Happy Coding 😊

Java tinylog Tutorial

Java tinylog Rolling File Writer

Java tinylog with Multiple Writers

Java tinylog Tags to Different Log Files