1 在spring boot中已经自动包含了logback的jar包,如果不是spring boot需要在maven中导入依赖
<!-- /artifact/ch.qos.logback/logback-classic --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency>
2 配置logback的xml
<!-- Logback configuration. See http://logback.qos.ch/manual/index.html --><configuration scan="true" scanPeriod="10 seconds"><!--说明:1、日志级别及文件日志记录采用分级记录,级别与日志文件名相对应,不同级别的日志信息记录到不同的日志文件中例如:error级别记录到log_error_xxx.log或log_error.log(该文件为当前记录的日志文件),而log_error_xxx.log为归档日志,日志文件按日期记录,同一天内,若日志文件大小等于或大于2M,则按0、1、2...顺序分别命名例如log-level--12-21.0.log其它级别的日志也是如此。2、文件路径若开发、测试用,在Eclipse中运行项目,则到Eclipse的安装路径查找logs文件夹,以相对路径../logs。若部署到Tomcat下,则在Tomcat下的logs文件中3、AppenderFILEERROR对应error级别,文件名以log-error-xxx.log形式命名FILEWARN对应warn级别,文件名以log-warn-xxx.log形式命名FILEINFO对应info级别,文件名以log-info-xxx.log形式命名FILEDEBUG对应debug级别,文件名以log-debug-xxx.log形式命名stdout将日志信息输出到控制上,为方便开发测试使用--><include resource="org/springframework/boot/logging/logback/base.xml"/><!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径--><property name="LOG_PATH" value="app-log"/><!-- 彩色日志依赖的渲染类 --><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}}" /><!-- Console 输出设置 --><appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>${CONSOLE_LOG_PATTERN}</pattern><charset>utf8</charset></encoder></appender><appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"><!-- 此日志文件只记录debug级别的 --><filter class="ch.qos.logback.classic.filter.LevelFilter"><level>DEBUG</level><onMatch>ACCEPT</onMatch><onMismatch>DENY</onMismatch></filter><File>${LOG_PATH}/debug.log</File><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><!--日志文件输出的文件名--><fileNamePattern>${LOG_PATH}/debug-%d{yyyyMMdd}.%i.log</fileNamePattern><!--日志文件最大的大小--><timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><maxFileSize>10MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy><!--日志文件保留天数--><maxHistory>15</maxHistory></rollingPolicy><layout class="ch.qos.logback.classic.PatternLayout"><!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符--><Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n%n</Pattern></layout></appender><appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"><filter class="ch.qos.logback.classic.filter.LevelFilter"><level>INFO</level><onMatch>ACCEPT</onMatch><onMismatch>DENY</onMismatch></filter><File>${LOG_PATH}/info.log</File><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${LOG_PATH}/info-%d{yyyyMMdd}.%i.log</fileNamePattern><timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><maxFileSize>10MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy><maxHistory>15</maxHistory></rollingPolicy><layout class="ch.qos.logback.classic.PatternLayout"><Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n%n</Pattern></layout></appender><appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"><filter class="ch.qos.logback.classic.filter.ThresholdFilter"><level>ERROR</level></filter><File>${LOG_PATH}/error.log</File><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${LOG_PATH}/error-%d{yyyyMMdd}.%i.log</fileNamePattern><timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><maxFileSize>10MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy><maxHistory>15</maxHistory></rollingPolicy><layout class="ch.qos.logback.classic.PatternLayout"><Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} -%msg%n%rootException{1}%n%n</Pattern></layout></appender><appender name="console" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %clr(%-5level) %logger{16} %msg%n%rootException{1}</pattern></encoder></appender><logger name="com" level="DEBUG" additivity="false"><appender-ref ref="DEBUG_FILE"/><appender-ref ref="INFO_FILE"/><appender-ref ref="ERROR_FILE"/><appender-ref ref="console"/></logger><root level="INFO"><appender-ref ref="INFO_FILE"/><appender-ref ref="ERROR_FILE"/></root><!--日志异步到数据库 --><!-- <appender name="DB" class="ch.qos.logback.classic.db.DBAppender">日志异步到数据库<connectionSource class="ch.qos.logback.core.db.DriverManagerConnectionSource">连接池<dataSource class="com.mchange.boPooledDataSource"><driverClass>com.mysql.jdbc.Driver</driverClass><url>jdbc:mysql://127.0.0.1:3306/databaseName</url><user>root</user><password>root</password></dataSource></connectionSource></appender> --></configuration>
3 在application.properties 中加入此配置文件
# loglogging.config=classpath:logback.xml