Write a static log operation class everywhere

zhaozj2021-02-16  89

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 users' logins, access, and 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 plugged into a "next week plan" record ID: 1048

WED May 19 13:35:54 CST 2004: Li Si landed the system

WED May 19 13:35:55 ​​CST 2004: Zhang San deleted a "last week summary" record, D: 1024

WED May 19 13:35:59 CST 2004: Zhang San withdrew from the system

..............

Implementation:

1. For a good implementation of this record class, you must use the "single" mode so that this class does not have to generate an instance of it, initialize I / O. (This is very expensive to a certain extent Time).

2. In order to prevent multiple threads while operating (write) log files, cause "dead lock", you must consider synchronization, use the synchronized keyword.

3. In order not to care about the generation of the unique instance of this class, the log record is implemented directly using the static method of this class.

4. For more convenient configuration log files, use the properties file configuration.

There is too much nonsense, unlike the process, directly look at the code, all the comments illustrated 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 ();

}

}

}

● Specific use of class ::

1. Compile this class, newly established attribute profiles: log.properties, and make sure you put it down your compilation class location

The contents of the file are as follows: Of course, you can modify the path to the path you want.

Logfile = E: //logtext.log

2. Example of use:

Use 1:

Logwriter.log ("Zhang San landed the system");

Logwriter.log ("Zhang San deemed XXX record: Record ID:");

Use 2:

Try {

}

Catch (Exception EX) {

Logwriter.log (ex);

}

● Some descriptions:

I. The getClass (). GetResourceAsStream ("file name") does not support Static calls, so putting this class as non-Static, but its call is only called in Outinit (), and Outinit ()

Also call only in the private constructor, and private constructors can be called in static STATIC,

This will reach the use of a static method to call the log at any time, and ensure that only one instance is

II. If you understand the log4j, you can use the similar approach to encapsulate the log4j and achieve static convenient calls.

III. I used multiple threads to test this class. I didn't find problems. Because I have studied Java for a year. If the master sees the mistakes or deficiencies, I will enlighten me. Thank you. Details : QQ: 29189725

转载请注明原文地址:https://www.9cbs.com/read-13229.html

New Post(0)