定义注解log
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
/*
* 定义注解 log
* 注解中含有一个元素 description
* description 元素 有默认值 "no description"
*/
public @interface Log {
public String description() default "no description";
}
配置操作日志拦截器
class="类名">
操作日志类
import java.lang.reflect.Method;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class OperationLogInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
@Resource(name=TbloperatelogService.OperateLog_SERVICE_IMPL)
protected TbloperatelogService operateLogService ;
/**
* 操作日志拦截器
*
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public String intercept(ActionInvocation invocation) throws Exception {
String actionName="";
String methodName="";
System.out.println("操作日志开始启动..");
Class clazz = invocation.getAction().getClass();
Log annot;
if(clazz.isAnnotationPresent(Log.class)){
annot = (Log) clazz.getAnnotation(Log.class);
actionName = annot.description();
}
String methods = invocation.getProxy().getMethod();
Method method= clazz.getDeclaredMethod(methods, null);
if(method.isAnnotationPresent(Log.class)){
annot = (Log) method.getAnnotation(Log.class);
methodName = annot.description();
}
String result = invocation.invoke();
boolean executeresult = invocation.getProxy().getExecuteResult();
// 取得请求相关的ActionContext实例
ActionContext ctx = invocation.getInvocationContext();
HttpServletRequest request = ServletActionContext.getRequest();
Map session = ctx.getSession();
Tbluser user=null;
if(null!=session.get("tbluser")){
user = (Tbluser) session.get("tbluser");
}
//保存操作日志内容Tbloperatelog
Tbloperatelog operateLog = new Tbloperatelog();
if (user != null) {
operateLog.setUsername(user.getId().toString());
operateLog.setOreratetime(DateUtil.getCurrentT());
//系统名称
operateLog.setSysname(actionName);
if (!method.equals("")&&methodName.contains("|")) {
String str = methodName;
int numone = str.indexOf("|");
//模块功能名称
if(numone!=-1){
String moduleName = str.substring(0, numone);
operateLog.setModulename(moduleName);
}else{
//operateLog.setModulename(str);
}
str=str.substring(numone+1, str.length());
//操作名称
int numtwo = str.indexOf("|");
if(numtwo!=-1){
String operName = str.substring(0, numtwo);
operateLog.setOpername(operName);
}else{
operateLog.setOpername(str);
}
}else{
operateLog.setOpername(methodName);
}
//if (executeresult) {//0代表成功;1代表失败
//operateLog.setOperState(0);
//}else {
//operateLog.setOperState(1);
//}
//acton和method的名字不为空时运行存储
if(!"".equals(actionName)&&!"".equals(methodName)){
try {
operateLogService.save(operateLog);
} catch (Exception e) {
e.printStackTrace();
}
}
}else{
//退出
}
return Action.SUCCESS;
}
}
使用方式
@Log(description="系统模块")
如果是多个中间用|分隔
@Log(description="系统模块|模块|操作")