默认采用Logback打印日志
文件名:logback-spring.xml,SpringBoot自动加载xml文件
日志具体配置说明
<configuration>
<!--在控制台显示日志颜色-->
<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"/>
<!--日志文件目录-->
<springProperty scope="context" name="LOG_HOME" source="log.home" defaultValue="/data/logs/web-admin"/>
<!--root的默认日志级别-->
<springProperty scope="context" name="LOG_ROOT_LEVEL" source="log.level" defaultValue="INFO"/>
<springProperty scope="context" name="STDOUT" source="log.stdout" defaultValue="STDOUT"/>
<!--日志文件名前缀-->
<springProperty scope="context" name="LOG_PREFIX" source="spring.application.name"/>
<!--日志内部属性-->
<property name="LOG_CHARSET" value="UTF-8" />
<property name="LOG_DIR" value="${LOG_HOME}/%d{yyyyMMdd}" /> <!--按天生成日志-->
<property name="MAX_FILE_SIZE" value="1MB" /><!--日志文件大小-->
<property name="MAX_HISTORY" value="7"/><!--日志保留天数-->
<!--控制台格式配置-->
<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}}"/>
<!--日志文件中格式配置-->
<property name="LOG_MSG" value="- | [%d{yyyyMMdd HH:mm:ss.SSS}] | [%level]|[${HOSTNAME}] | [%thread]|[%logger{40}] | [--> %msg] |%n "/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<charset>${LOG_CHARSET}</charset>
</encoder>
</appender>
<!--全部日志-->
<appender name="FILE_ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOG_HOME}/all_${LOG_PREFIX}.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_DIR}/all_${LOG_PREFIX}%i.log</FileNamePattern>
<MaxHistory>${MAX_HISTORY}</MaxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>${LOG_MSG}</pattern>
</layout>
</appender>
<!--错误日志-->
<appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<OnMismatch>DENY</OnMismatch>
<OnMatch>ACCEPT</OnMatch>
</filter>
<File>${LOG_HOME}/err_${LOG_PREFIX}.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${LOG_DIR}/err_${LOG_PREFIX}%i.log</FileNamePattern>
<MaxHistory>${MAX_HISTORY}</MaxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>${LOG_MSG}</Pattern>
</layout>
</appender>
<logger name="root" level="INFO"/>
<!--根据不同环境配置日志级别-->
<springProfile name="default">
<logger name="com.shuangxin.admin" level="DEBUG"/>
</springProfile>
<springProfile name="prod">
<logger name="com.shuangxin.admin" level="ERROR"/>
</springProfile>
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE_ALL" />
</root>
</configuration>
效果显示
2018-04-09 16:42:13.602 INFO 8188 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2018-04-09 16:42:13.608 INFO 8188 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2018-04-09 16:42:13.637 INFO 8188 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 29 ms 2018-04-09 16:42:13.727 INFO 8188 --- [nio-8080-exec-1] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
日志按分钟分割效果