tinylog (https://tinylog.org/v2/), like various other things that start with tiny, is a lightweight open source logging solution. It itself contains only two JAR files (one for the API and the other for the implementation) and does not have any external dependencies. The total size of the two JAR files is only 178KB.

Although it is a lightweight level solution , but we commonly used the basic log management features are very complete , it has a similar API design with other popular logging framework , a variety of configurable log output options , performance is also very impressive (this is the official Benchmark (https://tinylog.org/v2/benchmark/).

Today we’ll learn how to use tinylog logging in Spring Boot.

Integration tinylog

This can be summarized in the following steps.

  1. exclude the default Spring Boot logging framework dependencies
  2. introduce the logging framework dependencies to be used
  3. add the configuration file of the new logging framework

Okay, let’s follow this step by step.

Step 1: Exclude Spring Boot default logging framework dependencies

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<dependencies>
    <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>
</dependencies>

Step 2: Import tinylog’s dependencies

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<properties>
    <tinylog.version>2.4.1</tinylog.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.tinylog</groupId>
        <artifactId>tinylog-api</artifactId>
        <version>${tinylog.version}</version>
    </dependency>
    <dependency>
        <groupId>org.tinylog</groupId>
        <artifactId>tinylog-impl</artifactId>
        <version>${tinylog.version}</version>
    </dependency>
    <dependency>
        <groupId>org.tinylog</groupId>
        <artifactId>slf4j-tinylog</artifactId>
        <version>${tinylog.version}</version>
    </dependency>
    <dependency>
        <groupId>org.tinylog</groupId>
        <artifactId>jcl-tinylog</artifactId>
        <version>${tinylog.version}</version>
    </dependency>
    <dependency>
        <groupId>org.tinylog</groupId>
        <artifactId>log4j1.2-api</artifactId>
        <version>${tinylog.version}</version>
    </dependency>

</dependencies>

Testing and Validation

By now, the basic integration is complete. Let’s not rush to do detailed configuration of tinylog, but first verify that everything is correct up to this point. As in the previous log integration example, write a main class to print the logs at each level.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
@Slf4j
@SpringBootApplication
public class Chapter83Application {

    public static void main(String[] args) {
        SpringApplication.run(Chapter83Application.class, args);

        log.error("Hello World");
        log.warn("Hello World");
        log.info("Hello World");
        log.debug("Hello World");
        log.trace("Hello World");
    }

}

Run it and you will see the following log output from the console.

log

By debugging, we can see that the log implementation is already TinylogLogger at this point.

TinylogLogger

Step 3: Add the tinylog configuration file

With the previous step, we have completed the integration, but the format above, is it what you want? Add the configuration and adjust it!

Create the file: tinylog.properties in the resources directory and add the following configuration.

1
2
writer=console
writer.format={date: HH:mm:ss.SSS} {level}: {message}

Re-run the test and the console output looks better.

tinylog

More configuration, such as: file output, level control, etc. Here is not detailed, you can check the official documentation (https://tinylog.org/v2/configuration/), basically with other frameworks are similar, it is easy to configure.

Reference https://mp.weixin.qq.com/s/zJGsEmbn4hCYdiTry3ulGQ