Using Currentthread.name to unify identity logging (Java-logback article)

Source: Internet
Author: User

Java Version Payment Center, the log component uses Logback. The log pattern in the Logback.xml is configured as follows:

    <!--Local Log directory -    < Propertyname= "User_home"value= "${catalina.base}/logs/logback-srv" />    < Propertyname= "Log_msg"value= "%x{sid}%d{yyyy-mm-dd HH:mm:ss. SSS} [%t]%-5p [%c]-%m%n " />    < Propertyname= "Log_dir"value= "${user_home}/%d{yyyymmdd}"/>    <!--2017-08-22 10:43:19.307 [dubboserverhandler-10.0.0.178:38001-thread-187] INFO [ Com.emax.paycenter.backend.facade.IPayCenterFacadeImpl] -    <Appendername= "STDOUT"class= "Ch.qos.logback.core.ConsoleAppender">        <Encoder>            <pattern>${LOG_MSG}</pattern>        </Encoder>    </Appender>

There is%t in the pattern, which means the thread ID (at first, I mistakenly think that this%t is referring to the thread id!!! ), and each transaction request is processed in a separate thread, then it can mark all logs for each transaction request. The log files are as follows:

The analysis log found that this thread identified as "dubboserverhandler-10.0.0.178:38001-thread-195" is reused by different trade requests and cannot uniquely mark a thread, which obviously increases the difficulty of troubleshooting the problem on the line. Last night, 2 people in our project decided to do a refactoring on this point.

Prior to the. NET Payment Center, I made a refactoring of all the logs that uniformly marked a transaction request, see "using Currentthread.name to unify identity logging." Obviously, it is also necessary for Java to use a unique identifier to mark all logs for a transaction request.

The thread ID of the current thread is read-only and we cannot change it. How do we do that?
Experience tells me to use the name attribute of CurrentThread. But the problem comes, I in the transaction processing of the first statement to the current thread name assignment, after each module in each method of each Log.info statement must be explicitly recorded as the front-line name, too many changes. And that kind of code has a weird taste.
Of course, another way is to follow the. NET version of the idea, do a log proxy class, the above call Log.info and explicitly record the current thread name to do a package, and then the logic code in the log to call this proxy class. This implementation of the drawbacks with the above scheme Dora.

What about that?

Ask.
First ask Niang, no solution.
Then asked the colleague, said to write a apperder. The unknown of the logback underlying code will make it harder to solve the problem.
"Baidu: Logback Custom Appender
Read Logback Source series article (v)--appender http://kyfxbl.iteye.com/blog/1173788 "

No inserting willow

I went to work in the morning. Constructs a unique string identifier to assign a value to the current thread name. The following two lines of code are written before the first statement of the transaction processing.

 PublicBaseresponse Invoke (String Requestjson, Baserequest baserequest)throwsException {String ThreadName        = String.Format ("%s_%s_%s", Baserequest.getmethod (),  new simpledatefor Mat ("Hhmmsss"). Format (new  Date ()), Uuid.randomuuid (). toString (). toUpperCase (). substring (0 , 5)); Thread.CurrentThread (). SetName (threadname);Log.info ("#IPayCenterFacadeImpl, calling service: {}", Baserequest.getmethod ()); Log.info ("#IPayCenterFacadeImpl, request parameter: {}", Requestjson); //1. Get API Interface ImplementationIpaycenterapi Paycenterapi =(IPAYCENTERAPI) Paycenterservicefactory.getservice (Baserequest.getmethod ()); //2. Handling Business logicBaseresponse result =Paycenterapi.handle (Requestjson); returnresult; }

Inadvertently, when I ran the code, I found that the thread information ID of the%t was the string ID that I set for the current thread name.
This only found that the%t or%thread output is the name of the thread that generated the log!!

Debug Code Discovery: Thread.CurrentThread (). GetName ():D ubboserverhandler-192.168.40.80:28005-thread-2,thread.currentthread () . GetId (): 246

After this twists and turns, our problem is finally solved, the excitement is abnormal. Look at the following log, a strong sense of accomplishment O (∩_∩) o

Using Currentthread.name to unify identity logging (Java-logback article)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.