1. 程式人生 > >Javaweb記錄日誌到資料庫和檔案中

Javaweb記錄日誌到資料庫和檔案中

=============jsp頁面===================
// 封裝的js函式,可通用
/**
*title:標題
*content:詳細內容
*target: 記錄到哪裡(1=資料庫(default),2=檔案,3=資料庫+檔案)
*type:日誌種類(1=伺服器執行日誌(default),2=SQL日誌,3=使用者動作日誌),
*level:日誌等級(1=普通(default),2=中等,3=警告,4=出錯,5=危險)
*/
var log=function (title,content,type,level,target){
if(title==null||content==null){return;}
if(targer==null||targer<1||targer>3){target=1;}
if(type==null|type<1||type>3){type=1;}
if(level==null|level<1||level>5){level=1;}

$.ajax({
type : “POST”,
url : “xxx!log.action”,
data : {
“log.title”:title,
“log.content”:content,
“log.target”:target,
“log.type”:type,
“log.level”:level
},
async : true,
success : function(data) {
// nothing..
}
});

};

//比如:

try {

} catch (e) {
log(“登入失敗”,”使用者致遠嘗試連續10次登入賬戶,均失敗”,3,1);
}

try {

} catch (e) {
log(“系統出錯”,”Caused by: org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.”,1,4);

}

try {

} catch (e) {
log(“頁面出錯”,e.message);

}

//========================JavaBean==============

package test;

import java.util.Date;

/**
* 2017/07/13
* @author king
*
*/
public class Log {

private Integer id; // mysql自增主鍵
private String title; // 日誌標題
private String content; // 日誌詳細內容,如伺服器報錯資訊,SQL語句, 使用者執行了某種敏感操作等
private String ip; // 使用者IP地址,無法獲取到預設為 "0.0.0.0"
private String userid; // 使用者名稱,能記錄到誰引發的錯誤就記錄,否則就預設為匿名 "ANONYMOUS"
private Integer target; // 記錄日誌到哪裡(1=資料庫(default),2=檔案,3=資料庫+檔案)
private Integer type;// 日誌種類(1=伺服器執行日誌(default),2=SQL日誌,3=使用者動作日誌)
private Integer level;// 日誌等級(1=普通(default),2=中等,3=警告,4=出錯,5=危險)
private Date logTime; // 日誌記錄的時間

public Log() {
    super();
}

public Log(String title, String content, String ip, String userid,
        Integer target, Integer type, Integer level ) {
    super();
    this.title = title;
    this.content = content;
    this.ip = ip;
    this.userid = userid;
    this.target = target;
    this.type = type;
    this.level = level;

}




public Log(String title, String content) {
    super();
    this.title = title;
    this.content = content;
}




public String getIp() {
    return ip;
}

public void setIp(String ip) {
    if (ip == null || "".equals(ip)) {
        this.ip = "0,0,0,0";
    } else {
        this.ip = ip;
    }
}

public String getUserid() {

    return userid;
}

public void setUserid(String userid) {

    if (userid == null || "".equals(userid)) {
        this.ip = "ANONYMOUS";
    } else {
        this.userid = userid;
    }

}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

public Integer getTarget() {
    return target;
}

public void setTarget(Integer target) {

    if (target == null) {

        this.target = 1;
    } else {
        this.target = target;

    }

}

public Integer getType() {
    return type;
}

public void setType(Integer type) {

    if (type == null) {

        this.type = 1;
    } else {
        this.type = type;

    }
}

public Integer getLevel() {
    return level;
}

public void setLevel(Integer level) {
    if (level == null) {

        this.level = 1;
    } else {
        this.level = level;

    }
}

public Date getLogTime() {
    return logTime;
}

public void setLogTime(Date logTime) {
    if (logTime == null) {
        this.logTime = new Date();

    } else {
        this.logTime = logTime;

    }

}

}

//=================Action 類 ==========

package test;

import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;

/**
* 2017/07/13 此Action用於記錄日誌用,可儲存日誌到資料庫和檔案中去
*
* @author king
*
*/
public class LogAction implements Action {

private Log log;

public String log() {

    if (log == null || log.getTarget() == null) {
        return "NOLOG";
    }
    log.setIp(ServletActionContext.getRequest().getRemoteAddr());
    log.setLogTime(new Date());

    switch (log.getTarget().intValue()) {
    case 1:
        // 儲存日誌到(mysql)資料庫中去
        // xxxdbserice.save(log);
        break;

    case 2:
        // 儲存日誌資訊到文字檔案中去
        // xxxfileserice.save(log);
        break;

    case 3:
        // 同時儲存 日誌到文字檔案到資料庫和檔案中去
        // xxxfileserice.save(log);
        // xxxdbserice.save(log);

        break;

    }

    return "success";

}

public Log getLog() {
    return log;
}

public void setLog(Log log) {
    this.log = log;
}

}

// ============Mysql 資料庫表================
table log:

id int(11),
title varchar(128),
content varchar(1024),
ip varchar(15) default “0,0,0,0”,
userid varchar(64),
type tinyint(2) default 1,
level tinyint(2) default 1,
logTime date