天天看點

springboot內建logback,報錯java.lang.IllegalStateException: Logback configuration error detected:

   最近需要将一個springboot項目部署到一台新伺服器上。而且新伺服器是完全copy了舊伺服器的環境與項目。但是在新伺服器上啟動該springboot項目,就一直報錯

2019-03-18 18:54:35.532 [main] ERROR o.s.boot.SpringApplication - Application startup failed
java.lang.IllegalStateException: Logback configuration error detected: 
ERROR in [email protected]:13 - RuntimeException in Action for tag [appender] java.lang.IllegalStateException: DBAppender cannot function if the JDBC driver does not support getGeneratedKeys method *and* without a specific SQL dialect
	at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:162)
	at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:81)
	at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:59)
	at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:115)
	at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:303)
	at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:276)
	at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
	at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:212)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
	at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:74)
	at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
	at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:325)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:296)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
	at com.xhl.leads.leadsmail.LeadsMailApplication.main(LeadsMailApplication.java:66)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
	at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:587)
           

看上面報錯資訊說是資料庫相關的問題。找到項目內建的logback-spring.xml,然後搜尋DBAppender這個類,猜測是在連結資料庫時有問題。于是和運維人員确認了一下該配置檔案連接配接的資料庫伺服器的防火牆。經檢視,該資料庫的防火牆,對于通路ip有限制。将新伺服器的ip加入資料庫伺服器的防火牆裡,項目就能正常啟動了。

附贈:logback-spring.xml配置檔案。注意:該配置檔案中有需要自己改動的地方:例如"項目名","資料庫ip","資料庫使用者名","資料庫密碼","資料庫庫名"

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder>
      		<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    	</encoder>
        <!--滾動政策-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路徑-->
            <fileNamePattern>/var/log/項目名/info.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
        <!--滾動政策-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路徑-->
            <fileNamePattern>/var/log/項目名/error.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>
    
    <appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
		<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">
			<driverClass>com.mysql.jdbc.Driver</driverClass>
			<url>jdbc:mysql://資料庫ip:3306/資料庫庫名?useUnicode=true&amp;characterEncoding=UTF-8</url>
			<user>資料庫使用者名</user>
			<password>資料庫密碼</password>
		</connectionSource>
	</appender>
	
	 <appender name ="ASYNC" class= "ch.qos.logback.classic.AsyncAppender">  
            <!-- 不丢失日志.預設的,如果隊列的80%已滿,則會丢棄TRACT、DEBUG、INFO級别的日志 -->  
            <discardingThreshold >0</discardingThreshold>  
            <!-- 更改預設的隊列的深度,該值會影響性能.預設值為256 -->  
            <queueSize>512</queueSize>  
            <!-- 添加附加的appender,最多隻能添加一個 -->  
         <appender-ref ref ="DB"/>  
     </appender>  
    
    <root level="info">
        <appender-ref ref="consoleLog" />
    </root>
    
    <logger name="com.alibaba.druid" level="error" additivity="false">
		<appender-ref ref="consoleLog" />
		<appender-ref ref="fileErrorLog" />
	</logger>
	
	<logger name="org.springframework" level="error" additivity="false">
		<appender-ref ref="consoleLog" />
		<appender-ref ref="fileErrorLog" />
	</logger>
	
	<logger name="org.hibernate" level="error" additivity="false">
		<appender-ref ref="consoleLog" />
		<appender-ref ref="fileErrorLog" />
	</logger>
	
	<logger name="org.apache" level="error" additivity="false">
		<appender-ref ref="consoleLog" />
		<appender-ref ref="fileErrorLog" />
	</logger>
	
	<logger name="com.xhl.cuss" level="info" additivity="false">
	    <appender-ref ref="fileInfoLog" />
		<appender-ref ref="fileErrorLog" />
		<appender-ref ref="consoleLog" />
		<appender-ref ref="ASYNC" />
	</logger>
    
</configuration>