關於用aop方式記錄日誌

2021-08-31 06:28:13 字數 3980 閱讀 1171

最近需要開始剝離日誌邏輯,故想到了aop方式插入日誌。

配置檔案:

<?xml version="1.0" encoding="utf-8"?>

補充一點:如果在pointcut中存在多個表示式,可以用"||"來進行分隔,如:

實際執行日誌插入的類**:

import org.aopalliance.intercept.methodinterceptor;

import org.aopalliance.intercept.methodinvocation;

import org.apache.commons.lang.stringutils;

import org.apache.commons.lang.time.stopwatch;

import org.apache.commons.logging.log;

import org.apache.commons.logging.logfactory;

public class methodtimeadvice implements methodinterceptor

for (int i = 0; i < params.length; i++)

log.info("method:["

+ invocation.getthis().getclass().getname() + "."

+ invocation.getmethod().getname() + "("

+ stringutils.join(******params, ",") + ")]<|>" + "params: "

+ stringutils.join(paramsstr, ",") + "<|>" + "takes: "

+ clock.gettime() + " ms");

return result;

}}

主要將service層和dao層的方法資訊和引數情況還有方法執行時間列印出來。下面還要進一步在logback中配置用jms來將打出的日誌已訊息的形式發出,供消費者接受並集中計算,這個下次再發~ 

開發過程中遇到乙個問題,就是專案中的dao層都採用了繼承帶泛型類的方式,在用cglib做動態**的時候總是報錯,所以在中將原來的proxy-target-class="true"的配置去掉了(當然也要滿足dao層的實現類實現了乙個dao介面才行,開始的**也是上網找的,後來查了下,才將這個配置去掉了,留在這裡,作為備忘)

執行測試結果:

method:[com.cms5.cmsservice.jms.monitor.dao.impl.jmsmonitordaoimpl.countbycondition(jmsmonitorpo)]<|>params: jmsmonitorpo [biz_type=null, error_message_cms_end=null, error_message_product_end=null, error_message_rate_end=null, error_message_receive_end=null, error_time_cms_end=null, error_time_product_end=null, error_time_rate_end=null, error_time_receive_end=null, id=null, is_again=null, mdmc_id=null, mdmc_music_id=null, operate_type=null, state=null, state_cms_begin=null, state_cms_end=null, state_product_begin=null, state_product_end=null, state_rate_begin=null, state_rate_end=null, state_receive_begin=null, state_receive_end=null, success_time_cms_end=null, success_time_product_end=null, success_time_rate_end=null, success_time_receive_end=null, time_cms_begin=null, time_product_begin=null, time_rate_begin=null, time_receive_begin=null, version=null]<|>takes: 3609 ms

method:[com.cms5.cmsservice.jms.monitor.dao.impl.jmsmonitordaoimpl.querybycondition(int,int,jmsmonitorpo)]<|>params: 0,20,jmsmonitorpo [biz_type=null, error_message_cms_end=null, error_message_product_end=null, error_message_rate_end=null, error_message_receive_end=null, error_time_cms_end=null, error_time_product_end=null, error_time_rate_end=null, error_time_receive_end=null, id=null, is_again=null, mdmc_id=null, mdmc_music_id=null, operate_type=null, state=null, state_cms_begin=null, state_cms_end=null, state_product_begin=null, state_product_end=null, state_rate_begin=null, state_rate_end=null, state_receive_begin=null, state_receive_end=null, success_time_cms_end=null, success_time_product_end=null, success_time_rate_end=null, success_time_receive_end=null, time_cms_begin=null, time_product_begin=null, time_rate_begin=null, time_receive_begin=null, version=null]<|>takes: 625 ms

method:[com.cms5.cmsservice.jms.monitor.service.impl.jmsmonitorserviceimpl.getpagedatas(int,int,jmsmonitorpo)]<|>params: 1,20,jmsmonitorpo [biz_type=null, error_message_cms_end=null, error_message_product_end=null, error_message_rate_end=null, error_message_receive_end=null, error_time_cms_end=null, error_time_product_end=null, error_time_rate_end=null, error_time_receive_end=null, id=null, is_again=null, mdmc_id=null, mdmc_music_id=null, operate_type=null, state=null, state_cms_begin=null, state_cms_end=null, state_product_begin=null, state_product_end=null, state_rate_begin=null, state_rate_end=null, state_receive_begin=null, state_receive_end=null, success_time_cms_end=null, success_time_product_end=null, success_time_rate_end=null, success_time_receive_end=null, time_cms_begin=null, time_product_begin=null, time_rate_begin=null, time_receive_begin=null, version=null]<|>takes: 4250 ms

AOP實踐 日誌記錄

aop實踐 自定義註解實現日誌記錄 專案環境springboot spring aop預設是使用aspectj的註解 1.引入jar包 org.springframework.boot spring boot starter aop 2.自定義註解 target retention retentio...

JavaLib 使用AOP幫你記錄日誌

這一次,我們乾脆點,直接進入正題。使用這個模組,你必要加入的包依賴 你需要告訴我,你的切入點 繼承lbaseweblogaspect,並實現pointcut 方法,配置你的切入點 寫乙個介面進行測試,我們還是使用上一次的切口吧 完整 我們放大點,看得仔細 日誌主要記錄以下資訊 請求客戶端ip 請求u...

Lpp PHP的日誌記錄方式

1.採用檔案記錄,2.依靠系統的服務資訊幫助記錄 1.採用檔案記錄日誌 1 配置 在php.ini配置檔案中配置如下資訊 error reporting e all 將向php傳送每個錯誤 display errors off 不顯示錯誤報告 log errors on 決定日誌語句記錄的位置。lo...