java注释日志打印_java 注解结合 spring aop 实现自动输出日志
auto-log
auto-log 是一款為 java 設(shè)計(jì)的自動(dòng)日志監(jiān)控框架。
創(chuàng)作目的
經(jīng)常會(huì)寫一些工具,有時(shí)候手動(dòng)加一些日志很麻煩,引入 spring 又過于大材小用。
所以希望從從簡到繁實(shí)現(xiàn)一個(gè)工具,便于平時(shí)使用。
特性
基于注解+字節(jié)碼,配置靈活
自動(dòng)適配常見的日志框架
支持編程式的調(diào)用
支持注解式,完美整合 spring
支持整合 spring-boot
支持慢日志閾值指定,耗時(shí),入?yún)?#xff0c;出參,異常信息等常見屬性指定
核心原理
注解定義
import java.lang.annotation.*;
/**
* 自動(dòng)注解
* @author binbin.hou
* @since 0.0.1
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface AutoLog {
/**
* 輸出參數(shù)
* @return 參數(shù)
* @since 0.0.1
*/
boolean param() default true;
/**
* 是否輸出結(jié)果
* @return 結(jié)果
* @since 0.0.1
*/
boolean result() default true;
/**
* 是否輸出時(shí)間
* @return 耗時(shí)
* @since 0.0.1
*/
boolean costTime() default false;
/**
* 是否輸出異常信息
* @return 是否
* @since 0.0.6
*/
boolean exception() default true;
/**
* 慢日志閾值
*
* 當(dāng)值小于 0 時(shí),不進(jìn)行慢日志統(tǒng)計(jì)。
* 當(dāng)值大于等于0時(shí),當(dāng)前值只要大于等于這個(gè)值,就進(jìn)行統(tǒng)計(jì)。
* @return 閾值
* @since 0.0.4
*/
long slowThresholdMills() default -1;
}
復(fù)制代碼
核心 AOP 實(shí)現(xiàn)
這里的 LogFactory 類是關(guān)鍵,可以兼容目前大部分的日志框架。
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;
/**
* 這是一種寫法
* 自動(dòng)日志輸出 aop
* @author binbin.hou
* @since 0.0.3
*/
@Aspect
@Component
@EnableAspectJAutoProxy
public class AutoLogAop{
private static final Log LOG = LogFactory.getLog(AutoLogAop.class);
/**
* 執(zhí)行核心方法
*
* 相當(dāng)于 MethodInterceptor
* @param point 切點(diǎn)
* @param autoLog 日志參數(shù)
* @return 結(jié)果
* @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. 是否輸入入?yún)?/p>
if (autoLog.param()) {
LOG.info("{} param is {}.", methodName, Arrays.toString(point.getArgs()));
}
//2. 執(zhí)行方法
Object result = point.proceed();
//3. 結(jié)果
if (autoLog.result()) {
LOG.info("{} result is {}.", methodName, result);
}
//3.1 耗時(shí)
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;
}
}
/**
* 獲取當(dāng)前方法信息
*
* @param point 切點(diǎn)
* @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);
}
}
}
復(fù)制代碼
快速開始
maven 引入
com.github.houbb
auto-log-core
${最新版本}
復(fù)制代碼
入門案例
UserService userService = AutoLogHelper.proxy(new UserServiceImpl());
userService.queryLog("1");
復(fù)制代碼
日志如下
[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
復(fù)制代碼
代碼
其中方法實(shí)現(xiàn)如下:
UserService.java
public interface UserService{
String queryLog(final String id);
}
復(fù)制代碼
UserServiceImpl.java
直接使用注解 @AutoLog 指定需要打日志的方法即可。
public class UserServiceImpl implements UserService{
@Override
@AutoLog
public String queryLog(String id){
return "result-"+id;
}
}
復(fù)制代碼
注解說明
核心注解 @AutoLog 的屬性說明如下:
屬性類型默認(rèn)值說明parambooleantrue是否打印入?yún)?/p>
resultbooleantrue是否打印出參
costTimebooleanfalse是否打印耗時(shí)
exceptionbooleantrue是否打印異常
slowThresholdMillslong-1當(dāng)這個(gè)值大于等于 0 時(shí),且耗時(shí)超過配置值,會(huì)輸出慢日志
spring 整合使用
注解聲明
使用 @EnableAutoLog 啟用自動(dòng)日志輸出
@Configurable
@ComponentScan(basePackages = "com.github.houbb.auto.log.test.service")
@EnableAutoLog
public class SpringConfig{
}
復(fù)制代碼
測(cè)試代碼
@ContextConfiguration(classes = SpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringServiceTest{
@Autowired
private UserService userService;
@Test
public void queryLogTest(){
userService.queryLog("1");
}
}
復(fù)制代碼
輸出結(jié)果
信息: 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
復(fù)制代碼
開源地址
歡迎 fork/star~
總結(jié)
以上是生活随笔為你收集整理的java注释日志打印_java 注解结合 spring aop 实现自动输出日志的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 几款经典好用的Android,经典实用
- 下一篇: mysql left 数学原理,MySQ