Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the Logback configuration file

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to write the Logback configuration file, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Affected by Typhoon Lekima, many places have ushered in heavy rain, so you can't go out to play and study together on weekends. Let's start with the most basic configuration and then introduce the advanced features of asynchronous output logs. If you only want to see the asynchronous output log to improve performance, please pull down the article a little bit.

Profile logback-spring.xml

The SpringBoot project comes with logback and slf4j dependencies, so focus on writing configuration files, what dependencies need to be introduced, and log dependency conflicts are all out of our control. The logback framework loads a configuration file named logback-spring or logback under classpath by default. To store all logs in one file, the file size becomes larger and larger as the application runs and it is difficult to troubleshoot problems. The right thing to do is to separate error logs from other logs, and different levels of logs are recorded and stored according to the time period.

[% d {yyyy-MM-dd' 'HH:mm:ss.sss}] [% C] [% t] [% L] [%-5p]% m% n ERROR DENY ACCEPT [% d {yyyy-MM-dd'' HH:mm:ss.sss}] [% C] [% t] [% L] [%-5p]% m% n ${LOG_INFO_HOME} / /% d.log 30 ERROR [% d {yyyy-MM-dd' 'HH:mm:ss.sss}] [% C ] [% t] [% L] [%-5p]% m% n ${LOG_ERROR_HOME} / /% d.log 30

Partial label description

Tag, required, used to specify the most basic log output level

Tags, adding append

Tag, which specifies the collection policy for logs by using this tag

The name attribute specifies the appender naming

The class attribute specifies the output policy. There are usually two types: console output and file output, which is to persist the log. ConsoleAppender outputs logs to the console

Tag, which specifies the filtering policy by using the tag

The tag specifies the type of filter

Tag, and use the tag under this tag to specify the log output format

Tag specifies a collection policy, such as time-based collection

The tag specifies the storage address of generating logs. Through this configuration, the goal of classifying mobile phone logs has been achieved.

Logback Advanced feature Asynchronous output Log

The previous log configuration was based on synchronization, and a disk IO was performed each time the log was output to a file. Using the way of asynchronous log writing without disk IO, blocking threads and causing unnecessary performance loss. The way to output the log asynchronously is simple. Add an appender based on asynchronous write log and point to the previously configured appender.

0256 0 256 Asynchronous output Log performance Test

Since the performance can be improved, a test comparison must be conducted. How many times can the performance of synchronous and asynchronous output logs be improved?

Server hardware

CPU six cores

8GB of memory

Testing tool

Apache Jmeter

Synchronous output log

Number of threads: 100

Ramp-Up Loop (which can be understood as the time it takes to start a thread): 0 can be understood as 100 threads enabled at the same time

Test result

Focus on the metric Throughput [TPS] throughput: the number of requests processed by the system per unit time, and TPS is 44.2/sec in the synchronous output log

Asynchronous output log

Number of threads 100

Ramp-Up Loop:0

The test result TPS is 497.5/sec, and the performance is improved more than 10 times!

Principle of asynchronous log output

Start tracing from the Logger.info method under the logback framework. The method call path is shown in the following figure:

The most important thing in the asynchronous output log is the append method in the AsyncAppenderBase class under the ch.qos.logback.classic package in the configuration file. Check the source code of this method:

Protected void append (E eventObject) {if (! this.isQueueBelowDiscardingThreshold () | |! this.isDiscardable (eventObject)) {this.preprocess (eventObject); this.put (eventObject);}}

Determine whether the log needs to be discarded by the queue situation, and put it in the blocking queue if not. By looking at the code, the blocking queue is listed as ArrayBlockingQueueu, with a default size of 256, which can be modified through the configuration file. Logger.info (...) To append (...) It's over, just do the thing of stuffing the log into the blocking queue, and then continue to execute Logger.info (.) The following sentence. A worker thread is defined in the AsyncAppenderBase class, and the key code in the run method is as follows:

E e = parent.blockingQueue.take (); aai.appendLoopOnAppenders (e)

Take a log from the blocking queue and call the appendLoopOnAppenders method in the AppenderAttachableImpl class to maintain an Append list. The process of calling a method in a Worker thread is mainly shown in the following figure:

The two main methods are the encode and write methods. The former method converts the bytecode into bytecode according to the way specified by encode in the configuration file, and the latter method writes the converted bytecode to the file. So writing the file is done by setting up a new thread, and the main thread throws the log into the blocking queue and then does something else.

On how to write the Logback configuration file to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report