Log processor

xiaoxiao2021-03-06  71

Writing of log components

Several functions of the main implementation: 1 Processing of the records of the operation. 2 Flexible and diversification of processing. 3 The main use of the use.

First, a interface log is defined: addlog adds a log record. ADDLogFactory RemovelFactory GetFactory Add / remove / get the log processing factory setFactory GetFactory Setup / Get the final log processing factory. Let's take a look at the logFactory interface function isaccept a record Processing by the log processing. ProcessLog Processing Logging LogItem (log item) The function is as follows. GetInput Gets the parameter GetData sent by the action to get the old data (useful when updating data) getStaticItem get static data (for example, user name , IP, etc., relatively fixed information)

LogItemimimpl is shown. You can re-inherit the logitem according to your own actual needs. LogFactory implemented. DBLogFactory implemented features to write log records to the database. Logfactory is part of the part to reinload. Refer to DBLogFactory. LOG implementation now AbstractLog realizes most of the way to log (except for addlog), basically no longer inherit the log, as long as the AbstractLog can be inherited. Swanlog is a specific implementation of logs for a specific implementation. If you are not enough, you can do yourself in the AbstractLog class. The above-mentioned categories will be used in the future. Let's take the situation, I use PostgreSQL as a test, you can change the getConnection in dblogfactory to yourself. Connection. Because the table name written to the database is TBLLog I put TBLLOG's table SQL in the attachment, you can use the statement to establish a table first. Then run SwanLog to see a record in the data table. This is here The log is already available, and the rest is in use. I believe that everyone wants to use the problem when writing the program, as long as the program is written, you can automatically establish log. Below is the log Add to the code Method. For web programs, use the class requestInput I have written directly, I get INPUT directly from the request, then get static attributes in the session (of course, it will be set first when initialization). As for OldData, there is nothing a good way. Then before the program performs the POST (or get) method. If your servlet does not inherit or not make the workflow, then you have to add, if you already have it, it is very simple. Add a line. Change a place. Some libraries may be in multiple tables, you can build multiple logfactory, just write the ISACCEPT method. For the client program or RMI ... .. The program, the most important task is to implement the Input method. I believe that everyone has their own data package method. It should be very convenient to achieve it. The following is the code for everyone to browse, the remaining classes, such as Input ... You can be in me In addition to the additional code of the authority component. If there is any good suggestion, please contact me. ======================== Log.java ======================================= package org.fswa N.LOG;

/ ** * @Author Swan fong (Fang Zhiwen) * e-mail: fswan@yeah.net * site: http://blog.9cbs.net/fswan * 2004-11-19 10:37:44 * Log Interface * / public interface log {/ ** * Add log record * @Param ITEM log item * @return Add result * / public int AddLog (logitem item); / ** * Add log processing factory * @Param FAC log processing factory * / Public void addfactory (logfactory FAC); / ** * Remove log processing factory * @Param FAC log processing factory * / public void RemoveFactory (logfactory FAC); / ** * Get all log processing plants * @return log processing Factory * / public logfactory [] getFactory (); / ** * Set the final log processing factory * @Param FAC log processing factory * / public void setFactory (logfactory FAC); / ** * Get the final data processing factory * @return log Treatment factory * / public logfactory getFinAlFactory ();} ============================ ============================================================= =========== Package org.fswan.log; import org.fswan.input;

/ ** * @Author Swan fong (Fang Zhiwen) * E-mail: fswan@yeah.net * site: http://blog.9cbs.net/fswan * 2004-11-19 11:17:26 * Log items * / public interface logitem {/ ** * Get action information * @return operation information * / public input getInput (); / ** * Get raw data * @Param Prop data identity * @return data * / public string getolddata (String PrOP); / ** * Get static data * @Param Prop data identity * @return data * / public string getStaticItem (String prop);} ================== =========== logfactory.java =================================== package org.fswan.log;

/ ** * @Author Swan fong (Fang Zhiwen) * E-mail: fswan@yeah.net * site: http://blog.9cbs.net/fswan * 2004-11-19 11:15:00 * Log processing Factory * / public interface logfactory {/ ** * Does the factory handle a certain issue * @Param Item Item log item * @return can handle * / public boolean isaccept (logitem item); / ** * Processing log, processing results For reason, no negative and 0 * @Param Item Log item * @return processing result * / public int processlog (logitem item);} ==================== === dblogfactory.java =================================== package org.fswan.log; import java.sql.connection; import java.sql .DriverManager; import java.sql.preparedStatement; import java.sql.sqlexception; import java.sql.timestamp; import java.util.date;

