dubbo客户端打印dubbo框架内部日志

1.设置jvm启动参数来指定slf4j(这种方式无论是spring容器还是web容器均可适用)

          -Ddubbo.application.logger=slf4j

Run  As --->Run Configurations--->Arguments--->VM arguments

然后设置 -Ddubbo.application.logger=slf4j

然后apply --->Run

 

2.logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
  
        <!-- dubbo log start-->
    <substitutionProperty name="LOG_HOME_DUBBO" value="dubbologs"/>
    <timestamp key="byDate" datePattern="yyyyMMdd"/>
    <appender name="dubboRolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Encoding>UTF-8</Encoding>
        <file>${LOG_HOME_DUBBO}/MTP-DUBBO.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME_DUBBO}/DEMO-%d{yyyy-MM-dd}.%i-DUBBO.zip</fileNamePattern>
            <maxHistory>30</maxHistory>
            <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <MaxFileSize>2MB</MaxFileSize>
            </TimeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            <immediateFlush>true</immediateFlush>
        </encoder>
    </appender>
    <logger name="com.alibaba.dubbo" level="INFO">
        <appender-ref ref="dubboRolling"/>
    </logger>
     <!-- dubbo log end-->
    <root level="INFO">
        <appender-ref ref="dubboRolling" />
    </root>

    </configuration>
3.测试类中加载logback.xml文件

public static void main(String[] args) throws IOException {
        new TokenServiceFunctionTestFactory().tokenServiceMethodCheckOut();
    }
    public void tokenServiceMethodCheckOut() throws IOException{
        LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();

        //设置dubbo内部框架日志级别
        lc.getLogger("com.alibaba.dubbo").setLevel(Level.valueOf("INFO"));
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(lc);
        lc.reset();
        try {
            configurator.doConfigure("src/main/resources/logback.xml");
       } catch (JoranException e) {
            e.printStackTrace();
        }
        StatusPrinter.printInCaseOfErrorsOrWarnings(lc);

}

4.运行测试类时用下面这个运行

Run  As --->Run Configurations--->Arguments--->Run

5.然后就可以在看到日志了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值