項目整合koala業務日誌子系統

來源:互聯網
上載者:User

標籤:des   style   http   java   color   使用   

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=3000log.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.businessLogConfigclass 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



相關關鍵詞:
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.