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.然后就可以在看到日志了