package com.cloud.day3; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; /** * 正常日志记录实体类 * 使用注解的方式和数据库的表关联起来 */ @Entity @Table(name="systemlog") public class SystemLog { private String logId; private String modelName; private String codeEvent; private String userID; private String systemType; private String remark; private String date; public SystemLog(){} public SystemLog(String logId, String modelName, String codeEvent, String userID, String systemType, String remark) { this.logId = logId; this.modelName = modelName; this.codeEvent = codeEvent; this.userID = userID; this.systemType = systemType; this.remark = remark; } /*策略生成器*/ @GenericGenerator(name="generator",strategy="uuid.hex") @GeneratedValue(generator="generator") @Id @Column(name="log_id",unique=true,nullable=false,length=32) public String getLogId() { return logId; } public void setLogId(String logId) { this.logId = logId; } @Column(name="model_name",length=100) public String getModelName() { return modelName; } public void setModelName(String modelName) { this.modelName = modelName; } @Column(name="code_event",length=50) public String getCodeEvent() { return codeEvent; } public void setCodeEvent(String codeEvent) { this.codeEvent = codeEvent; } @Column(name="user_id",length=32) public String getUserID() { return userID; } public void setUserID(String userID) { this.userID = userID; } @Column(name="system_type",length=20) public String getSystemType() { return systemType; } public void setSystemType(String systemType) { this.systemType = systemType; } @Column(name="remark",length=200) public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } @Column(name="date",length=20) public String getDate() { return date; } public void setDate(String date) { this.date = date; } } |
package com.cloud.day3; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; /** * 异常日志记录实体类 * 使用注解的方式和数据库的表关联起来 */ @Entity @Table(name="exceptionlog") public class ExceptionLog { private String logId; private String className; private String methodName; private String exceptionName; private String desException; private String remark; public ExceptionLog(){} public ExceptionLog(String className, String methodName, String exceptionName, String desException, String remark) { this.className = className; this.methodName = methodName; this.exceptionName = exceptionName; this.desException = desException; this.remark = remark; } @GenericGenerator(name="generator",strategy="uuid.hex") @GeneratedValue(generator="generator") @Id @Column(name="log_id",unique=true,nullable=false,length=32) public String getLogId() { return logId; } public void setLogId(String logId) { this.logId = logId; } @Column(name="class_name",length=20) public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } @Column(name="method_name",length=20) public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName; } @Column(name="exception_name",length=20) public String getExceptionName() { return exceptionName; } public void setExceptionName(String exceptionName) { this.exceptionName = exceptionName; } @Column(name="des_exception",length=1000) public String getDesException() { return desException; } public void setDesException(String desException) { this.desException = desException; } @Column(name="remark",length=200) public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } } |
package com.cloud.day2; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.orm.hibernate3.HibernateTemplate; public class BaseJdbcSupport { private HibernateTemplate hibernateTemplate; private JdbcTemplate jdbcTemplate; public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } } |
package com.cloud.day3; public interface LogServiceInte { /** * 处理正常情况下的日志记录 * @param modelName 模块名称:商品模块,用户模块,等等 * @param codeEvent 事件名称:查询,删除,添加,等等 * @param date 处理时间 * @param userID 操作的用户 * @param systemType 操作系统:APP,接口,web端 */ public void saveLog(String modelName,String codeEvent,String date,String userID,String systemType); /** * 发生异常的情况下的日志记录 * @param className 类名 * @param methodName 方法名 * @param exceptionName 异常名 * @param desException 异常描述 * @param remark 备注 */ public void saveException(String className,String methodName,String exceptionName,String desException,String remark); } |
package com.cloud.day3; import java.text.SimpleDateFormat; import com.cloud.day2.BaseJdbcSupport; public class LogServiceImpl extends BaseJdbcSupport implements LogServiceInte{ /** * 保存正常情况下的日志记录 * calss:SystemLog */ @Override public void saveLog(String modelName, String codeEvent, String date, String userID, String systemType) { try{ SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SystemLog systemLog = new SystemLog(); systemLog.setModelName(modelName); systemLog.setDate(time.parse(date).toString()); systemLog.setUserID(userID); systemLog.setSystemType(systemType); this.getHibernateTemplate().save(systemLog); } catch(Exception e){ e.printStackTrace(); } } /** * 保存异常情况下日志记录 * class:ExceptionLog */ @Override public void saveException(String className, String methodName, String exceptionName, String desException, String remark) { try{ /** * 如果异常的描述过长,则截取部分 */ if(desException!=null&&desException.length()>1000){ desException = desException.substring(0, 1000); } ExceptionLog exceptionLog = new ExceptionLog(className, methodName, exceptionName, desException, remark); this.getHibernateTemplate().save(exceptionLog); } catch(Exception e){ e.printStackTrace(); } } } |