1

Introduction to tinylog

tinylog , like various other things starting 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) without any external dependencies. The total size of the two JAR files is only 178KB.

Although it is a lightweight solution, the basic log management functions we commonly use are very complete. It has a similar API design to other popular log frameworks, a variety of configurable log output options, and performance is also very good (this It is the official Benchmark ).

Today we will learn how to use tinylog to record logs in Spring Boot.

Integrate tinylog

Through the study of the previous Spring Boot 2.x Basic Tutorial: Using log4j2 to record logs , recall that integrating other logging frameworks can be summed up in the following steps:

  1. Exclude Spring Boot default logging framework dependencies
  2. Introduce the logging framework dependencies to be used
  3. Add the configuration file of the new logging framework

Well, let's follow these steps to practice:

Step 1: Exclude Spring Boot default logging framework dependencies

<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: Introduce tinylog dependencies

<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>

Test and Verification

At this point, the basic integration has been completed. We are not in a hurry to configure tinylog in detail, first verify whether everything is correct here. Like the previous log integration example, write a main class to print logs at all levels.

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

}
Lombok's @Slf4j is used here. If you don't understand it, please read this: Lombok: Make JAVA code more elegant

Run it, and you can see the output from the console as follows:

Through debug, we can see that the log at this time is already TinylogLogger

Step 3: Add tinylog configuration file

Through the previous step, although we have completed the integration, is the above format what you want? Join the configuration to adjust it!

Create a file in the resources directory: tinylog.properties

Add the following configuration:

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

Rerun the test, does the console output look better?

More configurations, such as: file output, level control, etc., are not detailed here. You can check the official document , which is basically similar to other frameworks and easy to configure.

Well, today's learning is here! If you encounter difficulties in the learning process, you can join our super high-quality Spring technical exchange group , participate in exchanges and discussions, and learn and progress better! More Spring Boot tutorials can be clicked directly! , welcome to collect and forward support!

code example

The complete project of this article can be viewed in the chapter8-3 project in the 2.x directory in the following warehouse:

If you think this article is good, welcome Star support, your attention is the driving force for me to persist!

Welcome to my public account: Programmer DD. Learn about cutting-edge industry news for the first time, share in-depth technical dry goods, and obtain high-quality learning resources

程序猿DD
2.2k 声望2.8k 粉丝

作品:《Spring Cloud微服务实战》、SpringForAll社区、OpenWrite、Youtube中文配音