<!-- logback日志-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
注意:springboot内部是有Logback的包,但是本人使用的时候缺少部分类文件,因此单独映入了一次
logback有三张表logging_event、logging_event_exception、logging_event_property,不需要自己创建,只需要在如下位置找到自己保存的库表生成sql复制运行即可.
<configuration
scan="false"
scanPeriod="60 seconds"
debug="false">
<property name="log.filepath" value="logs" />
<property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{100} %msg%n" />
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<springProperty scope="context" name="DATA_SOURCE" source="spring.datasource.type" />
<springProperty scope="context" name="JDBC_URL" source="spring.datasource.dynamic.datasource.yeWu.url" />
<springProperty scope="context" name="USER_NAME" source="spring.datasource.dynamic.datasource.yeWu.username" />
<springProperty scope="context" name="PASSWORD" source="spring.datasource.dynamic.datasource.yeWu.password" />
<springProperty scope="context" name="DRIVER_CLASS_NAME" source="spring.datasource.dynamic.datasource.yeWu.driver-class-name" />
<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
<dataSource class="${DATA_SOURCE}">
<url>${JDBC_URL}url>
<username>${USER_NAME}username>
<password>${PASSWORD}password>
<driverClassName>${DRIVER_CLASS_NAME}driverClassName>
dataSource>
connectionSource>
appender>
<appender name="CONSOLE_APPENDER" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${log.pattern}pattern>
<charset>UTF-8charset>
encoder>
appender>
<appender name="DEBUG_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.filepath}/debug.logfile>
<encoder>
<pattern>${log.pattern}pattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.filepath}/debug-%d{yyyy-MM-dd}.%i.log.gzfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>10maxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>debuglevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="INFO_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.filepath}/info.logfile>
<encoder>
<pattern>${log.pattern}pattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.filepath}/info-%d{yyyy-MM-dd}.%i.log.gzfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>10maxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>infolevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="WARN_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.filepath}/warn.logfile>
<encoder>
<pattern>${log.pattern}pattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.filepath}/warn-%d{yyyy-MM-dd}.%i.log.gzfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>10maxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>warnlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<appender name="ERROR_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.filepath}/error.logfile>
<encoder>
<pattern>${log.pattern}pattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.filepath}/error-%d{yyyy-MM-dd}.%i.log.gzfileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>10maxHistory>
rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>errorlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<root level="info">
<appender-ref ref="DB"/>
<appender-ref ref="CONSOLE_APPENDER" />
<appender-ref ref="DEBUG_APPENDER" />
<appender-ref ref="INFO_APPENDER" />
<appender-ref ref="WARN_APPENDER" />
<appender-ref ref="ERROR_APPENDER" />
root>
configuration>
在上面的配置内容中,最重点的是配置 MySQL 数据库的连接信息和 DBAppender
,并引用 `DBAppender
<springProperty scope="context" name="DATA_SOURCE" source="spring.datasource.type" />
<springProperty scope="context" name="JDBC_URL" source="spring.datasource.druid.url" />
<springProperty scope="context" name="USER_NAME" source="spring.datasource.druid.username" />
<springProperty scope="context" name="PASSWORD" source="spring.datasource.druid.password" />
<springProperty scope="context" name="DRIVER_CLASS_NAME" source="spring.datasource.druid.driver-class-name" />
<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
<dataSource class="${DATA_SOURCE}">
<url>${JDBC_URL}url>
<username>${USER_NAME}username>
<password>${PASSWORD}password>
<driverClassName>${DRIVER_CLASS_NAME}driverClassName>
dataSource>
connectionSource>
appender>
<root level="info">
<appender-ref ref="DB"/>
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
root>
如果只希望记录某个包(如 com.gz.bussines
)的错误日志信息到 MySQL 数据库,可以参考以下配置内容
<appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
<dataSource class="${DATA_SOURCE}">
<url>${JDBC_URL}url>
<username>${USER_NAME}username>
<password>${PASSWORD}password>
<driverClassName>${DRIVER_CLASS_NAME}driverClassName>
dataSource>
connectionSource>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERRORlevel>
<onMatch>ACCEPTonMatch>
<onMismatch>DENYonMismatch>
filter>
appender>
<logger name="com.gz.bussines" value="ERROR" additivity="false" >
<appender-ref ref="DB"/>
<appender-ref ref="CONSOLE" />
<appender-ref ref="ERROR_FILE" />
logger>
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
root>