Home Java javaTutorial Introduction to the method of asynchronous printing of logback logs (code example)

Introduction to the method of asynchronous printing of logback logs (code example)

Feb 01, 2019 am 11:42 AM
logback

This article brings you an introduction to the method of asynchronous printing of logback logs (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Recently encountered a problem: the customer's server program occasionally responds too slowly to requests. By checking the log, it was found that the RSA signature verification code was executed for more than 20 seconds, while under normal circumstances it only takes Requires 16 milliseconds.

The RSA certificate is loaded when the server starts, and there is no problem of slow file reading. After looking at those lines of code, the most suspicious thing is the code for logback log printing.

Checked the production log configuration. The configuration in logback.xml is to generate a folder every month. The log files of the current month are all in the same folder. For example, the 201901 folder contains all the logs of January 2019. document. Each file is configured with a *.log.zip suffix and a size of 10MB, which means 10MB is a size-delimited file. The log printing class is configured with RollingFileAppender.

On 2019-1-30, more than 5,000 log files have been generated under the January 2019 folder, with an average of 167 log files per day.

So it is suspected that there are too many log files and the index file time is too long, which causes the code execution to slow down. In other words, if you want to doubt this, there is a premise: the log printing is synchronous, and the print log is called After the line of code is executed and written to the file, the business code will continue to be executed.

I asked several colleagues, and they all told me that log printing is performed asynchronously. Only one friend said it was synchronous. We all think that printing logs and writing files is time-consuming, and the logging framework should not write files synchronously. However, the fact is that if asynchronous printing is not configured, the log will be printed synchronously.

Breakpoint follow-up code found that the log is written to the file synchronously. Only when the configured appender is AsyncAppender, the log printing is printed asynchronously.

The following is the situation of synchronously printing logs:

logback.xml configuration:

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    ......
</appender>

<root level="DEBUG">
    <appender-ref ref="CONSOLE"/>
</root>
Copy after login

Execution code:

LogWork.debug("111111111111");
System.out.println("2222222222222");
Copy after login
Copy after login

Run result:

As you can see, the execution result is that the log of the log framework is printed and executed first. , and then execute the subsequent business code. So it's synchronous.

The following is how to configure the asynchronous printing log class:

The asynchronous printing log class AsyncAppender needs to reference an other log printing class, ASYNC only needs to print the log that needs to be printed Write to the defined cache queue, and then start a daemon thread to get the log from the queue and call the CONSOLE log printer to write the file. In this way, log printing is performed asynchronously.

logback.xml configuration:

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    ......
</appender>

<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
    <discardingThreshold>0</discardingThreshold>
    <queueSize>100</queueSize>
    <neverBlock>true</neverBlock>
    <appender-ref ref="CONSOLE"/>
</appender>

<root level="INFO">
    <appender-ref ref="ASYNC"/>
</root>
Copy after login

Execution code:

LogWork.debug("111111111111");
System.out.println("2222222222222");
Copy after login
Copy after login

Running result:

The running result shows that after the code that calls the log framework to print is executed, it just puts the log to be printed into the cache queue, and then continues to execute the following. code, so the following 222222 is printed first, and then 111111 is printed. The explanation is that after configuration, log printing works asynchronously.

Because the question at the beginning of the article raised the issue of synchronization of log printing, I did some research, but in the end I did not connect it with the problem encountered, because even if I knew that the log was printed synchronously, There is no way to explain why code execution is extremely slow for a few minutes occasionally. The evidence is insufficient, so no conclusion can be drawn.

The above is the detailed content of Introduction to the method of asynchronous printing of logback logs (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? Apr 19, 2025 pm 07:15 PM

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

How to restrict access to specific interfaces of nested H5 pages through OAuth2.0's scope mechanism? How to restrict access to specific interfaces of nested H5 pages through OAuth2.0's scope mechanism? Apr 19, 2025 pm 02:30 PM

How to use OAuth2.0's access_token to achieve control of interface access permissions? In the application of OAuth2.0, how to ensure that the...

In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? Apr 19, 2025 pm 01:51 PM

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

In Java remote debugging, how to correctly obtain constant values ​​on remote servers? In Java remote debugging, how to correctly obtain constant values ​​on remote servers? Apr 19, 2025 pm 01:54 PM

Questions and Answers about constant acquisition in Java Remote Debugging When using Java for remote debugging, many developers may encounter some difficult phenomena. It...

How to convert names to numbers to implement sorting within groups? How to convert names to numbers to implement sorting within groups? Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

How to choose Java project management tools when learning back-end development? How to choose Java project management tools when learning back-end development? Apr 19, 2025 pm 02:15 PM

Confused with choosing Java project management tools for beginners. For those who are just beginning to learn backend development, choosing the right project management tools is crucial...

When Tomcat loads Spring-Web modules, does the SPI mechanism really destroy the visibility principle of Java class loaders? When Tomcat loads Spring-Web modules, does the SPI mechanism really destroy the visibility principle of Java class loaders? Apr 19, 2025 pm 02:18 PM

Analysis of class loading behavior of SPI mechanism when Tomcat loads Spring-Web modules. Tomcat is used to discover and use the Servle provided by Spring-Web when loading Spring-Web modules...

How to analyze the cracking process of IntelliJ IDEA and find the lib or class responsible for registration? How to analyze the cracking process of IntelliJ IDEA and find the lib or class responsible for registration? Apr 19, 2025 pm 04:00 PM

Regarding the analysis method of IntelliJIDEA cracking in the programming world, IntelliJ...

See all articles