一個db類 類似於jdbc的驅動和連結配置
package com.assi;
import org.apache.log4j.jdbc.JDBCAppender;
public class DB extends JDBCAppender {
public DB(){
this.setDriver("oracle.jdbc.driver.OracleDriver");
this.setURL("jdbc:oracle:thin:@172.18.220.10:1521:orcl");
this.setUser("log");
this.setPassword("log");
}
}
具體寫日誌的類 //沒有注釋 代碼比較簡單
package com.assi;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class CataLogs extends DB {
private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
private Logger logger;
public CataLogs(){
logger=Logger.getLogger(CataLogs.class.getName());
}
//自動執行的
protected void execute(String sql){
Connection con=null;
PreparedStatement stmt=null;
try{
con=getConnection();
stmt=con.prepareStatement(this.getSql());
stmt.execute();
}
catch(Exception e){
e.printStackTrace();
}
}
public void writeCataLog(String resName,String operator,String content,String opratorType,String bizType) {
this.setSql("insert into tmon_monitor_log_catainfo values(test.nextval,'"+resName+"','"+operator+"','"+df.format(new Date())+"','"+content+"'" +
",'"+opratorType+"','"+bizType+"')");
logger.add(this);
logger.setLevel(Level.ALL);
logger.info("寫入目錄動作記錄成功");
}
}