/ ** * @Author Swan fong (Fang Zhiwen) * E-mail: fswan@yeah.net * site: http://blog.9cbs.net/fswan * 2004-11-19 14:25:14 * This log The factory exfers the data to the database. If successful returning success return, return failure * contains field IP, user name, description, time, whether it is successful * Name TBLLOG, the corresponding field is IP, User, Description, Date, ReturnValue * * / public class dblogfactory imports logfactory {/ ** * Operation success * / public static final int surpass = 1; / ** * Operation failed * / public static final int ticket = 2; / * (non-javadoc) * @see org .fswan.log.LogFactory # doneLog (org.fswan.log.LogItem) * / public int processLog (logItem item) {Connection conn = getConnection (); try {PreparedStatement pst = conn.prepareStatement ( "insert into tbllog (ip, / "User /", description, processdate, return, value value (?,?,?,?,?); pst.setint (1, IPTOINT (IPTOINT (IPTOINT (IP ")))))); Pst.SetString 2, item.getStaticItem ("User")); Pst.SetString (3, "it's operation"); Pst.SetTimeStamp (4, New time (). GetTime ()); pst.setint (5, 1); pst.executeUpdate (); conn.close (); return 1;} catch (sqlexception e) {E.Print StackTrace ();} return 2;

/ * (non-javadoc) * @see org.fswan.log.logfactory # isaccept (org.fswan.log.logitem) * / public boolean isaccept (logitem item) {return true;} / ** * Get database connection * @return database joint * / public connection getConnection () {transnection getConnection () {transna.forname ("org.postgreSQL.Driver); Return DriverManager.getConnection (" JDBC: PostgreSQL: // localhost: 5432 / hrms "," fswan ", "314843");} catch (ClassNotFoundException E) {E.PrintStackTrace ();} catch (sqlexception e) {E.PrintStackTrace ();} return null;} / ** * Transfer IP string into digital * @Param IP IP String Represents * @Return IP Corresponding Number * / Public IPTOINT (String IP) {String [] IPS = IP.SPLIT ("//."); if (ips.length! = 4) Return -1; int [] = new int [ips.lend "; for (int i = 0; i

Import org.fswan.input;

/ ** * @Author Swan fong (Fang Zhiwen) * E-mail: fswan@yeah.net * site: http://blog.9cbs.net/fswan * 2004-11-19 14:17:04 * * / public class LogItemImpl implements logItem {Input input; HashMap map; Properties staticItem; public LogItemImpl (Input input, HashMap map, Properties staticItem) {this.input = input; this.map = map; this.staticItem = staticItem;} / * ( Non-javadoc) * @see org.fswan.log.logitem # getInput () * / public input;} / * (non-javadoc) * @see org.fswan.log.logitem # getolddata ( Java.lang.string) * / public string getData (String Prop) " map.get (prop);}

/ * (Non-javadoc) * @see org.fswan.log.logitem # getStaticItem (java.lang.string) * / public string getStaticItem (String Prop) {Return staticItem.getProperty (prop);}

}

============================ AbstractLog.java =================== ===== Package org.fswan.log;

/ ** * @Author Swan fong (Fang Zhiwen) * e-mail: fswan@yeah.net * site: http://blog.9cbs.net/fswan * 2004-11-19 11:37:24 * * / public abstract class AbstractLog implements Log {private LogFactory [] facs; private LogFactory finalFactory; / * (non-Javadoc) * @see org.fswan.log.Log # addFactory (org.fswan.log.LogFactory) * / public void addFactory (Logfactory FAC) {IF (FAC == NULL) Return; if (FACS == NULL) {FACS = New Logfactory [1]; FACS [0] = FAC;} else {logfactory [] fs = new logfactory [FACS. Length 1]; System.Arraycopy (FACS, 0, FS, 0, FACS.LENGTH); FS [fs.length-1] = FAC; FACS = fs;}} / * (non-javadoc) * @see org .fswan.log.log # getFactory () * / public logfactory [] getFactory () {Return FACS;}

/ * (non-javadoc) * @see org.fswan.log.log # getfactory () * / public logfactory getFinalFactory () {return finalfactory;}

/ * (Non-javadoc) * @see org.fswan.log.log # removefactory (org.fswan.log.logfactory) * / public void RemoveFactory (logfactory FAC) {if (FAC == Null) Return; if (FACS == NULL) RETURN; for (int i = 0; I

/ * (Non-javadoc) * @see org.fswan.log.log # setfinalFactory (org.fswan.log.logfactory) * / public void setfactory (logfactory fac) {FinalFactory = FAC;}

} ========================== swanlog.java ======================================================================================================================================================================= ======== Package org.fswan.log; import java.util.properties;

/ ** * @Author Swan fong (Fang Zhiwen) * E-mail: fswan@yeah.net * site: http://blog.9cbs.net/fswan * 2004-11-19 11:49:27 * Implemented The log processor, first find the appropriate processor in the logfactory, if the process does not process it in the final processor processing, the return value after the process is false * * / public class swanlog extends AbstractLog {public static firm Int HaventProcess = 0 Public swanlog () {addfactory (new dblogfactory ());} / * If the return value for processing is processed to process the value returned, if it is the final processor processing return value is a negative number of processing return value * (Non-javadoc ) * @See org.fswan.log.log # addlog (org.fswan.log.logitem) * / public int address {logfactory [] fs = getFactory (); if (fs! = Null) {for (INT i = 0; i

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

New Post(0)