[Original] Be careful when writing log4j multi-process

Source: Internet
Author: User
Problem Discovery : Log4j logs are used for the project. The company uses the log4j-wget to download logs-awk to process logs-into MySQL
Recent statistics show that the statistics are less serious than the logs. The script database is synchronized to the test machine, and the execution is normal.
Later, an error occurred while writing logs. For example, the log theory of x.2009-08-13-00 should be cut out at, that is, the write is completed. However, the last write time is more than 14 o'clock.
Tail the logs. even more amazing, we found that the logs in x.2009-08-13-00 are all. It was rewritten.

Problem Summary : Log4j uses the following mechanism to cut files: A New write request is generated in the next hour of the system time. For example, a request is sent at, which has been a request. Switch the log x.2009-08-12-00.
Suspected system time. Because the script for system time synchronization is made in Linux. However, this problem was quickly ruled out.
Consider the problem of multi-process. Because PV logs on multiple pages must be recorded in the same log. Therefore, action must call the same service class for processing. However, synchronization synchronized on the service is still fruitless.
My call structure is like this.
Action1 -- call service --- Call log4j to write data to a static method logger, which is shown in the following figure:
Public static void log (shoppageloggerbean bean ){
Stringbuffer contents = new stringbuffer ();
Contents. append (SDF. Format (bean. getdate ()));
Contents. append (split_str );
Logger.info (contents );}
Analysis : Google knows that log4j has its own file lock function. View me again Program Public static void log (shoppageloggerbean bean) does not actually implement the synchronization function.
Because it is a static method, the upper layer (service) and synchronize do not work at all. Later, synchronization is successfully added to logger ~!
Public static Synchronized Void log (shoppageloggerbean bean ){
Stringbuffer contents = new stringbuffer ();
Contents. append (SDF. Format (bean. getdate ()));
Contents. append (split_str );
Logger.info (contents );}
Conclusion: When processing log4j multi-process processing, you must perform synchronous processing at the place where logs are written.

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.