java后台注解式日志,利用注解记录日志

日志记录简化,只需要在action或者controller的方法上加logging注解即可。注解:@Logging(description = "{username}登录"),description是注解内容,{}中为动态参数,是传入该方法中的指定po的属性。

注解类代码:

package cn.com.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 日志注解
 * @author ZhangShaobo
 * @date 2017-09-04
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Logging {
	
	String description();
	
}

具体的日志实现类,该类中loginfo方法需要在拦截器中调用。

package cn.com.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.annotation.Logging;

/**
 * 日志注解的具体实现类
 * @author ZhangShaobo
 * @date 2018-09-04
 */
public class LogAnnoUtil {
	
	private static Logger log = LoggerFactory.getLogger(LogAnnoUtil.class);
	
	/**
	 * 根据注解记录日志
	 * @param t action对象
	 * @param methodName 调用的action方法名称
	 * @param map 参数map
	 */
	public static <T> void loginfo(T t, String methodName, Map map){
		Method [] methods = t.getClass().getDeclaredMethods();
    	for (final Method m : methods) {
    		if (m.getName().equals(methodName)) {
    			if(m.isAnnotationPresent(Logging.class)){
    				String desc = m.getAnnotation(Logging.class).description();
    				List<String> list = descFormat(desc);
    				for (String s : list) {
						String value = map.get(s).toString();
						desc = desc.replace("{"+s+"}", value);
					}
    				// 暂时只是输出到了控制台,具体需要存库或者存缓存,需要改这段代码
    				log.info(desc);
    			}
			}
			
		}
	}
	
	/**
	 * 获取日志信息中的动态参数,然后替换
	 * @param desc
	 * @return
	 */
	private static List<String> descFormat(String desc){
		List<String> list = new ArrayList<>();
		Pattern pattern = Pattern.compile("\\{([^\\}]+)\\}"); 
        Matcher matcher = pattern.matcher(desc); 
        while(matcher.find()){ 
            String t = matcher.group(1); 
            list.add(t);
        }
		return list;
	}
	
	
}

拦截器中写调用,以上代码实现的是在struts框架中用注解记录日志,springmvc或者springboot需要适当更改代码。有好的建议或者有不明白的地方欢迎评论沟通!




  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
后台管理系统页面操作日志的设计与代码实现可以参考以下步骤: 1. 设计数据库表结构 可以创建一个名为“sys_log”的表来保存操作日志。表中可以包含以下字段:日志ID、操作人员、操作时间、操作模块、操作类型、操作对象、操作结果等。 2. 编写AOP切面 使用AOP技术,在系统中切入日志记录代码。在AOP切面中,可以通过注解或者切入点来确定需要记录日志的方法,并在方法执行前后记录相关操作日志信息。 3. 编写日志记录代码 在AOP切面中编写日志记录代码,将日志信息保存到数据库中。可以使用Spring JDBC或MyBatis等数据库操作框架来进行数据持久化操作。 4. 集成日志管理模块 可以在系统中集成日志管理模块,将保存的日志信息展示给管理员。管理员可以根据需求查询、导出、删除等日志操作。 以下是一个简单的AOP切面示例,用于记录操作日志: ```java @Component @Aspect public class LogAspect { @Autowired private LogService logService; @Pointcut("execution(* com.example.controller.*.*(..))") public void logPointcut() {} @AfterReturning(pointcut = "logPointcut()") public void doAfterReturning(JoinPoint joinPoint) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); User user = (User) request.getSession().getAttribute("user"); if (user != null) { String username = user.getUsername(); String ip = request.getRemoteAddr(); String method = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName(); String params = Arrays.toString(joinPoint.getArgs()); String operation = "操作描述"; String result = "操作结果"; Log log = new Log(username, ip, method, params, operation, result); logService.saveLog(log); } } } ``` 在上述代码中,@Pointcut注解用于定义切入点,@AfterReturning注解用于定义在方法执行后记录日志的操作。在记录日志时,可以获取当前用户、请求IP、请求方法、请求参数等信息,并将这些信息保存到数据库中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值