SpringBoot Logback配置,SpringBoot日志配置
SpringBoot springProfile属性配置
================================
©Copyright 蕃薯耀 2018年3月27日
http://www.cnblogs.com/fanshuyao/
附件下载(源码下载)见:http://fanshuyao.iteye.com/blog/2414522
将logback-spring.xml或者logback.xml放在src/main/resources下,springBoot日志框架会自动识别日志配置。
建议使用logback-spring.xml命名,因为可以使用springBoot的高级属性(springProfile):可以加上开发模式、测试模式、生产模式的配置,利于多环境打包。
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration debug="false">
- <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
- <property name="LOG_HOME" value="/home" />
- <property name="pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n" />
- <!-- 控制台输出 -->
- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
- <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
- <pattern>${pattern}</pattern>
- </encoder>
- </appender>
- <!-- 控制台输出 -->
- <!-- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
- <layout class="ch.qos.logback.classic.PatternLayout">
- <springProfile name="dev">
- <Pattern>%date [%thread] %-5level %logger{80} - %msg%n</Pattern>
- </springProfile>
- </layout>
- </appender> -->
- <!-- 按照每天生成日志文件 -->
- <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
- <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
- <!--日志文件输出的文件名-->
- <FileNamePattern>${LOG_HOME}/TestWeb.log.%d{yyyy-MM-dd}.log</FileNamePattern>
- <!--日志文件保留天数-->
- <MaxHistory>30</MaxHistory>
- </rollingPolicy>
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
- <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
- <pattern>${pattern}</pattern>
- </encoder>
- <!--日志文件最大的大小-->
- <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- <MaxFileSize>10MB</MaxFileSize>
- </triggeringPolicy>
- </appender>
- <!-- 日志输出级别 -->
- <root level="INFO">
- <appender-ref ref="STDOUT" />
- </root>
- </configuration>
springProfile官方说明:
The <springProfile>
tag lets you optionally include or exclude sections of configuration based on the active Spring profiles. Profile sections are supported anywhere within the <configuration>
element. Use the name
attribute to specify which profile accepts the configuration. Multiple profiles can be specified with a comma-separated list. The following listing shows three sample profiles:
- <springProfile name="staging">
- <!-- configuration to be enabled when the "staging" profile is active -->
- </springProfile>
- <springProfile name="dev, staging">
- <!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
- </springProfile>
- <springProfile name="!production">
- <!-- configuration to be enabled when the "production" profile is not active -->
- </springProfile>
例子:
使用springProfile配置,logback日志配置文件必须命名为:logback-spring.xml
- <springProfile name="dev">
- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
- <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
- <pattern>---dev - %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
- </encoder>
- </appender>
- </springProfile>
- <springProfile name="test">
- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
- <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
- <pattern>===test %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
- </encoder>
- </appender>
- </springProfile>
- <springProfile name="zprod">
- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
- <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
- <pattern>+++zprod %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
- </encoder>
- </appender>
- </springProfile>
然后在application.properties配置spring.profiles.active属性:
- spring.profiles.active=zprod
================================
©Copyright 蕃薯耀 2018年3月27日
http://www.cnblogs.com/fanshuyao/