The log file is a file that is in place, it can record the running status of the program and the error message, almost every installer has its installation file .. In our Java development web project, often need Record some of the user's login, access, operation information. Such as: a office system, its log file requires a record: ......... WED May 19 13:35:50 CST 2004: Zhang San landed the system WED May 19 13:35 : 53 CST 2004: Zhang San inserted a "next week plan" record ID: 1048wed May 19 13:35:54 CST 2004: Li Si landed the system WED May 19 13:35:55 CST 2004: Zhang San deemed A "Summary last week" record, D: 1024wed May 19 13:35:59 CST 2004: Zhang San quits the system ................ Implementation: 1. For very Good implementation This record class, you must use "single" mode so that this class does not have to generate an instance of it, initialize I / O. (This is very time time). 2. In order to prevent multithreading simultaneously (write) log files, cause the file "dead lock", you must consider synchronization, use the synchronized keyword .3. In order not to care about the unique instance of this class, directly use the static method of this class Realize log record 4. For more convenient configuration log files, use the properties file configuration. There is too much nonsense, unreasonable, directly see the code, all comments in the code:
Import java.io. *;
Import java.util. *;
Public class logwriter {
Private static factory string defalutlogfilepathname = "c: //logtext.log"; // The path and file name of the default log file
Private static logwriter logwriter; // The only instance of this class
Private static inputstream fin; // Input stream of Property profile
Private Static Properties Pro; // Class Properties's Suppine Is Hashtable Class
Private static printwriter out; // output stream
Private static string logfilename; // Output File Name
private logwriter () {
Outinit (); // init out put stream, instantiate the PrintWriter Out object.
}
/ ** Save the information you want to save in the log file to achieve synchronization
* Out Put The Message Infomation
* @Param Message Infomation
* /
Public static synchronized void log (string message) {
IF (logwriter == null || (out == null) {
Logwriter = new logwriter ();
}
IF (out! = NULL) {
Out.println (New Java.util.date () ":" Message);
}
}
/ ** Save the exception information in the log file to achieve synchronization
* OUT PUT THE EXCETION OFFMATION
* @Param Message Infomation
* /
Public Static Synchronized Void Log (Exception EX) {
IF (logwriter == null || (out == null))
Logwriter = new logwriter ();
IF (out! = NULL) {
Out.println (New Java.util.date () ":"); EX.PrintStackTrace (OUT);
}
}
/ **
* Output file stream of init
* /
Private void outinit () {
IF (logfilename == null)
LogFileName = getLogFileName (); // From the property file to the path to log files
Try {
If (out == null) {// If there is no instance of output I / O, a letter is generated.
OUT = New FileWriter (LogfileName, True), true); //
// The meaning of the second parameter in the filewriter () is: whether to add content in the file
}
}
Catch (IOException EX) {
System.out.println ("Unable to open the log file:" logfilename);
EX.PrintStackTrace ();
OUT = NULL;
}
}
/ **
* According to the configuration file. To get the location of the log file
*
* @Return logfilename
* /
Private string getlogfilename () {
Try {
IF (Pro == Null) {
Pro = new java.util.properties ();
FIN = getClass (). getResourceAsStream ("log.properties"); // In the current location of the class, find the property profile log.properties
Pro.Load (FIN); / / Load Profile
Fin.close ();
}
}
Catch (IOException EX) {
System.err.println ("Unable to open Properties: Log.properties");
EX.PrintStackTrace ();
}
Return Pro.GetProperty ("logfile", defalutlogfilepathname;
/ / Get the log file path according to the attribute value, the second parameter is: If the "logfile" flag is not found, the default value returned
}
/ ** You can also call this method to release the resource when all logs are completed.
* free all the resouce, this is secondy method
* /
Public void free () {
Try {
This.logwriter = NULL;
IF (Out! = NULL)
THIS.OUT.CLOSE ();
IF (Fin! = NULL)
this.fin.close ();
}
Catch (IOException EX) {
EX.PrintStackTrace ();
}
}
}