1、使用 MessageFormat 格式化文本
int planet = 7;
String event = "a disturbance in the Force";
String result = MessageFormat.format(
"At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
planet, new Date(), event);
The output is:
At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.
2、自定义注解
定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperateLog {
/**
* 模块名称
* @return
*/
String moudleName() default "";
/**
* 操作名称
* @return
*/
String optName() default "";
/**
* 业务类型描述
* @return
*/
String description() default "";
}
通过 aop 做拦截处理
@Aspect
@Component
public class OperateLogAspect {
@Autowired
private OperateLogService operateLogService;
@Pointcut("@annotation(com.youngcms.core.annotation.OperateLog)")
public void operateLogCut(){
}
@Around("operateLogCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
long beginTime = System.currentTimeMillis();
//执行方法
Object result = point.proceed();
//执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
//保存日志
saveOperateLog(point, time);
return result;
}
private void saveOperateLog(ProceedingJoinPoint joinPoint, long time) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
RequestMapping classRequestMapping=method.getDeclaringClass().getAnnotation(RequestMapping.class);
OperateLog operateLog=method.getAnnotation(OperateLog.class);
RequestMapping mothodRequestMapping= method.getAnnotation(RequestMapping.class);
com.youngcms.bean.OperateLog operateLogBean=new com.youngcms.bean.OperateLog();
if(operateLog!=null){
operateLogBean.setModuleName(operateLog.moudleName());
operateLogBean.setOptName(operateLog.optName());
operateLogBean.setDescription(operateLog.description());
}
//请求的类名
String className = joinPoint.getTarget().getClass().getName();
//请求的方法名
String methodName = signature.getName();
//请求的参数
//Object[] args = joinPoint.getArgs();
operateLogBean.setIp(IPUtils.getIpAddr(HttpKit.getRequest()));
operateLogBean.setTime(time);
operateLogBean.setMethod(className+"."+methodName+"()");
operateLogBean.setUrl(classRequestMapping.value()[0]+mothodRequestMapping.value()[0]);
String username = ((SysUser) SecurityUtils.getSubject().getPrincipal()).getRealName();
operateLogBean.setAuthor(username);
operateLogBean.setCreateTime(DateUtil.dateToStr(new Date(), 12));
operateLogService.insert(operateLogBean);
}
}
233
原文:https://www.cnblogs.com/lemos/p/10829080.html