java注释日志打印_java 注解结合 spring aop 实现自动输出日志
auto-log
auto-log 是一款為 java 設計的自動日志監控框架。
創作目的
經常會寫一些工具,有時候手動加一些日志很麻煩,引入 spring 又過于大材小用。
所以希望從從簡到繁實現一個工具,便于平時使用。
特性
基于注解+字節碼,配置靈活
自動適配常見的日志框架
支持編程式的調用
支持注解式,完美整合 spring
支持整合 spring-boot
支持慢日志閾值指定,耗時,入參,出參,異常信息等常見屬性指定
核心原理
注解定義
import java.lang.annotation.*;
/**
* 自動注解
* @author binbin.hou
* @since 0.0.1
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface AutoLog {
/**
* 輸出參數
* @return 參數
* @since 0.0.1
*/
boolean param() default true;
/**
* 是否輸出結果
* @return 結果
* @since 0.0.1
*/
boolean result() default true;
/**
* 是否輸出時間
* @return 耗時
* @since 0.0.1
*/
boolean costTime() default false;
/**
* 是否輸出異常信息
* @return 是否
* @since 0.0.6
*/
boolean exception() default true;
/**
* 慢日志閾值
*
* 當值小于 0 時,不進行慢日志統計。
* 當值大于等于0時,當前值只要大于等于這個值,就進行統計。
* @return 閾值
* @since 0.0.4
*/
long slowThresholdMills() default -1;
}
復制代碼
核心 AOP 實現
這里的 LogFactory 類是關鍵,可以兼容目前大部分的日志框架。
import com.github.houbb.auto.log.annotation.AutoLog;
import com.github.houbb.heaven.response.exception.CommonRuntimeException;
import com.github.houbb.log.integration.core.Log;
import com.github.houbb.log.integration.core.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* 這是一種寫法
* 自動日志輸出 aop
* @author binbin.hou
* @since 0.0.3
*/
@Aspect
@Component
@EnableAspectJAutoProxy
public class AutoLogAop{
private static final Log LOG = LogFactory.getLog(AutoLogAop.class);
/**
* 執行核心方法
*
* 相當于 MethodInterceptor
* @param point 切點
* @param autoLog 日志參數
* @return 結果
* @throws Throwable 異常信息
* @since 0.0.3
*/
@Around("@annotation(autoLog)")
public Object around(ProceedingJoinPoint point, AutoLog autoLog) throws Throwable{
Method method = getCurrentMethod(point);
String methodName = method.getName();
try {
final long startMills = System.currentTimeMillis();
//1. 是否輸入入參
if (autoLog.param()) {
LOG.info("{} param is {}.", methodName, Arrays.toString(point.getArgs()));
}
//2. 執行方法
Object result = point.proceed();
//3. 結果
if (autoLog.result()) {
LOG.info("{} result is {}.", methodName, result);
}
//3.1 耗時
final long slowThreshold = autoLog.slowThresholdMills();
if (autoLog.costTime() || slowThreshold >= 0) {
final long endMills = System.currentTimeMillis();
long costTime = endMills - startMills;
if (autoLog.costTime()) {
LOG.info("{} cost time is {}ms.", methodName, costTime);
}
//3.2 慢日志
if (slowThreshold >= 0 && costTime >= slowThreshold) {
LOG.warn("{} is slow log, {}ms >= {}ms.", methodName, costTime, slowThreshold);
}
}
return result;
} catch (Throwable e) {
if(autoLog.exception()) {
LOG.error("{} meet ex.", methodName, e);
}
throw e;
}
}
/**
* 獲取當前方法信息
*
* @param point 切點
* @return 方法
*/
private Method getCurrentMethod(ProceedingJoinPoint point){
try {
Signature sig = point.getSignature();
MethodSignature msig = (MethodSignature) sig;
Object target = point.getTarget();
return target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
} catch (NoSuchMethodException e) {
throw new CommonRuntimeException(e);
}
}
}
復制代碼
快速開始
maven 引入
com.github.houbb
auto-log-core
${最新版本}
復制代碼
入門案例
UserService userService = AutoLogHelper.proxy(new UserServiceImpl());
userService.queryLog("1");
復制代碼
日志如下
[INFO] [2020-05-29 16:24:06.227] [main] [c.g.h.a.l.c.s.i.AutoLogMethodInterceptor.invoke] - public java.lang.String com.github.houbb.auto.log.test.service.impl.UserServiceImpl.queryLog(java.lang.String) param is [1]
[INFO] [2020-05-29 16:24:06.228] [main] [c.g.h.a.l.c.s.i.AutoLogMethodInterceptor.invoke] - public java.lang.String com.github.houbb.auto.log.test.service.impl.UserServiceImpl.queryLog(java.lang.String) result is result-1
復制代碼
代碼
其中方法實現如下:
UserService.java
public interface UserService{
String queryLog(final String id);
}
復制代碼
UserServiceImpl.java
直接使用注解 @AutoLog 指定需要打日志的方法即可。
public class UserServiceImpl implements UserService{
@Override
@AutoLog
public String queryLog(String id){
return "result-"+id;
}
}
復制代碼
注解說明
核心注解 @AutoLog 的屬性說明如下:
屬性類型默認值說明parambooleantrue是否打印入參
resultbooleantrue是否打印出參
costTimebooleanfalse是否打印耗時
exceptionbooleantrue是否打印異常
slowThresholdMillslong-1當這個值大于等于 0 時,且耗時超過配置值,會輸出慢日志
spring 整合使用
注解聲明
使用 @EnableAutoLog 啟用自動日志輸出
@Configurable
@ComponentScan(basePackages = "com.github.houbb.auto.log.test.service")
@EnableAutoLog
public class SpringConfig{
}
復制代碼
測試代碼
@ContextConfiguration(classes = SpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringServiceTest{
@Autowired
private UserService userService;
@Test
public void queryLogTest(){
userService.queryLog("1");
}
}
復制代碼
輸出結果
信息: public java.lang.String com.github.houbb.auto.log.test.service.impl.UserServiceImpl.queryLog(java.lang.String) param is [1]
五月 30, 2020 12:17:51 下午 com.github.houbb.auto.log.core.support.interceptor.AutoLogMethodInterceptor info
信息: public java.lang.String com.github.houbb.auto.log.test.service.impl.UserServiceImpl.queryLog(java.lang.String) result is result-1
五月 30, 2020 12:17:51 下午 org.springframework.context.support.GenericApplicationContext doClose
復制代碼
開源地址
歡迎 fork/star~
總結
以上是生活随笔為你收集整理的java注释日志打印_java 注解结合 spring aop 实现自动输出日志的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 几款经典好用的Android,经典实用
- 下一篇: mysql left 数学原理,MySQ