欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

动态替换logback FileAppender/RollingFileAppender 配置- Programmatically configure logback FileAppender/RollingBackAppender

程序员文章站 2023-01-30 08:11:50
一、本文实际解决的问题 如何在代码中修改logback的RollingFileAppender配置(本文代码实例为修改日志文件路径) 二、针对的场景: 本文解决的问题属于一个大需求的一部分,需求为:需要特殊处理的请求的相关日志,需要与正常日志分开打印;同时不能去推动全公司应用修改代码,这样成本过高, ......

一、本文实际解决的问题

如何在代码中修改logback的rollingfileappender配置(本文代码实例为修改日志文件路径)

二、针对的场景:

本文解决的问题属于一个大需求的一部分,需求为:需要特殊处理的请求的相关日志,需要与正常日志分开打印;同时不能去推动全公司应用修改代码,这样成本过高,尽量通过引入二方包解决问题

本文主要介绍获取logger之后如何修改logger配置,实现日志输出到别的路径

三、logback logger结构简述

动态替换logback FileAppender/RollingFileAppender 配置- Programmatically configure logback FileAppender/RollingBackAppender

四、实现流程

1、获取logger

private static logger logger = loggerfactory.getlogger(taskhistoryservice.class);

2、获取所有appender

// 获取从logger到rootlogger的所有appenders
    public static arraylist<appender> getappenderstraversal(logger logger){
        logger parent = logger;
        arraylist<appender> result = new arraylist<>();
        while(parent != null){
            arraylist<appender> tempresult = getappenders(parent);
            if (tempresult != null){
                result.addall(tempresult);
            }

            parent = getparent(parent);
        }
        return result;
    }

// 获取logger中的appenders
    public static arraylist<appender> getappenders(logger logger){
        appenderattachableimpl<iloggingevent> aai;
        arraylist<appender> fileappenders = new arraylist<>();
        try {
            class<?> clazz = ch.qos.logback.classic.logger.class;
            field aaifield = clazz.getdeclaredfield("aai");
            aaifield.setaccessible(true);
            appenderattachableimpl<iloggingevent> markedaai;
            aai =  (appenderattachableimpl<iloggingevent>)aaifield.get(logger);
            if (aai == null){
                return null;
            }
            iterator<appender<iloggingevent>> aaiiterator = aai.iteratorforappenders();
            while (aaiiterator.hasnext()){
                appender<ch.qos.logback.classic.spi.iloggingevent> appender = aaiiterator.next();
                fileappenders.add(appender);
            }
            return fileappenders;
        } catch (nosuchfieldexception e){
            e.printstacktrace();
            return fileappenders;
        } catch (illegalaccessexception e){
            e.printstacktrace();
            return fileappenders;
        }
    }
    
// 获取logger的parent logger
    public static logger getparent(logger logger) {
        class<?> clazz = ch.qos.logback.classic.logger.class;
        try {
            field parentfield = clazz.getdeclaredfield("parent");
            parentfield.setaccessible(true);
            logger parent = (logger)parentfield.get(logger);
            return parent;
        } catch (nosuchfieldexception e){
            e.printstacktrace();
            return null;
        } catch (illegalaccessexception e){
            e.printstacktrace();
            return null;
        }
    }

3、获取rollingfileappender

public static rollingfileappender getrollingfileappender(appender appender){
        if (appender instanceof rollingfileappender){
            return (rollingfileappender)appender;
        }
        return null;
    }

4、更新policy

private static rollingfileappender updaterollingpolicytomark(rollingfileappender rollingfileappender, string newfile, string newpattern){
        rollingpolicy rollingpolicy = rollingfileappender.getrollingpolicy();
        triggeringpolicy triggeringpolicy = rollingfileappender.gettriggeringpolicy();
        rollingpolicybase rollingpolicybase = (rollingpolicybase)rollingpolicy;
        rollingpolicybase.setfilenamepattern(newpattern);

        // stop all:appender + policy
        stoprollingfileappender(rollingfileappender);
        // set filename
        setfilename(rollingfileappender, newfile);
        // start
        startrollingfileappender(rollingfileappender);

        return rollingfileappender;
    }

    private static void stoprollingfileappender(rollingfileappender rollingfileappender){
        rollingfileappender.stop();
    }

    private static void startrollingfileappender(rollingfileappender rollingfileappender){
        rollingfileappender.gettriggeringpolicy().start();
        rollingfileappender.getrollingpolicy().start();
        rollingfileappender.start();
    }

    private static void setfilename(rollingfileappender rollingfileappender, string filename){
        rollingpolicy rollingpolicy = rollingfileappender.getrollingpolicy();
        triggeringpolicy triggeringpolicy = rollingfileappender.gettriggeringpolicy();
        // remove policy
        rollingfileappender.setrollingpolicy(null);
        rollingfileappender.settriggeringpolicy(null);

        // set filename
        rollingfileappender.setfile(filename);

        // set policy
        rollingfileappender.setrollingpolicy(rollingpolicy);
        rollingfileappender.settriggeringpolicy(triggeringpolicy);
    }