spring log 日志管理
?
?
?
import java.util.Map; import javax.annotation.Resource; import org.aspectj.lang.ProceedingJoinPoint; /** * Logger */ public class Logger { @Resource private LogService logService ; public Object record(ProceedingJoinPoint pjp){ Log log = new Log(); try { ActionContext ac = ActionContext.getContext(); if(ac != null){ Map<String, Object> session = ac.getSession(); if(session != null){ User user = (User) session.get("user"); if(user != null){ log.setOperator("" + user.getId() + ":" + user.getEmail()); } } } String mname = pjp.getSignature().getName(); log.setOperName(mname); Object[] params = pjp.getArgs(); log.setOperParams(StringUtil.arr2Str(params)); Object ret = pjp.proceed(); log.setOperResult("success"); if(ret != null){ log.setResultMsg(ret.toString()); } return ret ; } catch (Throwable e) { log.setOperResult("failure"); log.setResultMsg(e.getMessage()); } finally{ logService.saveEntity(log); } return null ; } }
?
?
<!-- 日志记录仪 --> <bean id="logger" class="com.surveypark.advice.Logger" /> <!-- aop配置 --> <aop:config> <!-- 事务切入点通知 --> <aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Service.*(..))" order="2"/> <!-- 缓存切入点通知 --> <aop:advisor advice-ref="cacheAdvice" pointcut="execution(* *..*Service.*(..))" order="0"/> <!-- Logger切面 --> <aop:aspect id="loggerAspect" ref="logger" order="1"> <aop:around method="record" pointcut="(execution(* *..*Service.save*(..)) or execution(* *..*Service.update*(..)) or execution(* *..*Service.delete*(..)) or execution(* *..*Service.batch*(..)) or execution(* *..*Service.new*(..)) or execution(* *..*Service.move*(..)) or execution(* *..*Service.clear*(..)) or execution(* *..*Service.toggle*(..))) and !bean(logService) "/> </aop:aspect> </aop:config>
?
?
?
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 任务明细bean --> <bean id="jobDetailBean" class="org.springframework.scheduling.quartz.JobDetailBean"> <!-- 通过任务类指定石英任务 --> <property name="jobClass" value="com.surveypark.scheduler.CreateLogTablesTask" /> <property name="jobDataMap"> <map> <entry key="logService" value-ref="logService" /> </map> </property> </bean> <!-- cron触发器bean,设置石英任务的调度策略 --> <bean id="cronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="jobDetailBean" /> <!-- cron表达式 --> <property name="cronExpression"> <value>0 19 10 3 * ?</value> </property> </bean> <!-- 调度工厂bean,激活触发器运行 --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTriggerBean"/> </list> </property> </bean> </beans>
?
public class CreateLogTablesTask extends QuartzJobBean { private LogService logService ; public void setLogService(LogService logService) { this.logService = logService; } protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { String tableName = LogUtil.generateLogTableName(1 ); logService.createLogTable(tableName); tableName = LogUtil.generateLogTableName(2); logService.createLogTable(tableName); tableName = LogUtil.generateLogTableName(3); logService.createLogTable(tableName); } }
??
public void createLogTable(String tableName){ String sql = "create table if not exists " +tableName + " like logs" ; this.executeSQL(sql); }
??
?
?
public class LogUtil { public static String generateLogTableName(int offset){ Calendar c = Calendar.getInstance(); // 2013 int year = c.get(Calendar.YEAR); // 0 -11 int month = c.get(Calendar.MONTH) + 1 + offset; if(month > 12){ year ++ ; month = month - 12 ; } if(month < 1){ year -- ; month = month + 12 ; } DecimalFormat df = new DecimalFormat(); df.applyPattern("00"); return "logs_" + year + "_" + df.format(month) ; } }
?
public void saveEntity(Log t) { //insert into logs_2013_9() String sql = "insert into " + LogUtil.generateLogTableName(0) + "(id,operator,opername,operparams,operresult,resultmsg,opertime) " + "values(?,?,?,?,?,?,?)" ; UUIDHexGenerator uuid = new UUIDHexGenerator(); String id = (String) uuid.generate(null, null); this.executeSQL(sql, id, t.getOperator(), t.getOperName(), t.getOperParams(), t.getOperResult(), t.getResultMsg(), t.getOperTime()); }
??
???
??
?
?
?
?
?
?
?
?
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。