首页 > 代码库 > 项目集成koala业务日志子系统
项目集成koala业务日志子系统
Koala平台的业务日志子系统是基于Maven的项目模块,最方便的集成是项目也使用Maven,war项目集成请自行下载jar包
前提
依赖spring
添加仓库
<repositories> <repository> <id>koala-releases</id> <url>http://nexus.openkoala.org/content/repositories/public-releases/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>koala-snapshots</id> <url>http://nexus.openkoala.org/content/repositories/public-snapshots/</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>
添加依赖
<dependency> <groupId>org.openkoala.businesslog</groupId> <artifactId>koala-businesslog-api</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.openkoala.businesslog</groupId> <artifactId>koala-businesslog-impl</artifactId> <version>3.0.0</version> </dependency>
根据项目实际分层情况添加依赖到对应的pom.xml,Koala项目把依赖添加到application-impl层
创建LogFilter类
例如com.xiaokaceng.demo.web.controller.businesslog.LogFilter.java
package com.xiaokaceng.demo.web.controller.businesslog; import org.openkoala.businesslog.utils.BusinessLogServletFilter; import javax.servlet.*; public class LogFilter extends BusinessLogServletFilter { /** * 将需要用到的信息放入日志上下文 * * @param req * @param resp * @param chain */ @Override public void beforeFilter(ServletRequest req, ServletResponse resp, FilterChain chain) { addIpContext(getIp(req)); // TODO 需要自己实现获取用户名 addUserContext("xxx"); } public void init(FilterConfig filterConfig) throws ServletException { //To change body of implemented methods use File | Settings | File Templates. } public void destroy() { //To change body of implemented methods use File | Settings | File Templates. } }
注意:当前用户需根据系统实现来获取
配置web.xml
<filter> <filter-name>LogFilter</filter-name> <filter-class>com.xiaokaceng.demo.web.controller.businesslog.LogFilter</filter-class> </filter> <filter-mapping> <filter-name>LogFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
类路径下创建koala-businesslog.properties
pointcut=execution(* org.openkoala.example.application.impl.*.*(..)) #日志开关 kaola.businesslog.enable=true #日志导出器 businessLogExporter=org.openkoala.businesslog.utils.BusinessLogExporterImpl #数据库设置 log.db.jdbc.driver=${db.jdbcDriver} log.db.jdbc.connection.url=${db.connectionURL} log.db.jdbc.username=${db.username} log.db.jdbc.password=${db.password} log.db.jdbc.dialect=${hibernate.dialect} log.hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto} log.hibernate.show_sql=${hibernate.show_sql} log.db.Type=${db.Type} db.generateDdl=${generateDdl} log.maximumConnectionCount=3000 log.minimumConnectionCount=100 #线程池配置 #核心线程数 log.threadPool.corePoolSize=100 #最大线程数 log.threadPool.maxPoolSize=3000 #队列最大长度 log.threadPool.queueCapacity=2000 #线程池维护线程所允许的空闲时间 log.threadPool.keepAliveSeconds=300 #线程池对拒绝任务(无线程可用)的处理策略 log.threadPool.rejectedExecutionHandler=java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy
pointcut(切入点):通过切面记录业务日志
businessLogExporter(业务日志输出):指定采用那种方式输出,默认输出是记录到数据库,可扩展BusinessLogExporter接口实现自定义输出
整合业务功能
例如某接口实现类PersonInfoApplicationImpl.java,需要对新增、删除方法进行业务日志记录。代码如下:
@MethodAlias("save") public PersonInfoDTO savePersonInfo(PersonInfoDTO personInfoDTO) { PersonInfo personInfo = new PersonInfo(); try { BeanUtils.copyProperties(personInfo, personInfoDTO); } catch (Exception e) { e.printStackTrace(); } personInfo.save(); personInfoDTO.setId((java.lang.Long)personInfo.getId()); return personInfoDTO; } @MethodAlias("remove") public void removePersonInfo(Long id) { this.removePersonInfos(new Long[] { id }); }
在需要记录的业务日志的方法上打上@MethodAlias的注解,value的值必须是唯一的,命名规则必须符合java方法命名
- 类路径下创建businessLogConfig文件夹
用于存放处理业务日志的groovy文件
- businessLogConfig文件夹下创建groovy文件
文件名可以随意,只要符合java类命名规则。如PersonInfoApplicationImpl.groovy
package vm.other.businesslog_resources.businessLogConfig
class PersonInfoApplicationImpl {
def context
def save() {
"${getPreTemplate()}:创建个人信息,名字为:${context._param0.name}"
}
def remove() {
"${getPreTemplate()}:删除个人信息"
}
def getPreTemplate(){
"${context._user}-"
}
}
这里的方法名必须与@MethodAlias的value一一对应
- 引入Spring配置
<import resource="classpath*:koala-businesslog-aop.xml"></import>
<import resource="classpath*:koala-businesslog-shared-persistence.xml"></import>
- 添加packagesToScan
<value>org.openkoala.businesslog.model</value>
演示
业务功能访问地址:http://localhost:8080/pages/example/PersonInfo-list.jsp
业务日志功能访问地址:http://localhost:8080/pages/log/index.jsp
Demo下载地址:http://pan.baidu.com/s/1qWufvkC