package com.ninerapoint.tms.config;

import com.xxl.job.core.context.XxlJobHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * xxljob 页面日志增强
 */
public class LoggerEnhancer {
 
    private static final Map<Class<?>, Logger> LOGGER_CACHE = new HashMap<>();
 
    private final static List<String> ENHANCE_METHOD_LIST = Arrays.asList("info", "debug", "error");
 
    public static Logger getLogger(Class<?> clazz) {
        if (LOGGER_CACHE.containsKey(clazz)) {
            return LOGGER_CACHE.get(clazz);
        }
        Logger logger = LoggerFactory.getLogger(clazz);
        LOGGER_CACHE.put(clazz, logger);
        return logger;
    }
 
    public static Logger createLogger(Class<?> clazz) {
        Object proxy = Enhancer.create(Logger.class, new LoggerMethodInterceptor(clazz));
        return (Logger) proxy;
    }
 
    public static class LoggerMethodInterceptor implements MethodInterceptor {
 
        private final Class<?> clazz;
 
        public LoggerMethodInterceptor(Class<?> clazz) {
            this.clazz = clazz;
        }
 
        @Override
        public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
            if (ENHANCE_METHOD_LIST.contains(method.getName()) && objects != null && objects.length  > 0) {
                Object[] args = new Object[0];
                if (objects.length > 1) {
 
                    // 超过3个参数时 logger 通过不定参数形式重载
                    if (method.getParameterCount() == 2 && method.getParameters()[1].getType().isArray()) {
                        args = (Object[]) objects[1];
                    }else {
                        args = new Object[objects.length - 1];
                        System.arraycopy(objects, 1, args, 0, args.length);
                    }
                }
                String temp = (String) objects[0];
                log(method.getName(), temp, args);
            }
            return null;
 
        }
 
        public void log(String methodName, String str, Object... args) {
            Logger logger = getLogger(clazz);
            switch (methodName) {
                case "log":
                    logger.info(str, args);
                    XxlJobHelper.log(str, args);
                    break;
                case "debug":
                    logger.debug(str, args);
                    XxlJobHelper.log(str, args);
                    break;
                case "error":
                    logger.error(str, args);
                    logError(str, args);
                    break;
                default:
                    logger.info(str, args);
                    XxlJobHelper.log(str, args);
            }
        }
 
        public static void logError(String str, Object... args) {
            for (Object arg : args) {
                if (arg instanceof Throwable) {
                    XxlJobHelper.log((Throwable)arg);
                    return;
                }
            }
            XxlJobHelper.log(str, args);
        }
    }
}

使用拦截器增强 即可将日志同步输出到xxljob dashboard 中
只需在要打印日志的类中添加一下代码即可 代替原来的@Slf4j 即可

private static final Logger log = LoggerEnhancer.createLogger(KeyExchangeJob.class);

GitHub 加速计划 / xx / xxl-job
27.16 K
10.79 K
下载
xxl-job: 是一个分布式任务调度平台,核心设计目标是开发迅速、学习简单、轻量级、易扩展。
最近提交(Master分支:3 个月前 )
e5d26ba2 - 4 个月前
977ad87b - 4 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