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.