Java项目 切片实现log4j的终极封装

通用型(再也不用每个类new一个logger了)

public class Log {
    
    private static Map<String,Logger> loggerMap = new HashMap<String,Logger>();

    public static void main(String[] args) throws ClassNotFoundException {
        Log.error("自定义LOG","sss444444");
    }
    
    public static void debug(Object message){
        String className = getClassName();
        Logger log = getLogger(className);
        if(log.isDebugEnabled()){
            log.debug(message);
        }
    }
    
    public static void debug(String tag, Object message){
        String className = getClassName();
        Logger log = getLogger(className);
        if(log.isDebugEnabled()){
            log.debug(new StringBuffer().append("【").append(tag).append("】").append(message).toString());
        }
    }

    public static void info(Object message){
        String className = getClassName();
        Logger log = getLogger(className);
        if(log.isInfoEnabled()){
            log.info(message);
        }
    }
    
    public static void info(String tag, Object message){
        String className = getClassName();
        Logger log = getLogger(className);
        if(log.isInfoEnabled()){
            log.info(new StringBuffer().append("【").append(tag).append("】").append(message).toString());
        }
    }
    
    public static void warn(Object message){
        String className = getClassName();
        Logger log = getLogger(className);
        log.warn(message);
    }
    
    public static void warn(String tag, Object message){
        String className = getClassName();
        Logger log = getLogger(className);
        log.warn(new StringBuffer().append("【").append(tag).append("】").append(message).toString());
    }
    
    public static void error(Object message){
        String className = getClassName();
        Logger log = getLogger(className);
        log.error(message);
    }
    
    public static void error(String tag, Object message){
        String className = getClassName();
        Logger log = getLogger(className);
        log.error(new StringBuffer().append("【").append(tag).append("】").append(message).toString());
    }
    
    /**
     * 获取最开始的调用者所在类
     * @return
     */
    private static String getClassName(){
        Throwable th = new Throwable();
        StackTraceElement[] stes = th.getStackTrace();
        StackTraceElement ste = stes[2];
        return ste.getClassName();
    }
    /**
     * 根据类名获得logger对象
     * @param className
     * @return
     */
    private static Logger getLogger(String className){
        Logger log = null;
        if(loggerMap.containsKey(className)){
            log = loggerMap.get(className);
        }else{
            try {
                log = Logger.getLogger(Class.forName(className));
                loggerMap.put(className, log);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return log;
    }
}
 

spring型(上个注释自己帮我们log了)

@Aspect
@Service
public class LoggerAdvice {

    private Logger logger = Logger.getLogger(this.getClass());

    @Before("within(com.example.webmanager..*) && @annotation(loggerManage)")
    public void addBeforeLogger(JoinPoint joinPoint, LoggerManage loggerManage) {
        logger.info("执行 " + loggerManage.description() + " 开始");
        logger.info("地址 "+joinPoint.getSignature().toString());
        logger.info("参数 "+this.parseParames(joinPoint.getArgs()));
    }

    @AfterReturning("within(com.example.webmanager..*) && @annotation(loggerManage)")
    public void addAfterReturningLogger(JoinPoint joinPoint, LoggerManage loggerManage) {
        logger.info("执行 " + loggerManage.description() + " 结束");
    }

    @AfterThrowing(pointcut = "within(com.example.webmanager..*) && @annotation(loggerManage)", throwing = "ex")
    public void addAfterThrowingLogger(JoinPoint joinPoint, LoggerManage loggerManage, Exception ex) {
        logger.error("执行 " + loggerManage.description() + " 异常", ex);
    }

    private String parseParames(Object[] parames) {
        if (null == parames || parames.length <= 0) {
            return "";
        }
        StringBuffer param = new StringBuffer("传入参数[");
        for (Object obj : parames) {
            param.append("{").append(ToStringBuilder.reflectionToString(obj)).append("}  ");
        }
        param.append("]");
        return param.toString();
    }

}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LoggerManage {
     String description();
}

 

posted @ 2018-01-22 17:16  lgp20151222  阅读(190)  评论(0编辑  收藏  举报