Summary of log records

Source: Internet
Author: User

Some time ago, a company's project required a lot of data interfaces and synchronization programs, so it encountered a log record problem, how to record the time, which do not need to be remembered. After a series of discussions on the log record issue, a unified solution was finally achieved. It solves the problem of inconsistent and random log records of different modules and systems and different developers. Today, I finally took the time to summarize this problem and sort it out based on the information on the network.

Why record logs?

Recording logs is an important way to debug programs and monitor program running. It has two main objectives: to promptly discover and locate bugs and display the program running status. Correct and detailed log records can help you quickly locate problems. Similarly, by checking the log, you can see what the program is doing and whether it is executed as expected. Therefore, it is necessary to record the running status of the program.

Where logs need to be recorded

1. Call or encapsulate external programs or interfaces
Logs should be recorded before and after calls to external systems and modules to facilitate interface debugging. When a problem occurs, you can quickly find out the problem.

Log. debug ("calling external system:" + parameters );
Object result = NULL;
Try {
Result = callremotesystem (Params );
Log. debug ("called successfully. result is" + result );
} Catch (exception e ){
Log. Warn ("failed at calling XXX system. Exception:" + E );
}

2. If an important object or variable is modified, the information before modification and the status after modification should be recorded in info.
Changes in important state information in the program should be recorded to facilitate the recovery of the site when querying the problem, and to infer the program running process

Boolean isrunning;
Isrunning = true;
Log.info ("system is running ");
//...
Isrunning = false;
Log.info ("system was interrupted by" + thread. currentthread (). getname ());

3. System Entry and Exit:
The entry and exit of the system, and the execution of important methods or modules. Record its input and output for easy location.

4. Handling of business exceptions or important services:
Any business exceptions should be recorded, and some important services in the system, such as settlement, should also be recorded.

5. Unexpected execution:
Print the log where the program is executed "possibly. If I want to delete an object, the result is returned successfully. But in fact, the file does not exist until you want to delete it. The final result is consistent, but the program needs to let us know this situation and find out why the file does not exist before it is deleted.

Int myvalue = xxxx;
Int absresult = math. Abs (myvalue );
If (absresult <0 ){
Log.info ("original int" + myvalue + "has nagetive abs" + absresult );
}
6. Rare else situations:
In the code, you must have a complete if else. If there is no execution in the else, record the log to avoid the else from consuming your request or returning unintelligible results to you.

Object result = NULL;
If (running ){
Result = xxx;
} Else {
Result = YYY;
Log. debug ("system does not running, we change the final result ");
}

7. Execution progress of batch data:

Log. debug ("current progress:" + (currentpos * 100/totalamount) + "% ");

  Log recording method

1. The log information should contain the recorded program running time and error occurrence time.

2. clear, accurate, and meaningful log information
The log should be clear and accurate: When you see the log, you can accurately know the error because the Connection Pool cannot obtain the connection.

Connection connection = connectionfactory. getconnection ();
If (connection = NULL ){
Log. Warn ("System initialized unsuccessfully ");
}
3. Record the error location
In the code, you must record the file name and function name of the error (you must be able to partition and reload the function separately), with the best number of rows.

4. differentiate log levels
It is very important to classify logs in programs. Generally, logs can be divided into six levels: trace <debug <info <warn <error <fatal.
Debug is used to output debugging information, which is generally not very important in the program; Info represents some important information, such as the program Initialization is complete, error is generally caused by the program itself, recoverable errors. Fatal generally indicates that the environment on which the program depends has an error, so that the program cannot be recovered. Among them, debug and error are commonly used.

5. detailed and accurate information
Make sure to record the complete and accurate information. Otherwise, it is better to record the information, because the wrong information may mislead developers. Therefore, the simplest way is to record the complete stack trace.

6. It is best to bring the context to the log.
There is no corresponding context log, but it is noise. They do not take effect for meaningful values or spaces in the debugging process.
Log information such as transaction failed is even more maddening than non-recorded log information.



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.