1.去除springboot-starter的log4j依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
2.引入log4j2
<!-- Log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
3.dubbo-springboot的依賴不需要去除log4j,在依賴樹中只能看到接口層的slf4j,可以兼容log4j2,所以不需要更改
<!--dubbo-springBoot依賴 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
4.更改配置信息:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!-- 設置變量替換屬性 -->
<log4j2.level>debug</log4j2.level>
<log4j2.root.path>/opt/appstack/apache-tomcat/logs/${project.name}</log4j2.root.path>
<log4j2.error.path>/opt/appstack/apache-tomcat/logs/${project.name}-error</log4j2.error.path>
<log4j2.package.path>/opt/appstack/apache-tomcat/logs/${project.name}-kk</log4j2.package.path>
</properties>
5.在resources目錄下新建log4j2-spring.xml
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<properties>
<!-- 文件輸出格式 -->
<property name="PATTERN">log4j2|%d{yyyy-MM-dd HH:mm:ss.SSS} | -%-5level [%thread] %c [%L] -| %msg%n</property>
</properties>
<appenders>
<Console name="CONSOLE" target="system_out">
<PatternLayout pattern="${PATTERN}" />
</Console>
</appenders>
<loggers>
<logger name="com.roncoo.education" level="debug" />
<root level="info">
<appenderref ref="CONSOLE" />
</root>
</loggers>
</configuration>
6.思考
其實上面的步驟,基本上亦可以被用來更換springboot默認使用的logback日誌應用層,因爲dubbo-spring-boot-starter這個依賴中,我本以爲需要去除log4j,在網上查了很久,都查不到這個依賴的信息,查看依賴等級之後沒有看到log4j的依賴,此處存疑,希望有人能告訴我結果.
7.其他問題:
maven的依賴都是很多層的關係,稍有不慎就會出現問題,在我這個項目中,同樣出現一個很奇怪的bug,那就是出現服務層消費者在調用提供者的服務時,出現java.lang.ClassCastException的錯誤,可是明明對象都是一個,經過查詢得知,是springboot dev tools的問題,這裏需要去除這個依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
另外,如果出現跟數據源有關的錯誤的時候,
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
需要在啓動類的註解
@SpringBootApplication()加入exclude = DataSourceAutoConfiguration.class--->
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)