Use threadlocal to record logs

Source: Internet
Author: User


Use threadlocal to record logs
Original reverocean (Participation score: 9727, expert score: 822) published: Version: 1.0 read:353Times

It is necessary to record logs in a project, but it is often recorded by logger. debug ("... "). logs are recorded when the program runs, especially in multi-threaded or web applications. Different logs may be recorded in the same file at the same time, when an error occurs, you cannot determine which information you want to care about. therefore, if we start logging at the beginning of a program, but this log is not recorded in the log file, but saved in a threadlocal, when an error occurs, all these logs can be output to the log file for easy search.
The Code is as follows:
 
 
Import java. util. arraylist;
Import java. util. List;
Public class debuglogger {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Test and use. A new debuglogger can be created at the beginning of a method.
Debuglogger logger = new debuglogger ();
// Simulate the ten logs to be recorded and record them to the debuglogger at a time.
For (INT I = 0; I <10; I ++ ){
// Modify the following code to record logs
Logger. Put ("log Info" + I );
}

 
// When the method ends or an exception occurs, extract the recorded log
String [] strings = logger. Get ();
// Record the obtained logs to the log file once.
For (INT I = 0; I <strings. length; I ++ ){
System. Out. println (strings [I]);
}

// Enter the log Content recorded in debuglogger.
Logger. Clear ();
{
String [] strings1 = logger. Get ();
For (INT I = 0; I <strings1.length; I ++ ){
System. Out. println (strings1 [I]);
}

}
}
 
// Internal static class, inherited to threadlocal
Private Static class threadlocallist extends threadlocal {
// An arraylist object is returned when the get () method is called.
Public object initialvalue (){
Return new arraylist ();
}
// Returns the list stored in threadlocal.
Public list getlist (){
Return (list) Super. Get ();
}
}
 
Private threadlocallist list = new threadlocallist ();
Private Static string [] stringarray = new string [0];
// Clear the recorded logs
Public void clear (){
List. getlist (). Clear ();
}
// Save the log content to be recorded
Public void put (string text ){
List. getlist (). Add (text );
}
// Return the logs to be recorded
Public String [] Get (){
Return (string []) List. getlist (). toarray (stringarray );
}
}

 
In the code, you can call debuglogger. put () to save information about what your program is doing, and, if necessary later (for example, an error occurs), you can easily retrieve debugging information related to a specific thread. This technology is much easier than simply dumping all the information to a log file and then trying to find out which log records come from which thread (and worry about thread contention for log record objects, and more effective.

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.