Tinylog profile

Tinylog, like everything else that starts tiny, is a lightweight open source logging solution. It itself contains only two JAR files (one for the API and one for the implementation) without any external dependencies. The combined size of the two JAR files is only 178KB.

Although it is a lightweight solution, the basic log management features we use are complete, it has an API design similar to other popular logging frameworks, a variety of configurable log output options, and excellent performance (official Benchmark).

Today we will learn how to log using TinyLog in Spring Boot.

Integrated tinylog

From the previous Spring Boot 2.x basics tutorial: Logging with Log4j2, recall that integrating other logging frameworks can be concluded as follows:

  1. Exclude the Spring Boot default logging framework dependency
  2. Import logging framework dependencies to use
  3. Add the configuration file of the new logging framework

All right, let’s do this:

Step 1: Remove the Spring Boot default logging framework dependency

<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>
Copy the code

Step 2: Introduce tinylog dependencies

<properties>
    <tinylog.version>Against 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>
Copy the code

Test and validation

At this point, the basic integration is complete. Let’s not rush into the detailed configuration of TinyLog, but verify that everything up here is correct. As in the previous log consolidation example, write a main class that prints each level of log.

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

Lombok’s @slf4J is used here. If you don’t already know lombok, read this article: Making JAVA Code more elegant

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

With debug, we can see that the log is already TinylogLogger

Step 3: Add the Tinylog configuration file

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

Create the file tinylog.properties in the Resources directory

Add the following configuration:

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

Rerun the test and see if the console output looks better

More configuration, such as: file output, level control, etc., is not detailed here, you can check the official documentation, basically similar to other frameworks, easy to configure.

Well, that’s all for today’s learning! If you encounter difficulties during the learning process, you can join our super high quality Spring technology exchange group to participate in the exchange and discussion, better learning and progress! More Spring Boot tutorials can be clicked direct! , welcome to collect and forward support!

Code sample

For the complete project of this article, see the chapter8-3 project in the 2.x directory of the warehouse below:

  • Github:github.com/dyc87112/Sp…
  • Gitee:gitee.com/didispace/S…

If you found this article good, welcomeStarSupport, your attention is my motivation!

Welcome to pay attention to my public number: program ape DD. Learn cutting-edge industry news, share in-depth technical know-how, and obtain high-quality learning resources at the first time