Spring在项目中记录日志

本文介绍了一种基于AOP(面向切面编程)的操作日志记录机制,通过定义OperationLog类来存储操作日志信息,并利用OperationLogHandler类进行日志记录。根据不同类型的业务操作(如保存、更新和删除),系统会自动记录操作者、操作时间、IP地址等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

你可以根据自己的需求自定义,需要记录那些,该例子,只对增删改做记录 。

public class OperationLog {

	private String id;
	
	private Date operateTime;
	
	private String operator;
	
	private String ipAddress;
	
	private String module;
	
	private String node;
	
	private String type;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Date getOperateTime() {
		return operateTime;
	}

	public void setOperateTime(Date operateTime) {
		this.operateTime = operateTime;
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	
}
 
//这个类就是AOP中的A,Aspect
//将横切性关注点模块化(分离出来),分离出来的这个类就是Aspect
public class OperationLogHandler {
	
//	private Map<String, String> moduleMap = new HashMap<String, String>();
//	{
//		moduleMap.put("/org", "机构维护");
//		moduleMap.put("/emp", "员工维护");
//		moduleMap.put("/permisssion", "许可维护");
//		moduleMap.put("/role", "角色维护");
//	}
 
	private Map<String, String> moduleMap;
	
	private OperationLogService operationLogService;
	
	public void setModuleMap(Map<String, String> moduleMap) {
		this.moduleMap = moduleMap;
	}

	public void setOperationLogService(OperationLogService operationLogService) {
		this.operationLogService = operationLogService;
	}

	//这个方法就是AOP中的Advice,横切性关注点的具体实现
	//JoinPoint称为连接点,Advice执行的时机(或执行的地方)
	public void handle(JoinPoint joinPoint) {
		//System.out.println("saveOperationLog");
		OperationLog operationLog = new OperationLog();
		operationLog.setOperateTime(new Date());
		
        String sessionId = ServletActionContext.getRequest().getParameter("sessionId");
        // 根据sessionid获取session
        MySessionContext myc = MySessionContext.getInstance();
        HttpSession sess = myc.getSession(sessionId);
		
//		User user = (User)ServletActionContext.getRequest().getSession().getAttribute("USER");
		operationLog.setOperator((String) sess.getAttribute("USER"));
		operationLog.setIpAddress(ServletActionContext.getRequest().getRemoteAddr());
		String namespace = ServletActionContext.getActionMapping().getNamespace();
		operationLog.setModule(moduleMap.get(namespace));
		String node = joinPoint.getTarget().getClass().getName() + "-->" + joinPoint.getSignature().getName(); 
		operationLog.setNode(node);
		if (joinPoint.getSignature().getName().startsWith("save")) {
			operationLog.setType("保存");
		}else if (joinPoint.getSignature().getName().startsWith("update")) {
			operationLog.setType("更新");
		}else {
			operationLog.setType("删除");
		}
		operationLogService.save(operationLog);
	}

}
 
public interface OperationLogService {

	public void save(OperationLog operationLog);
}
 
public class OperationLogServiceImpl implements OperationLogService {

	private OperationLogDao operationLogDao;
	
	public void setOperationLogDao(OperationLogDao operationLogDao) {
		this.operationLogDao = operationLogDao;
	}

	@Override
	public void save(OperationLog operationLog) {
		operationLogDao.save(operationLog);
	}
}
 
public interface OperationLogDao {

	public void save(OperationLog operationLog);
}
 
public class OperationLogDaoImpl extends HibernateDaoSupport implements OperationLogDao {

    /**
     * @param operationLog
     * @see com.acca.dao.OperationLogDao#save(com.acca.entity.OperationLog)
     */
    @Override
    public void save(OperationLog operationLog) {
        getHibernateTemplate().save(operationLog);
        
    }
}
 
<bean id="operationLogService" class="com.acca.service.impl.OperationLogServiceImpl">
 <property name="operationLogDao" ref="operationLogDao"></property>
</bean>
	<bean id="operationLogHandler" class="com.acca.service.impl.OperationLogHandler">
		<property name="moduleMap">
			<map>
			    <entry key="/" value="数据导入"/>
				<entry key="/org" value="机构维护"/>
				<entry key="/emp" value="员工维护"/>
				<entry key="/permission" value="许可维护"/>
				<entry key="/role" value="角色维护"/>
				<entry key="/user" value="用户维护"/>
				<entry key="/announcement" value="公告管理"/>
			</map>
		</property>
		<property name="operationLogService" ref="operationLogService"/>
	</bean>
	<aop:config>
		<aop:aspect id="operationLogAspect" ref="operationLogHandler">
			<aop:pointcut id="serviceMethod" expression="(execution(* com.acca.service.*.save*(..)) || execution(* com.acca.service.*.load*(..)) || execution(* com.acca.service.*.delete*(..)))  and  not execution(* com.acca.service.OperationLogService.*(..))"/>
			<aop:after pointcut-ref="serviceMethod" method="handle"/>
		</aop:aspect>
	</aop:config>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值