java 日志操作日志_java 操作日志 log

定义注解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="系统模块|模块|操作")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值